如何在XAML中指定泛型类型参数 [英] How to specify generic type argument in XAML

查看:405
本文介绍了如何在XAML中指定泛型类型参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的MVP-PRISM WPF应用程序具有BaseView.现在由于某种原因,我们考虑将_presenter作为BaseView中的Templated字段.

I have a BaseView for my MVP - PRISM WPF application. Now for some reason we thought to make the _presenter as a Templated field in the BaseView.

之前,我将xaml表示为

earlier i had the view xaml representation as

<base:BaseView xamlns:base="clr address of the dll which had BaseView" >

</base:BaseView>

现在,因为我已经将BaseView更改为BaseView<TPresenter>,,那么我该如何编写Xaml?

now since i have changed the BaseView to BaseView<TPresenter>, So how shall i write the Xaml then?

推荐答案

.NET 4 Framework和XAML 2009开始,您就可以这样做. 请参阅MSDN XAML中的泛型

You can do it since .NET 4 Framework and XAML 2009. See Generics in XAML on MSDN

例如:

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

对于.NET 3.5:

For .NET 3.5:

对于专门针对WPF的XAML 2006使用,x:Class也必须 在与x:TypeArguments相同的元素上提供,并且该元素 必须是XAML文档中的根元素.根元素必须映射 具有至少一个类型参数的泛型类型.一个例子是 PageFunction.

For XAML 2006 usage when specifically targeting WPF, x:Class must also be provided on the same element as x:TypeArguments, and that element must be the root element in a XAML document. The root element must map to a generic type with at least one type argument. An example is PageFunction.

支持通用用法的可能解决方法包括定义一个 自定义标记扩展,可以返回通用类型,或提供 包装类定义,该类定义派生自泛型类型,但 使通用约束在其自己的类定义中变平整.

Possible workarounds to support generic usages include defining a custom markup extension that can return generic types, or providing a wrapping class definition that derives from a generic type but flattens the generic constraint in its own class definition.

这篇关于如何在XAML中指定泛型类型参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆