Application.LoadComponent找不到资源 [英] Application.LoadComponent cannot find resource

查看:2054
本文介绍了Application.LoadComponent找不到资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在在我的项目中的XAML文件Ns1与\ NS2 \ myfile.xaml 。它的生成操作设置为页面,用的MSBuild的自定义工具:编译。我试图加载该文件中的静态构造函数:

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));
        }
    }
}

然而,当我尝试运行此code时,出现异常找不到资源myfile.xaml。如果我的URI改为绝对URI:

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));

失败与不能用绝对URI 。我得到同样的错误,如果我改变myfile.xaml的类型资源。

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

我如何编译和code基准myfile.xaml?

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

推荐答案

您应该指定程序集名称:

You should specify the assembly name:

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

另外,如果文件中有一个code-behind类,你可以只新了,并将生成的code将加载相关的XAML。

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

这篇关于Application.LoadComponent找不到资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆