在 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/en-us/visualstudio/ide/build-actions
并像这样使用它:
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Content Include="Resources\Image.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
那么你应该能够从另一个程序集中使用这个 Uri:
pack://application:,,,/Resources/Image.png
如此处所示:
但我得到以下异常:
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'.
我已经清理并重建了整个解决方案。
我做错了什么?
重要提示:我需要在 xaml 中使用一个包 URI - 太多了,无法将所有现有代码从 xaml 转换为 cs!
如果您引用复制到输出目录的内容文件,您可以使用路径而不是包 URI。试试这个:
image.Source = new BitmapImage(new Uri($@"{System.AppContext.BaseDirectory}\Resources\Image.png",
UriKind.Absolute));
如果出于某种原因您仍然需要使用包 URI,请将路径中的 pack://application:,,,
替换为 pack://siteoforigin:,,,
,它应该可以工作。
我想在 .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/en-us/visualstudio/ide/build-actions
并像这样使用它:
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Content Include="Resources\Image.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
那么你应该能够从另一个程序集中使用这个 Uri:
pack://application:,,,/Resources/Image.png
如此处所示:
但我得到以下异常:
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'.
我已经清理并重建了整个解决方案。
我做错了什么?
重要提示:我需要在 xaml 中使用一个包 URI - 太多了,无法将所有现有代码从 xaml 转换为 cs!
如果您引用复制到输出目录的内容文件,您可以使用路径而不是包 URI。试试这个:
image.Source = new BitmapImage(new Uri($@"{System.AppContext.BaseDirectory}\Resources\Image.png",
UriKind.Absolute));
如果出于某种原因您仍然需要使用包 URI,请将路径中的 pack://application:,,,
替换为 pack://siteoforigin:,,,
,它应该可以工作。