UWP - 在类库中加载图像 [英] UWP - Load image in class library

查看:31
本文介绍了UWP - 在类库中加载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个承载主菜单的通用 Windows 应用程序.我想要一个插件架构,其中的菜单项是从类库中添加的.

I have a Universal Windows application that hosts a main menu. I want a plugin architecture, where the menu items are added from class libraries.

但我似乎无法正确加载图像.我无法让 ms-appx:/// 工作,当我尝试将图像添加为嵌入式资源时,它挂起:

But I can't seem to load images correctly. I can't get the ms-appx:/// working, and when I try to add the images as an embedded resource, it hangs:

var assembly = typeof(CookModule).GetTypeInfo().Assembly;
using (var imageStream = assembly.GetManifestResourceStream("My.Namespace.Folder.ImageName-100.png"))
using (var raStream = imageStream.AsRandomAccessStream())
{
    var bitmap = new BitmapImage();
    bitmap.SetSource(raStream); //<- Hangs here

我没有得到任何异常、输出错误或任何东西.它只是挂在那里,应用程序根本不加载页面.

I get no exceptions, errors in the output or anything. It just hangs there, and the app simply doesn't load the page.

我也试过:

var bitmap = new BitmapImage(new Uri("/Folder/ImageName-100.png"));

我缺少类似于 WPF 包 uri 的内容,我可以在其中说明从哪个程序集加载图像.

I'm missing something similar to the WPF pack uri's where I can state which assembly to load the image from.

从类库向页面添加图像资源的正确(和有效)方法是什么?(或者是否有人有 ms-appx 的工作示例,其中图像位于类库中)

What is the correct (and working) way of adding a image resource to a Page from a class libary? (Or does anyone have a working example of ms-appx where the image is in a class library)

推荐答案

我可以重现这个问题.我目前使用的解决方法是将资源流复制到 .NET 内存流.

I can reproduce this issue. Current workaround I used is copying the resource stream to .NET memory stream.

        var assembly = typeof(CookModule).GetTypeInfo().Assembly;

        using (var imageStream = assembly.GetManifestResourceStream("UWP.ClassLibrary.210644575939381015.jpg"))
        using (var memStream = new MemoryStream())
        {
            await imageStream.CopyToAsync(memStream);

            memStream.Position = 0;

            using (var raStream = memStream.AsRandomAccessStream())
            {
                var bitmap = new BitmapImage();
                bitmap.SetSource(raStream);

                display.Source = bitmap;
            }
        }

这篇关于UWP - 在类库中加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆