Search code examples
uwpwin2d

UWP/Win2D - application crashes while loading canvas resources


I can't verify this for sure, but it appears as though my application is crashing because it's taking too long for my resources to load.

I believe it's coming from my call to CreateCanvasResourcesAsync

    public void OnCreateResources(CanvasControl sender, CanvasCreateResourcesEventArgs args)
    {
        args.TrackAsyncAction(CreateCanvasResourcesAsync(sender).AsAsyncAction());
    }

    private async Task CreateCanvasResourcesAsync(CanvasControl sender)
    {
        if (_tiles.Keys.Count > 0) return;
        await LoadImageAssets(sender, _tiles);
    }

    private async Task LoadImageAssets(CanvasControl canvas, IDictionary<int, CanvasBitmap> dictionary)
    {
        dictionary.Add((int)TileTypes.Tile1, await CanvasBitmap.LoadAsync(canvas, @"Assets/Tiles/Tile1.png"));
        dictionary.Add((int)TileTypes.Tile2, await CanvasBitmap.LoadAsync(canvas, @"Assets/Tiles/Tile2.png"));
    // etc.
    }

I am loading approximately 200 bitmaps, totalling roughly 4MB in size.

Has anybody else experienced this? If so, did you come up with a workaround?

I have found some articles that offer suggestions at loading needed resources at runtime, but they don't seem to fit with the problem I'm having.


Solution

  • The problem doesn't seem to be Win2D at all. The problem seems to be that the dictionary is blowing up from trying to stuff in too much data. I haven't empirically proven that, but I created two separate dictionaries, putting half the images in one and the other half into the second - and the problem was solved.

    There is likely a better way to manage the dictionary to accommodate the full list, but this is a reasonable workaround for now.