Ginormous Ide
Ginormous Ide

Reputation: 33

WinUI 3: Image content doesn't show after publish

I created an unpackaged WinUI 3 app based on this article: https://learn.microsoft.com/en-us/windows/apps/winui/winui3/create-your-first-winui3-app#unpackaged-create-a-new-project-for-an-unpackaged-c-or-c-winui-3-desktop-app.

The problem is that my window's XAML contains <Image Source="ms-appx:///Assets/Logo.png" />, and that Logo.png doesn't show in the window after publishing until I copy the image file to the Assets directory in the publish folder. But I don't want to keep images as separate files. I'd like them to be embedded in my build and to be accessed from XAML.

Whether it is realizable?

Upvotes: 0

Views: 619

Answers (1)

Ginormous Ide
Ginormous Ide

Reputation: 33

Based on Nick's link, I came up with the following solution:

class Loader: BitmapSource {
  static readonly Assembly assembly = typeof(Loader).GetTypeInfo().Assembly;
  public string Name {
    set {
      SetSource(assembly.GetManifestResourceStream(value).AsRandomAccessStream());
    }
  }
}
<Grid>
  <Grid.Resources>
    <local:Loader x:Key="Image1" Name="App1.Assets.Image.png" />
  </Grid.Resources>
  <Image Source="{StaticResource Image1}" />
</Grid>

The code above displays Assets\Image.png where App1 is the root namespace.

Upvotes: 0

Related Questions