Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Application.LoadComponent cannot find resource

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?

like image 330
thecoop Avatar asked Mar 11 '13 11:03

thecoop


1 Answers

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.

like image 124
Eli Arbel Avatar answered Sep 27 '22 16:09

Eli Arbel