动态加载资源字典文件到WPF应用程序给出了一个错误出了、字典、应用程序、加载

2023-09-03 07:33:39 作者:扣脚大汉i

我想用动态的语句添加一个XAML资源文件,

  Application.Current.Resources.MergedDictionaries.Add(新的ResourceDictionary(){源=新的URI(资源/ leaf_styles.xaml,UriKind.Relative)});
 

这是抛出一个异常,找不到资源的资源/ leaf_styles.xaml。

我添加了leaf_styles.xaml文件的项目资源文件夹下的的BuildAction设置为内容,CopyAlways设置为True。不过我得到这个错误。能有人帮我指点什么是错?

信息 -

我不想嵌入XAML文件作为资源 目前的项目是一个.NET 3.5类库项目 以上mergedictionary语句写在属于同一项目的类 我还添加了[总成:AssemblyAssociatedContentFile(资源/ leaf_styles.xaml)手动一旦我计算过,这是行不通的(测试)

更新

如果我把它作为一个绝对的位置,它工作正常。

  Application.Current.Resources.MergedDictionaries.Add(新的ResourceDictionary(){源=新的URI(@D:\ FOO \干线\ BIN \资源\ leaf_styles.xaml UriKind.Absolute)});
 
WPF Application Management应用程序管理 1

解决方案

最后,它的工作。这是我做的,

赴通http://msdn.microsoft.com/en-us/library/aa970069.aspx.

改变了URI模式以

 变种富=新的URI(包:// siteoforigin:,,, /资源/ leaf_styles.xaml,UriKind.RelativeOrAbsolute);
Application.Current.Resources.MergedDictionaries.Add(新的ResourceDictionary(){源= FOO});
 

I am trying to add a xaml resource file dynamically using the statement,

Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("resources/leaf_styles.xaml", UriKind.Relative) });

This is throwing an exception, Cannot locate resource 'resources/leaf_styles.xaml'.

I added the leaf_styles.xaml file to the project under resource folder and the BuildAction is set to "Content", CopyAlways is set to True. Still I get this error. Could some one help me out pointing whats wrong??

Additional information -

I don't want to embed the xaml file as a resource The current project is a .net 3.5 class library project The above mergedictionary statement is written in a class belonging to the same project I also added the [assembly: AssemblyAssociatedContentFile("resources/leaf_styles.xaml")] manually once I figured that this is not working (for testing)

Update

If I give it as an absolute location, it is working properly.

Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(@"D:\foo\trunk\bin\resources\leaf_styles.xaml", UriKind.Absolute) });

解决方案

At last, it worked. Here is what I did,

Went thru' http://msdn.microsoft.com/en-us/library/aa970069.aspx.

Changed the Uri pattern to

var foo = new Uri("pack://siteoforigin:,,,/resources/leaf_styles.xaml", UriKind.RelativeOrAbsolute);
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = foo });