2010-05-14 6 views
1

J'ai un système de menu sur mesure dans lequel je voudrais charger les contrôles utilisateur d'un autre projet dans un contrôle onglet sur mon projet principal (contrôle de menu)Launch Control utilisateur dans un contrôle onglet dynamiquement

du projet de contrôle de l'utilisateur: foobar système Menu projet: Menu

la fonction de les charger dans le contrôle onglet:

private void LaunchWPFApplication(string header, string pPath) 
     { 
      // Header - What loads in the tabs header portion. 
      // pPath - Page where to send the user 

      //Create a new browser tab object 
      BrowserTab bt = tabMain.SelectedItem as BrowserTab; 
      bt = new BrowserTab(); 
      bt.txtHeader.Text = header; 
      bt.myParent = BrowserTabs; 

      //Load in the path 
     try 
     { 
      Type formType = Type.GetType(pPath, true); 
      bt.Content = (UserControl)Activator.CreateInstance(formType); 
     } 
     catch 
     { 
      MessageBox.Show("The specified user control : " + pPath + " cannot be found"); 
     } 

      //Add the browser tab and then focus    
      BrowserTabs.Add(bt); 
      bt.IsSelected = true; 
     } 

Et ce que je vous envoie à la fonction à titre d'exemple:

LaunchWPFApplication("Calculater", "foobar.AppCalculater"); 

Mais à chaque exécution, l'application se plaint que le typeType est null. Je suis confus sur la façon de charger le contrôle de l'utilisateur et curieux si j'envoie les paramètres corrects.

Répondre

1

Le problème que j'avais était d'appeler le Type.GetType. L'appel générique que le MSDN donne est

Type formType = Type.GetType("AppCalculater"); 

qui appelle obtenir le type du nom spécifié. Ce serait toujours n'importe quoi retourner un nul. J'ai ensuite ajouté l'espace de noms au mixage. Ceci cependant me donnait encore une erreur en appelant les autres fichiers du projet dans foobar. Pour obtenir les contrôles utilisateur dans l'autre projet, j'ai ajouté l'assembly après l'appel de contrôle et d'espace de noms.

Type formType = Type.GetType("foobar.AppCalculater,foobar"); 

J'ai ensuite pu référencer dynamiquement tous les contrôles utilisateur en utilisant cet appel. Ainsi, mon appel maintenant mis à jour pour charger un contrôle d'utilisateur d'un autre projet dans mon contrôle onglet est la suivante:

private void LaunchWPFApplication(string header, string pPath) 
     { 
      // Header - What loads in the tabs top portion. 
      // Path - Page where to send the user 

      //Create a new browser tab object 
      BrowserTab bt = tabMain.SelectedItem as BrowserTab; 
      bt = new BrowserTab(); 
      bt.txtHeader.Text = header; 
      bt.myParent = BrowserTabs; 

      //Load in the path 
      try 
      { 
       Type formType = Type.GetType(pPath); //Example "foobar.foobarUserControl,foobar" 
       bt.Content = Activator.CreateInstance(formType); 
      } 
      catch (ArgumentNullException) 
      { 
       MessageBox.Show("The specified user control : " + pPath + " cannot be found"); 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show("An error has occurred while loaded the specified user control : " + pPath + ". It includes the following message : \n" + ex); 
      } 
      //Add the browser tab and then focus  
      try 
      { 
       BrowserTabs.Add(bt); 
      } 
      catch(InvalidOperationException) 
      { 
       MessageBox.Show("Cannot add " + pPath + " into the tab control"); 
      } 
      bt.IsSelected = true; 
     } 
Questions connexes