Application.LoadComponent cannot find resource

别等时光非礼了梦想. 提交于 2020-01-03 08:08:09

问题


I have a xaml file in my project at Ns1\Ns2\myfile.xaml. It's build action is set to Page, with a custom tool of MSBuild:Compile. I'm trying to load this file in a static constructor:

namespace Ns1.Ns2 {
    internal class MyClass {
        static() {
            var obj = Application.LoadComponent(new Uri("/myfile.xaml", UriKind.Relative));
        }
    }
}

However, when I try to run this code, it fails with an exception cannot locate resource 'myfile.xaml'. If I change the URI to an absolute URI:

var obj = Application.LoadComponent(new Uri("pack://application:,,,/ns1/ns2/myfile.xaml", UriKind.Absolute));

it fails with Cannot use absolute URI. I get the same errors if I change the type of myfile.xaml to Resource.

How can I compile and reference myfile.xaml from code?


回答1:


You should specify the assembly name:

Application.LoadComponent(new Uri("/AssemblyName;component/myfile.xaml", UriKind.Relative))

Alternatively, if the file has a code-behind class, you can just 'new' it, and the generated code will load the associated XAML.



来源:https://stackoverflow.com/questions/15336917/application-loadcomponent-cannot-find-resource

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!