Click here to Skip to main content
Rate this: bad
good
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
v2
Rate this: bad
good
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.
 
—SA
  Permalink  
v2
Comments
Elan.Cao at 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 at 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
Rate this: bad
good
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)
       {
           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"
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 390
1 Sergey Alexandrovich Kryukov 329
2 BillWoodruff 210
3 Afzaal Ahmad Zeeshan 204
4 CPallini 185
0 OriginalGriff 5,560
1 DamithSL 4,476
2 Maciej Los 3,942
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,175


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 1 Jul 2013
Copyright © CodeProject, 1999-2014
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