Click here to Skip to main content
11,920,377 members (53,297 online)
Rate this:
Please Sign up or sign in to vote.
See more: Windows WPF Generics
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;

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;
public partial class Choosen<T1, T2> : Window
    public Choosen(T1 uc, List<T2> list)
        InitializeComponent();  //it can't initialize now;
Posted 30-Jun-13 21:15pm
Edited 30-Jun-13 21:22pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.

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.
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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

To #1:
I do like this now:
public partial class Choosen<T1, T2> : Window
       where T1 : UserControl, new()
       where T2 : Entities, new()
public Choosen(T1 usercontrol, List<T2> list, bool canChooseAll)

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"

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

  Print Answers RSS
0 OriginalGriff 415
1 phil.o 170
3 F-ES Sitecore 130
4 RyanDev 120
0 OriginalGriff 6,778
1 KrunalRohit 4,495
2 Sergey Alexandrovich Kryukov 3,203
3 George Jonsson 2,805
4 Suvendu Shekhar Giri 2,181

Advertise | Privacy | Mobile
Web03 | 2.8.151120.1 | Last Updated 1 Jul 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100