Skip to content

.Net Core 3.0 WPF Resource doesn't work in project #2258

@getandplay

Description

@getandplay
  • .NET Core Version: (e.g. 3.0 Preview1, or daily build number, use dotnet --info)

    .Net Core 3.0 netcoreapp3.0

  • Windows version: (winver)

    win10

  • Does the bug reproduce also in WPF for .NET Framework 4.8?: Yes/No

    The bug can reproduce in WPF .Net Core but it works fine in .NET Framework 4.7

  • Is this bug related specifically to tooling in Visual Studio (e.g. XAML Designer, Code editing, etc...)? If yes, please file the issue via the instructions here.

No, I think it is .Net Core Bug

Problem description:

In my solution there are three projects all base on .Net Core 3.0.
- WpfApp1(exe project)
- WpfLibrary1
- Wpf.app.test
And I add an image as Resource in Wps.app.test (I have set the image's build action to Resource), then in WpfLibrary1 I add a ResourceDictionary ,

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <BitmapImage x:Key="Hello" UriSource="pack://application:,,,/Wpf.app.test;component/calendar.png"/>
    
</ResourceDictionary>

In WpfApp1 I use this BitmapImage in my xaml

 <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/WpfLibrary1;component/Test/Dictionary1.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>
    <Grid>
        <!--It works Fine-->
        <!--It will throw Exception, but .Net FrameWork works fine-->
        <Image Width="200" Height="200" Source="{StaticResource Hello}" HorizontalAlignment="Right"/>
    </Grid>

when I run WpfApp1 I got an error:
image
And we decompile the Wpf.app.test.dll found that there is no Image in the Dll. ( I have set image's build action to Resource )
The Wpf.app.test.project code

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <None Remove="calendar.png" />
  </ItemGroup>

  <ItemGroup>
    <Resource Include="calendar.png" />
  </ItemGroup>

</Project>

The Decompile result:
image

Actual behavior:

Throw an exception: System.Windows.Markup.XamlParseException: ''Initialization of 'System.Windows.Media.Imaging.BitmapImage' threw an exception.' Line number '4' and line position '32'.'
Inner Exception
IOException: Cannot locate resource 'calendar.png'.

Expected behaviour:

No Error, No exception

Minimal repro:
Here is the demo for this bug
WpfApp1.zip

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugProduct bug (most likely)

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions