简体   繁体   English

UWP - 在类库中加载图像

[英]UWP - Load image in class library

I have a Universal Windows application that hosts a main menu. 我有一个通用Windows应用程序,主机主菜单。 I want a plugin architecture, where the menu items are added from class libraries. 我想要一个插件架构,其中菜单项是从类库中添加的。

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: 我无法让ms-appx:///工作,当我尝试将图像添加为嵌入式资源时,它会挂起:

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. 它只是挂在那里,应用程序根本不加载页面。

I have also tried: 我也尝试过:

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

I'm missing something similar to the WPF pack uri's where I can state which assembly to load the image from. 我遗漏了类似于WPF包uri的东西,我可以说明从哪个程序集加载图像。

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) (或者有没有人有ms-appx的工作示例,其中图像在类库中)

I can reproduce this issue. 我可以重现这个问题。 Current workaround I used is copying the resource stream to .NET memory stream. 我使用的当前解决方法是将资源流复制到.NET内存流。

        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;
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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