UWP - Load image in class library

喜夏-厌秋 提交于 2019-12-22 08:37:34

问题


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.

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.

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.

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)


回答1:


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


来源:https://stackoverflow.com/questions/33064628/uwp-load-image-in-class-library

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!