简体   繁体   English

在XAML中定义的WPF窗口上使用泛型参数

[英]Using generic arguments on WPF Window defined in XAML

I'm trying to create a Window-derived class in XAML which can take a generic argument, but I can't seem to define the generic argument in the XAML so that it generates the partial class matching my code-behind file. 我正在尝试在XAML中创建一个Window派生类,它可以采用泛型参数,但我似乎无法在XAML中定义泛型参数,以便它生成与我的代码隐藏文件匹配的部分类。

What I'm trying to accomplish is a replacement for all the MessageBox calls for asking the user questions, where I can give meaningful button captions ('Save and quit'/'Quit without saving'/'Don't quit' type thing). 我想要完成的是替换所有MessageBox调用以询问用户问题,我可以在哪里给出有意义的按钮标题('保存并退出'/'退出而不保存'/'不要退出'类型的东西) 。 I'd like to be able to pass the window a generic argument constrained to System.Enum defining the return value for the selected option: 我希望能够向窗口传递一个限制为System.Enum的泛型参数,该参数定义所选选项的返回值:

<Window x:Class="EvilPenguin.MultipleChoiceQuestionBox">
    ...

public partial class MultipleChoiceQuestionBox<T> : Window where T : System.Enum
{
    public MultipleChoiceQuestionBox()
    {
        InitializeComponent();
    }

    public T SelectedOption
    {
        get;
    }
}
  • Is there a way I can make my XAML generate a partial class with the correct generic argument? 有没有办法让我的XAML生成一个具有正确泛型参数的分部类?
  • Am I 'doing it wrong'? 我做错了吗? Is this a bad idea for some reason, or is there an easier way? 出于某种原因这是一个坏主意,还是有更简单的方法?
  • Is this not possible in XAML at the moment? 目前在XAML中这是不可能的吗? The x:TypeArgument attribute doesn't quite do what I want, but it suggests that at least some aspects of XAML are aware of generic arguments x:TypeArgument属性并不能完全符合我的要求,但它表明XAML的至少某些方面知道泛型参数

Any help or hints are much appreciated 任何帮助或提示都非常感谢

You can't do it. 你不能这样做。 Here is my answer to this similar SO question : 以下是我对这个类似SO问题的回答

No, you can't declare a generic type in XAML. 不,您不能在XAML中声明泛型类型。 From http://social.msdn.microsoft.com/forums/en-US/wpf/thread/02ca0499-80af-4c56-bb80-f1185a619a9e : 来自http://social.msdn.microsoft.com/forums/en-US/wpf/thread/02ca0499-80af-4c56-bb80-f1185a619a9e

Hello, you can use generic as long as you don't use XAML. 您好,只要您不使用XAML就可以使用泛型。 But unfortunately, if you want to use XAML to define your control, you can't use generic… 但不幸的是,如果你想使用XAML来定义你的控件,你就不能使用泛型...

You can create a control in XAML that inherits from a generic type by putting a x:TypeArguments attribute on the root tag, but the control itself must be concrete. 您可以通过在根标记上放置x:TypeArguments属性来在XAML中创建继承自泛型类型的控件,但控件本身必须是具体的。

I'm not a XAML expert, but a quick google search for "generics in XAML markup" lead me to this MSDN article: http://msdn.microsoft.com/en-us/library/ee956431.aspx 我不是XAML专家,但快速谷歌搜索“XAML标记中的泛型”引导我阅读这篇MSDN文章: http//msdn.microsoft.com/en-us/library/ee956431.aspx

It seems to indicate that you can indeed use generics in XAML. 它似乎表明你确实可以在XAML中使用泛型。 See if this fits your scenario. 看看这是否适合您的情况。

<my:BusinessObject x:TypeArguments="x:String,x:Int32"/>

Try using x:Subclass and make the subclass a generic. 尝试使用x:Subclass并使子类成为泛型。 This allows the base class to be designed (and load the XAML) and the derived to use a generic type. 这允许设计(并加载XAML)基类,并使用泛型类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM