2

I have a litte problems with these class

public class MyClass : MyGenericClass<String, Int32>
{

}

// XAML Class
public class MyGenericClass<T, U> : MyGenericClassBase<T, U>
    where U : class
    where T : class
{
    public MyGenericClass()
    {
        InitializeComponent();
    }
}

public class MyGenericClassBase<T, U>
    where U : class, new()
    where T : class, new()
{
    T _t;
    U _u;
    public MyGenericClassBase()
    {

    }
}

I want to did the class "MyGenericClass" in XAML but I can't !

I try it :

<MyGenericClassBase x:Class="MyGenericClass"  
         x:TypeArguments="class,class"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >

       ...

How can i pass the argument type at my "MyGenericClass" and the inherit class "MyGenericClassBase "

Thanks a lot

Nico

Tim Post
  • 33,371
  • 15
  • 110
  • 174
Nicolas
  • 21
  • 1
  • Please use the comment system under the answers you receive to write comments. Answers are reserved for just that, answers. – Tim Post Sep 23 '11 at 11:25

1 Answers1

3

XAML 2006: You can't use generic types in xaml 2006. The easiest solution is to use MyClass directly. Check out this question for other workarounds: Can I specify a generic type in XAML (pre .NET 4 Framework)?

XAML 2009: Generic types are supported natively:

<MyGenericClass x:TypeArguments="x:Int32, x:String"> 
    ...
</MyGenericClass>
Community
  • 1
  • 1
alf
  • 18,372
  • 10
  • 61
  • 92
  • Hi Alfonso and thanks for your quick answer, But this topic didn't help me ! I can't use "MyClass" in XAML ! And my question is if I can use Generic Types in XAML Class ( not for the inherit class "MyGenericClassBase" but for my current class "MyGenericClass" ! Thanks – Nicolas Sep 23 '11 at 08:17
  • After doing some digging I found that generic types are supported natively in XAML 2009. I edited my answer to show you how to do this. – alf Sep 23 '11 at 12:12
  • I know this case but it's working only for the inherited class and not for my primary class "MyGenericClass". The parameter x:TypeArguments of your code are only for the class "MyGenericClassBase" ! If you have another Idea ? thanks – Nicolas Sep 23 '11 at 12:54
  • Please could you tell what xmlns:x and where I should specify? Example gives exception "'x' is an undeclared prefix." after invoking XamlServices.Parse – Ivan Akcheurov Feb 08 '12 at 14:32
  • 1
    OK, xmlns:x should be specified as xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" on the MyGenericClass element itself or ancestor element. That's despite of x:String being from XAML 2009. – Ivan Akcheurov Feb 08 '12 at 14:51