简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM