【问题标题】:WPF - Uri to content imagesWPF - 内容图像的 Uri
【发布时间】:2014-10-23 12:00:49
【问题描述】:

我花了 1 个小时浏览互联网并试图找到一种方法如何将 Uri 加载到内容图像并创建 BitmapImage。

这就是我所拥有的:

{
    Uri x = new Uri("/Images/appbar.chevron.up.png", UriKind.Relative);
    BitmapImage bi = new BitmapImage(x);
    return x;
}

解决方案资源管理器中的图像就在项目下:

并且每个图像都有构建动作作为内容

我在创建 BitmapImage 时收到 The given System.Uri cannot be converted into a Windows.Foundation.Uri. Please see http://go.microsoft.com/fwlink/?LinkID=215849 for details.,但调试器显示 Uri 已创建:

请告诉我,如何加载选定的图像?

【问题讨论】:

  • 您是否搜索过异常?就是这样:stackoverflow.com/questions/7569720/…
  • 是的,我用ms-appx: 尝试了选项。它也不起作用。
  • 不起作用永远不会解释任何事情。解释会发生什么。有什么例外吗?错误?
  • 您是否使用位图作为控件的内容?如果是这样,您可以在 Xaml 中设置它吗?

标签: c# image windows-runtime


【解决方案1】:

如前所述,要从应用包加载图像,您将使用 ms-appx 协议。请参阅 MSDN 上的 URI schemes 文档

Uri x = new Uri("ms-appx:///Images/appbar.chevron.up.png");
BitmapImage bi = new BitmapImage(x);

然后你会想要返回 BitmapImage 而不是 URI 给你的调用者:

return bi; // not: return x;

或将其设置在图像控件上。 BitmapImage 只是数据,不会单独显示。

img.SetSource(bi);

在 Xaml 中创建 img 的位置:

根据您尝试在应用栏按钮中设置的图像名称进行猜测。为此,您可以使用 Segoe UI Symbol 字体而不是加载图像:

<AppBarButton>
    <AppBarButton.Icon>
        <FontIcon FontFamily="Segoe UI Symbol" Glyph="&#57361;"/>
    </AppBarButton.Icon>
</AppBarButton>
<AppBarButton>
    <AppBarButton.Icon>
        <FontIcon FontFamily="Segoe UI Symbol" Glyph="&#57360;"/>
    </AppBarButton.Icon>
</AppBarButton>

或者如果你想将 AppBarButtons 设置为 xaml 中的图像:

<AppBarButton>
    <AppBarButton.Icon>
        <BitmapIcon UriSource="Images/appbar.chevron.up.png"/>
    </AppBarButton.Icon>
</AppBarButton>
<AppBarButton>
    <AppBarButton.Icon>
        <BitmapIcon UriSource="Images/appbar.chevron.down.png"/>
    </AppBarButton.Icon>
</AppBarButton>

【讨论】:

    【解决方案2】:

    我不确切知道内容和资源在 WinRT 中是如何工作的,但在 WPF 中,必须将“内容”项复制到输出目录。它们被添加到清单中,但文件本身并未嵌入。

    如果要嵌入文件,请使用“资源”的构建操作(WinRT 甚至可能不支持“内容”——我从未使用过它)。看来您还需要使用ms-appx: 方案创建一个Absolute URI。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-26
      • 2013-07-31
      • 2016-08-07
      • 1970-01-01
      相关资源
      最近更新 更多