【问题标题】:uno platform: GetManifestResourceNames returns nothinguno 平台:GetManifestResourceNames 不返回任何内容
【发布时间】:2021-01-20 06:04:50
【问题描述】:

对于 Uno 平台,我需要从“内容”资源加载图像。我正在使用GetManifestResourceStream,但是在运行 UWP 项目时它返回 null(我还没有尝试过其他风格)。

为了进一步挖掘,我添加了GetManifestResourceNames,但它也返回了一个空数组。

这是我的代码,位于 UserControl 内(Source 是一个属性):

private void LoadBmpSrc()
{
  if (Source == null)
    return;

  Assembly assembly = GetType().GetTypeInfo().Assembly;
  string[] names = assembly.GetManifestResourceNames();
  using (Stream stream = assembly.GetManifestResourceStream(Source))
  {
    bmpSrc = SKBitmap.Decode(stream);
  }
}

在消费 XAML 文件中,我有这个,在 Grid 内:

  <controls:ExpandableImage 
    Grid.Column="0"
    Source="ms-appx:///Assets/icons/folder_tab.png" 
    />

如果我换入这段代码:

  <Image 
    Grid.Column="0" 
    Source="ms-appx:///Assets/icons/folder_tab.png" 
    />

图像显示。

编辑

这是控件的 XAML:

<UserControl
  x:Class="UnoTest.Shared.Controls.ExpandableImage"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:local="using:UnoTest.Shared.Controls"
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  xmlns:skia="using:SkiaSharp.Views.UWP"
  >

  <skia:SKXamlCanvas x:Name="EICanvas" PaintSurface="OnPaintSurface" />
  
</UserControl>

【问题讨论】:

    标签: .net-assembly embedded-resource uno-platform


    【解决方案1】:

    您的问题基本上是Content 图像没有被视为Resources

    如果您想将图像作为资源包含在内,您需要将该文件的构建操作更改为嵌入式资源。更改此设置后,它将显示在

    string[] names = assembly.GetManifestResourceNames();
    

    方法调用。

    您会注意到图像的名称类似于ApplicationName.Directory.ImageName.png。您需要指定此名称才能使用

    assembly.GetManifestResourceStream(imageName)
    

    如果您想将 Content Image 数据加载到 Stream 中,您可以使用以下代码执行此操作(尚未测试):

    var imageUrl = "ms-appx:///Assets/SplashScreen.png";
    var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri(imageUrl));
            
    using(var stream = await file.OpenStreamForReadAsync())
    {
       //Use your data here
    }
    

    希望这会有所帮助。-

    【讨论】:

    • 我会试试你的建议。那么,您知道 标记为“内容”时如何加载文件吗?我假设答案是您回复的后半部分。
    • ImageSource 属性是 ImageSource (github.com/unoplatform/uno/blob/…),它负责“加载”文件。不同平台的实际实现确实不同。
    • 这种(内容方法)有效,但您必须将 GetFile... 中的结果显式键入为 StorageFile。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-27
    • 2019-04-06
    • 2012-02-15
    • 2020-09-07
    • 2021-11-22
    • 2015-11-06
    • 2013-11-14
    相关资源
    最近更新 更多