在WPF应用程序中使用.NET Standard程序集中的内容文件 [英] Use a Content File from a .NET Standard assembly in a WPF application
问题描述
我想将文件嵌入.NET Standard程序集中,并在WPF应用程序的XAML中使用它.
如果将生成操作设置为 Resource
,则很容易在其他程序集中使用嵌入式文件,但是必须使用< Project Sdk ="Microsoft.NET.Sdk.WindowsDesktop>
和< UseWPF> true</UseWPF>
即可使用 Resource
构建操作,如下所示:
< PropertyGroup>< TargetFramework> netcoreapp3.1</TargetFramework>< UseWPF> true</UseWPF></PropertyGroup>< ItemGroup>< Resource Include ="Resources \ Image.png"/></ItemGroup>
然后您可以在另一个程序集中使用此Uri:
pack://application:,,/ReferencedAssembly; component/Resources/Image.png
如此处所示:
我的问题:
如果仅需要< Project Sdk ="Microsoft.NET.Sdk">
,并且不希望< UseWPF> true</UseWPF>
您必须使用 Content
构建操作,因为它不需要WPF,如下所示:
https://docs.microsoft.com/zh-CN/visualstudio/ide/build-actions
并像这样使用它:
< PropertyGroup>< TargetFramework> netstandard2.0</TargetFramework></PropertyGroup>< ItemGroup>< Content Include ="Resources \ Image.png">< CopyToOutputDirectory> PreserveNewest</CopyToOutputDirectory></内容></ItemGroup>
然后,您应该可以从另一个程序集中使用此Uri:
pack://application:,,,/Resources/Image.png
如此处所示:
但是我得到以下异常:
引发的异常:System.Private.CoreLib.dll中的"System.Resources.MissingManifestResourceException"抛出异常:PresentationFramework.dll中的"System.IO.IOException"抛出异常:PresentationCore.dll中的"System.IO.IOException"System.Windows.Data错误:6:'TargetDefaultValueConverter'转换器无法转换值'pack://application:,,,/Resources/Image.png'(类型'String');如果可用,将使用后备值.BindingExpression:Path = BackgroundImage;DataItem ='NavigationNode'(HashCode = 663215);目标元素是'Image'(Name ='');target属性为源"(类型为"ImageSource")IOException:'System.IO.IOException:无法找到资源"resources/image.png".
我已经清理并重建了整个解决方案.
我在做什么错了?
重要提示:我需要在xaml中使用包URI-太多,无法将所有现有代码从xaml转换为CS!
如果引用要复制到输出目录的内容文件,则可以使用路径而不是包URI.试试这个:
image.Source =新的BitmapImage(新的Uri($ @"{System.AppContext.BaseDirectory} \ Resources \ Image.png",UriKind.Absolute));
如果出于某些原因仍需要使用包URI,请用 pack://siteoforigin:、、、
替换 pack://application:、、、
在路径中,它应该可以工作.
I want to embed a file in a .NET Standard assembly and use it in XAML in a WPF app.
If you set the build action to Resource
it is very easy to use the embedded file in other assemblies, but you have to use <Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
and <UseWPF>true</UseWPF>
to be able to use the Resource
build action like so:
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<UseWPF>true</UseWPF>
</PropertyGroup>
<ItemGroup>
<Resource Include="Resources\Image.png" />
</ItemGroup>
Then you can use this Uri from another assembly:
pack://application:,,,/ReferencedAssembly;component/Resources/Image.png
as seen here:
My question:
If you want only <Project Sdk="Microsoft.NET.Sdk">
and don't want <UseWPF>true</UseWPF>
then you have to use the Content
build action, because it does not require WPF, as seen here:
https://docs.microsoft.com/en-us/visualstudio/ide/build-actions
And use it like so:
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Content Include="Resources\Image.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
Then you should be able to use this Uri from another assembly:
pack://application:,,,/Resources/Image.png
as seen here:
But I get the following exception:
Exception thrown: 'System.Resources.MissingManifestResourceException' in System.Private.CoreLib.dll
Exception thrown: 'System.IO.IOException' in PresentationFramework.dll
Exception thrown: 'System.IO.IOException' in PresentationCore.dll
System.Windows.Data Error: 6 : 'TargetDefaultValueConverter' converter failed to convert value 'pack://application:,,,/Resources/Image.png' (type 'String'); fallback value will be used, if available. BindingExpression:Path=BackgroundImage; DataItem='NavigationNode' (HashCode=663215); target element is 'Image' (Name=''); target property is 'Source' (type 'ImageSource') IOException:'System.IO.IOException: Cannot locate resource 'resources/image.png'.
I have cleaned and rebuilt the whole solution.
What am I doing wrong?
Important: I need to use a pack URI in xaml - too many to convert all existing code from xaml to cs!
If you reference a content file that gets copied to the output directory, you could use a path rather than a pack URI. Try this:
image.Source = new BitmapImage(new Uri($@"{System.AppContext.BaseDirectory}\Resources\Image.png",
UriKind.Absolute));
If you still need to use a pack URI for some reason, replace pack://application:,,,
with pack://siteoforigin:,,,
in the path and it should work.
这篇关于在WPF应用程序中使用.NET Standard程序集中的内容文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!