How to specify generic type argument in XAML
Asked Answered
W

2

7

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.

earlier i had the view xaml representation as

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

</base:BaseView>

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

Weka answered 27/9, 2011 at 18:7 Comment(0)
C
14

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

For instance:

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

For .NET 3.5:

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.

Civilian answered 27/9, 2011 at 18:48 Comment(6)
can you tell me how it will look in my scenario where i wanna specifiy the TemplateArgument for a Root node of the view.Weka
i jus came across this article, blogs.msdn.com/b/wpfsldesigner/archive/2010/01/22/… which describes an existing issue. And i was able to mark the TypeArgument to get it working. Thanks Sll!Weka
where can I get this "XAML 2009" thing? I have .Net 4.6.2 and still my wpf application is using XAML 2006?Radom
@M.kazemAkhgary This is the standard and partially implemented in WPF, please read corresponding MSDN paperCivilian
@Civilian If it's possible, could you provide an example of how to actually use this? I put a question up before coming across this answer. I really appreciate any helpNorvan
Working link for what @Weka commented: web.archive.org/web/20140422181636/http://blogs.msdn.com/b/…Norvan
G
1

In case this happens to someone. I had a similar scenario where I converted my base class to a templated class (i.e. BaseView to BaseView). I kept receiving errors in the InitializeComponent() method. I was receiving the null exception error in the x:Class base type's InitializeComponent() call. I was able to resolve the errors by removing the form-level events from the XAML definition. If I had to keep the form-level events I would need to move them to BaseView.

Geer answered 26/7, 2021 at 20:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.