创建多个.xaml视图 [英] Creating multiple .xaml Views
问题描述
我想创建一个名为 view.xaml
的.xaml UserControl的多个实例,该实例位于程序集 dir1\asm.dll
和 dir2\asm.dll
,而 asm.dll
是同一程序集,只是不同在版本号和view.xaml的实现中。
我有以下设置:
public void TestCreation(){
汇编asm = null;
asm = Assembly.LoadFile( dir1\asm.dll);
CreateView(asm); //有效!
asm = Assembly.LoadFile( dir2\asm.dll);
CreateView(asm); //有效!
asm = Assembly.LoadFile( dir1\asm.dll);
CreateView(asm); //失败!
}
public void CreateView(Assembly assembly)
{
Type type = assembly.GetTypes()。First< Type>(t => ; t.Name.Equals( View));
UserControl视图=(UserControl)assembly.CreateInstance(type.FullName,false,BindingFlags.CreateInstance,null,新对象[] {},null,null);
}
我收到以下异常:
带有异常详细信息
我能够在我视图的InitializeComponent()方法中一直跟踪到该位置的问题。 xaml:
,尤其是在InitializeComponent( ):
在经历了一周的苦苦挣扎之后,我终于找到了两个原因问题和解决方案。
问题出在自动生成的 *。gics
文件中由 UserControl
的 InitializeComponent()
方法调用,如下所示:
此文件生成表示路径的字符串(资源定位器)如下所示:
现在,如果您拥有同一程序集的多个版本,并且两个版本包含相同的xaml文件, WPF 不知道要实例化哪个xaml文件,因为资源定位器仅引用程序集的名称,而不引用程序集的版本。
这将导致 TargetInvocationException
,表示
{组件'MyNamespace.MyUserControl'没有由URI'/MyAssembly;comoponent/myusercontrol.xaml'标识的资源}
如下:
此商品的赠送金额为:
I want to create multiple instances of a .xaml UserControl named view.xaml
, which resides in an assembly dir1\asm.dll
and dir2\asm.dll
whereas asm.dll
is the same assembly that only differs in its version number and the implementation of view.xaml.
I have the following setup:
public void TestCreation() {
Assembly asm = null;
asm = Assembly.LoadFile("dir1\asm.dll");
CreateView(asm); // works!
asm = Assembly.LoadFile("dir2\asm.dll");
CreateView(asm); // works!
asm = Assembly.LoadFile("dir1\asm.dll");
CreateView(asm); // FAILS!
}
public void CreateView(Assembly assembly)
{
Type type = assembly.GetTypes().First<Type>(t => t.Name.Equals("View"));
UserControl view = (UserControl)assembly.CreateInstance(type.FullName, false, BindingFlags.CreateInstance, null, new object[] { }, null, null);
}
I am getting the following Exception:
with the Exception detail
I was able to track the problem up to this location in the InitializeComponent() method of my view.xaml:
and more specificially within InitializeComponent():
After a week suffering and laboring with this issue, I finally found both the reason for the problem and its solution.
The problem lies within the auto-generated *.g.i.cs
file, which is called by the InitializeComponent()
method of a UserControl
, as seen by the following:
This file generates a string (a Resource Locator) that expresses the path to that xaml-component, as seen by the following:
Now, if you have multiple versions of the same assembly and both versions include the same xaml-file, WPF does not know what xaml-file to instantiate, because the Resource Locator only references the name of the assembly but not its version.
This results in a TargetInvocationException
, saying that
{"The component 'MyNamespace.MyUserControl' does not have a resource identified by the URI '/MyAssembly;comoponent/myusercontrol.xaml'"}
as follows:
The simple (but most definitely not obvious) solution for this is to add the version of the assembly to this Resource Locator. This can be achieved by modifying the build-file of the project by adding the <AssemblyVersion>
-tag as follows:
Credits for this go to:
这篇关于创建多个.xaml视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!