uwpinvalidationskiasharpuno-platform

uno platform: in UserControl, how do I invalidate to cause a paint action


In Uno, I have a UserControl with some property setters. When the setter gets called, I need to cause the control to be painted, i.e. invalidated. What method will do that ?

I've tried Invalidate (true), which should work for UWP but is not recognized for Uno. I also tried InvalidateSurface (), also not recognized.

I'm using SkiaSharp for the graphics.

Here's my xaml for the control:

<UserControl
  x:Class="UnoTest.Shared.Controls.ExpandableImage"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:local="using:UnoTest.Shared.Controls"
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  xmlns:skia="using:SkiaSharp.Views.UWP"
  >

  <skia:SKXamlCanvas PaintSurface="OnPaintSurface" />
  
</UserControl>

Here's some snips from the c# code-behind:

using SkiaSharp;
using SkiaSharp.Views.UWP;
using System.IO;
using System.Reflection;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace UnoTest.Shared.Controls
{
  public sealed partial class ExpandableImage : UserControl
  {
    ...
    public string Source
    {
      get { return (string)GetValue(SourceProperty); }
      set 
      { 
        SetValue(SourceProperty, value);
        // invalidate here
      }
    }
    ...
    private void OnPaintSurface(object sender, SKPaintSurfaceEventArgs e)
    {
    }
  }
}

Solution

  • The SKXamlCanvas does not invalidate itself unless its size or the screen's DPI changes.

    To force an invalidation, you'll need to give a name to your canvas:

      <skia:SKXamlCanvas x:Name="canvas" PaintSurface="OnPaintSurface" />
    

    Then invalidate it:

    public string Source
    {
      get => (string)GetValue(SourceProperty);
      set 
      { 
        SetValue(SourceProperty, value);
        canvas.Invalidate();
      }
    }
    

    You can view the source for this here.