XAML .Net MAUI GraphicsView未更新

1aaf6o9v  于 2023-09-28  发布在  .NET
关注(0)|答案(1)|浏览(144)

有人能解释一下我做错了什么吗?我只在android上测试过。我用GraphicsView图表创建了一个控件,它具有progress属性。
当页面第一次加载时,控件绘制为空进度,当我导航到另一个页面并返回时,进度正常绘制。我想强制Drawable在进度更改时更新,但它无法正常工作。
我的控制:

public partial class SunProgress : ContentView
{
    // other properies

    public static readonly BindableProperty ProgressProperty = BindableProperty.Create(nameof(Progress), typeof(float), typeof(SunProgress), propertyChanged: ProgressChanged);
    public float Progress
    {
        get => (float)GetValue(ProgressProperty);
        set => SetValue(ProgressProperty, value);
    }

    public SunProgress()
    {
        InitializeComponent();
    }

    private static void ProgressChanged(BindableObject bindable, object oldValue, object newValue)
    {
        var control = bindable as SunProgress;
        control.Chart.Progress = (float)newValue;
    }
}

在我的控制中,我有GraphicView:

<ContentView>
...
    <GraphicsView Grid.Row="0"
                  x:Name="ChartView"
                  HorizontalOptions="Center"
                  HeightRequest="150"
                  WidthRequest="150">
        <GraphicsView.Drawable>
            <controls:ProgressChart x:Name="Chart" />
        </GraphicsView.Drawable>
    </GraphicsView>
...
</ContentView>

我的进度表:

public class ProgressChart : GraphicsView, IDrawable
{
    private float _progress;

    public float Progress
    {
        get => _progress;
        set
        {
            _progress = value;
            Invalidate(); // this call should force Drawable to redraw itself? 
        }
    }

    public Color Color { get; set; } = Colors.Orange;

    public void Draw(ICanvas canvas, RectF dirtyRect)
    {
        // my drawing
    }
}

我还尝试从BindableObject继承并将SunProgress Progress绑定到ProgressChart Progress,但效果完全相同。

hfwmuf9z

hfwmuf9z1#

您组合了两个通过Drawable属性连接的不同对象。此外,您还将Drawable/GraphicsView Package 在另一个GraphicsView(xaml)中。如果我可以推测,当我查看源代码时,Invalidate为每个Invalidate调用处理程序,并且事件不知何故混淆了。也许不是你在寻找的答案。我的建议是创建一个干净的IDrawable类,并使用ContentView中的Invalidate()

public class ProgressChart : IDrawable
{
    private float _progress;

    public float Progress
    {
        get => _progress;
        set
        {
            _progress = value;
        }
    }

    public Color Color { get; set; } = Colors.Orange;

    public void Draw(ICanvas canvas, RectF dirtyRect)
    {
        // my drawing
    }
}
public partial class SunProgress : ContentView
{
    // other properies

    public static readonly BindableProperty ProgressProperty = BindableProperty.Create(nameof(Progress), typeof(float), typeof(SunProgress), propertyChanged: ProgressChanged);
    public float Progress
    {
        get => (float)GetValue(ProgressProperty);
        set => SetValue(ProgressProperty, value);
    }

    public SunProgress()
    {
        InitializeComponent();
    }

    private static void ProgressChanged(BindableObject bindable, object oldValue, object newValue)
    {
        var control = bindable as SunProgress;
        control.Chart.Progress = (float)newValue;
        control.ChartView.Invalidate();
    }
}

相关问题