Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UWP - Load image in class library

Tags:

c#

xaml

uwp

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)

like image 884
Troels Larsen Avatar asked Oct 11 '15 11:10

Troels Larsen


1 Answers

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;
            }
        }
like image 158
Jeffrey Chen Avatar answered Nov 05 '22 20:11

Jeffrey Chen