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

发布于 2024-09-01 13:46:15 字数 1194 浏览 3 评论 0原文

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

用户控件项目: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 为空。我对如何加载用户控件感到困惑,并且好奇我是否发送了正确的参数。

I have a custom built menu system in which I would like to load user controls from another project into a tab control on my main project (menu control)

User control Project : foobar
Menu system Project : Menu

The function to load them into the tab control:

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

And what I send to the function as an example:

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

But every time run, the application complains that the formType is null. I am confused on how to load the user control and curious if I'm sending the correct parameters.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柳若烟 2024-09-08 13:46:15

我遇到的问题是调用 Type.GetType。 MSDN 给出的通用调用

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

是获取指定名称的类型。无论怎样,这仍然会返回 null。然后我将命名空间添加到组合中。

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

The problem I had was in calling the Type.GetType. The generic call which the MSDN gives is

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

which calls get the type of the specified name. This would still no matter what return a null. I then added the namespace to the mix.

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

This however was still giving me an error calling the other project files in foobar. To get the user controls in the other project, I added the assembly after the control and namespace call.

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

I then was able to reference all the user controls dynamically using this call. Thus my now updated call to load a user control from another project into my tab control is as follows :

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;
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文