uno 平台:在 UserControl 中,如何使绘制操作无效

问题描述

在 Uno 中,我有一个带有一些属性设置器的 UserControl。当 setter 被调用时,我需要使控件被绘制,即无效。什么方法可以做到这一点?

我试过 Invalidate (true),它应该适用于 UWP,但无法被 Uno 识别。我也试过InvalidateSurface (),也无法识别。

我使用 SkiaSharp 制作图形。

这是我的控件 xaml:

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

以下是 C# 代码隐藏的一些片段:

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)
    {
    }
  }
}

解决方法

除非其大小或屏幕的 DPI 发生变化,否则 SKXamlCanvas 不会使自身失效。

要强制失效,您需要为画布命名:

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

然后使其无效:

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

您可以查看 this here 的来源。