如何在 wpf 中使用通用基类而不是窗口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17841336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:16:36  来源:igfitidea点击:

How do I use generic base class instead of window in wpf?

wpfxamlgenericsinheritance

提问by Price Jones

How do I make this work?

我如何使这项工作?

public partial class MyWindow : View<MyViewModel>
{
}

where View is defined as

其中 View 定义为

public class View<T> : Window where T : IViewModel, new()
{
}

XAML:

XAML:

<local:View
x:Class="Project.MyWindow"
x:TypeArguments="ViewModels:MyViewModel"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" 
xmlns:ViewModels="clr-namespace:Mynamespace.ViewModels;assembly=Mynamespace.ViewModels"
xmlns:local="clr-namespace:Project"
>

I get this error... The name View does not exist in the namespace Project... which of course it does.

我收到此错误...名称视图在命名空间 Project 中不存在...当然它确实存在。

and I get this error which really confuses me... The property "TypeArguments" does not exist in the "http://schemas.microsoft.com/winfx/2006/xaml" namespace... which of course it does.

我得到了这个错误,这让我很困惑......“ http://schemas.microsoft.com/winfx/2006/xaml”命名空间中不存在属性“TypeArguments” ......当然它确实存在。

Any clues on how to use generics as a base class for windows in wpf?

关于如何在 wpf 中使用泛型作为 Windows 基类的任何线索?

回答by Price Jones

The answer to this question is to never use generics with xaml. There is always a better way or a workaround. See the comments on the original post.

这个问题的答案是永远不要在 xaml 中使用泛型。总有更好的方法或解决方法。见原帖的评论。

EDIT!

编辑!

Thanks to Micky Duncan (see the comments above) the correct answer to this question can be found by investigating these two links:

感谢 Micky Duncan(见上面的评论),可以通过调查这两个链接找到这个问题的正确答案:

http://msdn.microsoft.com/en-us/library/ms741843(v=vs.110).aspxhttp://www.codeproject.com/Articles/37317/Generic-Support-In-XAML

http://msdn.microsoft.com/en-us/library/ms741843(v=vs.110).aspx http://www.codeproject.com/Articles/37317/Generic-Support-In-XAML