UWP bound image not updating

Mal Ball 96 Reputation points
2020-10-14T14:44:30.247+00:00

I have an image bound to my viewmodel

<Image x:Name="imgNew" Source="{x:Bind Pvm.NewBitmap}" />

I do an initial load of the image in the viewmodel

        fileStream = await file.OpenAsync(FileAccessMode.Read);
        try
        {
            fileStream.Seek(0);
            NewBitmap.SetSource(fileStream);
        }

    private WriteableBitmap _NewBitmap;
    public WriteableBitmap NewBitmap
    {
        get { return _NewBitmap; }
        set
        {
            if (value != _NewBitmap)
            {
                _NewBitmap = value;
                RaisePropertyChanged("NewBitmap");
            }
        }
    }

This shows the image on my page

I am then doing some rendering using the Lumia Imaging library to change the NewBitmap which I want to be reflected in the page but it doesn't change.

The Property change event fires but the change doesn't show until I move away to do something like resizing the app.

Is there a way for the image to be updated though binding?

Universal Windows Platform (UWP)
0 comments No comments
{count} votes

Accepted answer
  1. Mal Ball 96 Reputation points
    2020-10-18T14:39:23.867+00:00
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Roy Li - MSFT 33,001 Reputation points Microsoft Vendor
    2020-10-15T03:28:35.553+00:00

    Hello,

    Welcome to Microsoft Q&A!

    Currently, Microsoft Q&A supports the products listed over here: supported topics (more to be added later on).

    I noticed that you are using X:Bind markup in your code. The default binding mode of X:Bind is one-time. You could override the default mode of X:Bind to one-way or two-way in your code.

    Like this:

    <Image x:Name="imgNew" Source="{x:Bind NewBitmap, Mode=OneWay}"/>  
    

    For more information about data binding mode, you could go through this document: Data binding in depth

    Thank you.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Mal Ball 96 Reputation points
    2020-10-16T10:54:20.087+00:00

    Hi

    Ok, I have done some changes to the code and listed it below, it works the first time now but if I do a revert then apply another effect NewBitmap is not updated, the values are the same, however, if I then resize the page slightly the image is updated.

    Reset Code
    filterEffects.Clear()
    fileStream.Seek(0);
    NewBitmap.SetSource(fileStream);
    fileStream.Seek(0);
    OriginalBitmap.SetSource(fileStream);
    fileStream.Seek(0);
    Renderer = new WriteableBitmapRenderer
    {
    RenderOptions = RenderOptions.Mixed,
    OutputOption = OutputOption.PreserveAspectRatio,
    WriteableBitmap = OriginalBitmap,
    Source = new RandomAccessStreamImageSource(fileStream)
    };

    Imaging Library Set Effect

    filterEffects.Add(new GrayscaleEffect(Renderer.Source));
    WriteableBitmapRenderer l_render = new WriteableBitmapRenderer();
    l_render.WriteableBitmap = OriginalBitmap;
    l_render.RenderOptions = RenderOptions.Mixed;
    l_render.Source = filterEffects;
    NewBitmap = await l_render.RenderAsync();
    Renderer.Source = l_render.Source;
    await Renderer.RenderAsync();

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.