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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With