我在调用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;
}