Set resource URI of .xaml component

2019-08-08 21:08发布

I have a .xaml UserControl named MyUserControl.xaml and I want to set its resource URI.

Per default, WPF generates a URI that includes a resource name, which is equal to the resource it belongs to such as

"/MyNamespace;component/myusercontrol.xaml"

for the .xaml named MyUserControl.xaml

How can I have a UserControl MyUserControl.xaml and make WPF generate an individual resource identifies such as

"/MyNamespace;component/myusercontrol_A.xaml" or "/MyNamespace;component/myusercontrol_B.xaml" ?

The reason why I want to do that is described here.

In the image below you can see the resource identifier I am talking about:

enter image description here

and therein:

enter image description here

1条回答
Ridiculous、
2楼-- · 2019-08-08 21:54

Note, that that question is the origin of this question and might help to understand its background.

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:

enter image description here

This file generates a string (a Resource Locator) that expresses the path to that xaml-component, as seen by the following:

enter image description here

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:

enter image description here

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:

enter image description here

Credits for this go to:

查看更多
登录 后发表回答