Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How the Windows' constructor can accept 2 Generics parameter?
Such like this:

Choosen is a Window;
CustomerControl、UsersControl、StyleControl are UserControls;
CutomerInfo、UsersInfo、StyleInfo are classes;

C#
CustomerControl cc = new CustomerControl();
List<CutomerInfo> lci = new List<CutomerInfo>(10);

UsersControl uc = new UsersControl();
List<UsersInfo> lui = new List<UsersInfo>(20);

StyleControl sc = new StyleControl();
List<StyleInfo> lsi = new List<StyleInfo>(30);

Choosen chooseCustomer = new Choosen(cc,lci);chooseCustomer.Show();
Choosen chooseUsers = new Choosen(uc,lui);chooseUsers.Show();
Choosen chooseStyle = new Choosen(sc,lsi);chooseStyle.Show();


I tried like this,but the Window doesn't work;
C#
public partial class Choosen<T1, T2> : Window
{
    public Choosen(T1 uc, List<T2> list)
    {
        InitializeComponent();  //it can't initialize now;
    }
}
Posted
Updated 30-Jun-13 20:22pm
v2

You are confusing method parameters and generic parameters. Read about both, to see how they work. Your InitializeComponent is the auto-generated code, generated from your XAML. It is designed to work with classes derived from the class Window, and the constructor should have no parameters.

In principal, such constructor can work (you did not explain why "it does not work", it depends on how you use it), but I doubt it makes any sense.

And such code is not designed to work with generic classes. You should rather redesign your code. But if you want to use generics, you need to learn them properly. After looking at your code, I doubt you have a clue.

If you want to get some definitive advice, you need to explain your ultimate goal.

—SA
 
Share this answer
 
v2
Comments
Elan.Cao 1-Jul-13 3:47am    
Thanks,and in fact,i want to initializes different UserControl and add them to the "Choosen" Window by different Entities.
Sergey Alexandrovich Kryukov 1-Jul-13 8:23am    
I would say, don't make it a constructor. You should make it a separate post-construction addition. Then it can work. In such cases, never rely on auto-generated code. If you have a doubt on how to add controls without XAML, look at the auto-generated code to learn the technique.

If this is clear, please accept the answer formally (green button). In all cases, I'll answer your follow-up questions.
—SA
To #1:
I do like this now:
C#
public partial class Choosen<T1, T2> : Window
       where T1 : UserControl, new()
       where T2 : Entities, new()
{
public Choosen(T1 usercontrol, List<T2> list, bool canChooseAll)
       {
           InitializeComponent();
       }
}


to do like this ,I have to edit the Choosen.g.i.cs to change the class's name "Choosen" to "Choosen<T1, T2>",but when i compile it ,the Choosen.g.i.cs file will be set to default,and raise an error:"Can't find InitializeComponent"
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900