使用GraphicView进行绑定时遇到的问题

huangapple go评论143阅读模式
英文:

Problems doing a Binding with a GraphicView

问题

我需要在使用MVVM的GraphicView中使用BindableProperty进行数据绑定,但是它不起作用。我正在使用NET MAUI,并且我正在使用Nuget CommunityToolkit.Mvvm。
我的可绘制类是:

public class DibujarArcoGreenwich : GraphicsView, IDrawable
{
    public float AnguloFin
    {
        get => (float)GetValue(AnguloFinProperty);
        set => SetValue(AnguloFinProperty, value);
    }
    public static readonly BindableProperty AnguloFinProperty =
        BindableProperty.Create(nameof(AnguloFin), typeof(float), typeof(DibujarArcoGreenwich));

    public void Draw(ICanvas canvas, RectF dirtyRect)
    {
        //绘制弧线
        float finalgrados = 360 - AnguloFin;
        canvas.StrokeColor = Color.Red;
        canvas.StrokeSize = 6;
        int xposicion = XPosicionCentro - Radio;
        int yposicion = YPosicionCentro - Radio;
        canvas.DrawArc(10, 10, 90, 90, 0, finalgrados, true, false);
    }
}

我的视图模型是:

public partial class AriesViewModel : ObservableObject, INotifyPropertyChanged
{
    [ObservableProperty]
    float _anguloFin;
    public AriesViewModel()
    {
        AnguloFin = 0;
    }
    [RelayCommand]
    private void CalcularAries()
    {
        AnguloFin = HacerCalculos();
    }
}

我的页面XAML是:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:NautorStar.MisControles"
             xmlns:viewModels="clr-namespace:NautorStar.ViewModels"
             xmlns:drawables="clr-namespace:NautorStar.ClasesDibujo"
             x:Class="NautorStar.AriesPage"
             Title="Aries">
    <ContentPage.BindingContext>
        <viewModels:AriesViewModel />
    </ContentPage.BindingContext>
    <StackLayout>
        <GraphicsView 
            HorizontalOptions="Center"
            VerticalOptions="Center"
            HeightRequest="250"
            WidthRequest="400">
            <GraphicsView.Drawable>
                <drawables:DibujarArcoGreenwich           
                    AnguloFin="{Binding AnguloFin, Mode=TwoWay}"/>
            </GraphicsView.Drawable>
        </GraphicsView>
        <Button Text="Calcular"
                Command="{Binding CalcularAriesCommand}" />
    </StackLayout>
</ContentPage>

但是AnguloFin的值始终为0。如果我将绑定更改为固定值,则可以正常工作。有任何关于为什么绑定不起作用的想法吗?
谢谢

Maria

英文:

I need to do a data binding, using BindableProperty in a GraphicView using MVVM but it does not work. I'm working NET MAUI also I'm using the Nuget CommunityToolkit.Mvvm.
My drawable class is :

public class DibujarArcoGreenwich : GraphicsView, IDrawable
 {
 		public float AnguloFin
        {
            get =&gt; (float)GetValue(AnguloFinProperty);
            set =&gt; SetValue(AnguloFinProperty, value);
        }
        public static readonly BindableProperty AnguloFinProperty =
            BindableProperty.Create(nameof(AnguloFin), typeof(float), typeof(DibujarArcoGreenwich));
            
            public void Draw(ICanvas canvas, RectF dirtyRect)
        {
            //dibujo arco
            float finalgrados = 360 - AnguloFin;
            canvas.StrokeColor = Color.Red;
            canvas.StrokeSize = 6;
            int xposicion = XPosicionCentro - Radio;
            int yposicion = YPosicionCentro - Radio;
            canvas.DrawArc(10, 10,90,90, 0, finalgrados, true, false);
           }

}

My viewmodel is :

public partial class AriesViewModel : ObservableObject, INotifyPropertyChanged
   {
       [ObservableProperty]
       float _anguloFin;
        public AriesViewModel()
       {
           AnguloFin = 0;
       }
       [RelayCommand]
       private void CalcularAries()
       {
       	AnguloFin = HacerCalculos();
       }
    }

My page XAML is :

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt;
&lt;ContentPage xmlns=&quot;http://schemas.microsoft.com/dotnet/2021/maui&quot;
            xmlns:x=&quot;http://schemas.microsoft.com/winfx/2009/xaml&quot;
            xmlns:local=&quot;clr-namespace:NautorStar.MisControles&quot;
            xmlns:viewModels=&quot;clr-namespace:NautorStar.ViewModels&quot;
            xmlns:drawables=&quot;clr-namespace:NautorStar.ClasesDibujo&quot;
            x:Class=&quot;NautorStar.AriesPage&quot;
            Title=&quot;Aries&quot;&gt;
   &lt;ContentPage.BindingContext&gt;
       &lt;viewModels:AriesViewModel /&gt;
   &lt;/ContentPage.BindingContext&gt;
    &lt;StackLayout&gt;
                 &lt;GraphicsView 
                           HorizontalOptions=&quot;Center&quot;
                           VerticalOptions=&quot;Center&quot;
                           HeightRequest=&quot;250&quot;
                           WidthRequest=&quot;400&quot;&gt;
                &lt;GraphicsView.Drawable&gt;
                    &lt;drawables:DibujarArcoGreenwich           
                           AnguloFin=&quot;{Binding AnguloFin, Mode=TwoWay}&quot;/&gt;
                  &lt;/GraphicsView.Drawable&gt;
            &lt;/GraphicsView&gt;
             &lt;Button Text=&quot;Calcular&quot;
                   Command=&quot;{Binding CalcularAriesCommand}&quot; /&gt;
       &lt;/StackLayout&gt;
&lt;/ContentPage&gt;

But always the value of AnguloFin is 0. If I change the binding for a fixed value is OK.
Any idea why the binding doesn't work for me?
Thanks

Maria

答案1

得分: 0

我发现Drawable无法与BindableProperty一起使用。但是你可以通过继承来扩展GraphicsView并向其添加AnguloFin属性。

首先,你需要创建一个继承自GraphicsView的ScoreGraphicsView类,并在其中添加可绑定的AnguloFinProperty属性:

public class ScoreGraphicsView : GraphicsView
{
    public float AnguloFin
    {
        get => (float)GetValue(AnguloFinProperty);
        set => SetValue(AnguloFinProperty, value);
    }

    public static readonly BindableProperty AnguloFinProperty =
        BindableProperty.Create(nameof(AnguloFin), typeof(float), typeof(ScoreGraphicsView), propertyChanged: AnguloFinChanged);

    private static void AnguloFinChanged(BindableObject bindable, object oldValue, object newValue)
    {
        if (bindable is not ScoreGraphicsView { Drawable: DibujarArcoGreenwich drawable } view)
        {
            return;
        }

        drawable.AnguloFin = (float)newValue;
        view.Invalidate();
    }
}

其次,你需要从DibujarArcoGreenwich类中移除BindableProperty,并将AnguloFin属性设置为具有默认getter和setter的普通属性:

public class DibujarArcoGreenwich : IDrawable
{
    public float AnguloFin { get; set; }

    public void Draw(ICanvas canvas, RectF dirtyRect)
    {
        float finalgrados = 360 - AnguloFin;
        canvas.StrokeColor = Colors.Red;
        canvas.StrokeSize = 6;
        canvas.DrawArc(10, 10, 90, 90, 0, finalgrados, true, false);
    }
}

最后,你需要更改页面XAML中的代码,将AnguloFin放入drawables:ScoreGraphicsView中:

<StackLayout>
    <drawables:ScoreGraphicsView
        x:Name="draw"
        AnguloFin="{Binding AnguloFin}"
        HorizontalOptions="Center"
        VerticalOptions="Center"
        HeightRequest="250"
        WidthRequest="400">
        <GraphicsView.Drawable>
            <drawables:DibujarArcoGreenwich />
        </GraphicsView.Drawable>
    </drawables:ScoreGraphicsView>
    <Button Text="Calcular"
            Clicked="Button_Clicked"
            Command="{Binding CalcularAriesCommand}" />
</StackLayout>
英文:

I found that the Drawables cannot be used with BindableProperty. But you can add the AnguloFin property to the GraphicsView by extending it via inheritance.

First, you need to create a ScoreGraphicsView class that inherits from GraphicsView and add the bindable AnguloFinProperty to it:

 public class ScoreGraphicsView : GraphicsView
    {
        public float AnguloFin
        {
            get =&gt; (float)GetValue(AnguloFinProperty);
            set =&gt; SetValue(AnguloFinProperty, value);
        }
        public static readonly BindableProperty AnguloFinProperty =
             BindableProperty.Create(nameof(AnguloFin), typeof(float), typeof(ScoreGraphicsView),  propertyChanged: AnguloFinChanged);

        private static void AnguloFinChanged(BindableObject bindable, object oldValue, object newValue)
        {
            if (bindable is not ScoreGraphicsView { Drawable: DibujarArcoGreenwich drawable } view)
            {
                return;
            }

            drawable.AnguloFin = (float)newValue;
            view.Invalidate();
        }
    }

Second, You need to remove the BindableProperty from the DibujarArcoGreenwich and set the AnguloFin property into a regular property with default getter and setter:

public class DibujarArcoGreenwich :  IDrawable
    {
        public float AnguloFin { get; set; }
        public void Draw(ICanvas canvas, RectF dirtyRect)
        {
         
            float finalgrados = 360 - AnguloFin;
            canvas.StrokeColor = Colors.Red;
            canvas.StrokeSize = 6;
            canvas.DrawArc(10, 10, 90, 90, 0, finalgrados, true, false);
        }

    }

Last, you need to change the code in the page XAML, put the AnguloFin to the drawables:ScoreGraphicsView:

 &lt;StackLayout&gt;
        &lt;drawables:ScoreGraphicsView
                 x:Name=&quot;draw&quot;
                 AnguloFin=&quot;{Binding AnguloFin }&quot;
                 HorizontalOptions=&quot;Center&quot;
                 VerticalOptions=&quot;Center&quot;
                 HeightRequest=&quot;250&quot;
                 WidthRequest=&quot;400&quot;&gt;
            &lt;GraphicsView.Drawable&gt;
                &lt;drawables:DibujarArcoGreenwich        
                           /&gt;
            &lt;/GraphicsView.Drawable&gt;
        &lt;/drawables:ScoreGraphicsView&gt;
        &lt;Button Text=&quot;Calcular&quot;
                Clicked=&quot;Button_Clicked&quot;
                Command=&quot;{Binding CalcularAriesCommand}&quot; /&gt;
    &lt;/StackLayout&gt;

huangapple
  • 本文由 发表于 2023年8月8日 23:59:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76861295.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定