正在ClassLibrary dll中加载XAML资源文件
本文关键字:XAML 资源 源文件 加载 ClassLibrary dll 正在 | 更新日期: 2023-09-27 17:59:45
我有一个WPF应用程序,它包含一个标记为ressource(buildprocess)的xaml文件。在运行时,我可以使用以下代码加载和访问该文件:
Uri uri = new Uri("some.xaml", UriKind.Relative);
System.Windows.Resources.StreamResourceInfo sri = Application.GetResourceStream(uri);
System.Windows.Markup.XamlReader reader = new System.Windows.Markup.XamlReader();
Border savedBorder = reader.LoadAsync(sri.Stream) as Border;
将功能和xaml文件移到类库中,并将其标记为"some.xml"作为资源,上面的代码失败,出现IOException"找不到资源"some.xml"。这听起来很合理,因为我无法成功访问类库中的Application.GetResourceStream
。
System.Reflection.Assembly
.GetExecutingAssembly()
.GetManifestResourceStream("some.xaml") ;
返回CCD_ 2。
访问dll中的此类文件的正确方法是什么?
您的资源名称不正确。Visual Studio使用完全限定的名称(例如名称空间和资源名称)创建资源。
请参阅如何读取嵌入式资源文本文件
试试类似的东西:
System.Reflection.Assembly
.GetExecutingAssembly()
.GetManifestResourceStream("mycompany.myproduct.some.xaml");
我使用了完整的资源名称。此代码解决了问题:
Assembly assembly = GetExecutingAssembly();
using (var stream = assembly.GetManifestResourceStream(this.GetType(), "some.xaml"))
{
StreamReader sr = new StreamReader(stream);
string Content = sr.ReadToEnd();
}
因此,无需指定完整的资源名称。
两个答案都是对的,但我必须注意到一个不精确之处:资源名称应该是另一种格式。
var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("UCM.WFDesigner.Resources.Flows.DefaultFlow.xaml");
CCD_ 3是我的程序集名称,CCD_ 4是文件夹名称,并且CCD_ 5是xaml名称。