在选项卡控件中动态启动用户控件
我有一个自定义构建的菜单系统,我想在其中将用户控件从另一个项目加载到我的主项目(菜单控件)上的选项卡控件中
用户控件项目: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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我遇到的问题是调用 Type.GetType。 MSDN 给出的通用调用
是获取指定名称的类型。无论怎样,这仍然会返回 null。然后我将命名空间添加到组合中。
然而,这仍然给我调用 foobar 中的其他项目文件时出错。为了获取其他项目中的用户控件,我在控件和命名空间调用之后添加了程序集。
然后,我可以使用此调用动态引用所有用户控件。因此,我现在更新的将用户控件从另一个项目加载到我的选项卡控件中的调用如下:
The problem I had was in calling the Type.GetType. The generic call which the MSDN gives is
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.
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.
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 :