代码之家  ›  专栏  ›  技术社区  ›  Jacob Saylor

在选项卡控件中动态启动用户控件

  •  1
  • Jacob Saylor  · 技术社区  · 14 年前

    我有一个定制的菜单系统,在其中我想将用户控件从另一个项目加载到我的主项目(菜单控件)上的选项卡控件中。

    用户控制项目:foobar 菜单系统项目:菜单

    将它们加载到选项卡控件中的函数:

    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;
            }
    

    以及我发送给函数的示例:

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

    但每次运行时,应用程序都会抱怨FormType为空。我对如何加载用户控件感到困惑,并且好奇是否发送了正确的参数。

    1 回复  |  直到 14 年前
        1
  •  1
  •   Jacob Saylor    14 年前

    我在调用type.gettype时遇到的问题。一般调用 MSDN 给予是

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

    哪些调用获取指定名称的类型。不管返回空值是什么,这仍然会发生。然后我将名称空间添加到组合中。

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

    然而,这仍然让我在调用foobar中的其他项目文件时出错。为了在另一个项目中获取用户控件,我在控件和名称空间调用之后添加了程序集。

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

    然后我可以使用这个调用动态引用所有用户控件。因此,我现在更新的将用户控件从另一个项目加载到我的选项卡控件的调用如下:

    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;
            }