XAML 在.NET MAUI中从视图绑定到GraphicsView属性

pw9qyyiw  于 2022-12-07  发布在  .NET
关注(0)|答案(1)|浏览(320)

我正在尝试添加一个滑块和一个画布到我的主页。滑块值将控制绘制形状的高度。但是,我有一个非常困难的时间试图绑定属性。
我不确定如何绑定到资源类。

我的视图

<ContentPage.BindingContext>
    <viewmodel:MainViewModel />
</ContentPage.BindingContext>

<ContentPage.Resources>
    <charts:CustomChart x:Key="drawable"></charts:CustomChart>
</ContentPage.Resources>

<ScrollView>
    <VerticalStackLayout>
        <HorizontalStackLayout
        Spacing="25"
            Padding="30,0"
            VerticalOptions="Center">

            <Slider 
                x:Name="Sldr"
                Minimum="0.3"
                Maximum="1.0"
                Value="{Binding Hayt}"
                WidthRequest="200"
                />

        </HorizontalStackLayout>

        <GraphicsView>
            <GraphicsView.Drawable>
                <charts:CustomChart Grid_Height="{Binding Hayt}" />
            </GraphicsView.Drawable>
        </GraphicsView>

    </VerticalStackLayout>
</ScrollView>

我的视图模型

internal class MainViewModel : BaseViewModel
{

    double hayt;
    public double Hayt
    {
        get { return hayt; }
        set
        {
            if (hayt != value)
                hayt = value;
                OnPropertyChanged();
        }
    }
}

我的Graphview类

internal class CustomChart : GraphicsView, IDrawable
{
    // Screen Parameters
    readonly float ScreenWidth = (float)DeviceDisplay.MainDisplayInfo.Width;
    readonly float ScreenHeight = (float)DeviceDisplay.MainDisplayInfo.Height;
    readonly float Density = (float)DeviceDisplay.MainDisplayInfo.Density;

    public double Grid_Height
    {
        get => (double)GetValue(Grid_Height_Adjuster);
        set => SetValue(Grid_Height_Adjuster, value);
    }

    public static readonly BindableProperty Grid_Height_Adjuster = BindableProperty.Create(nameof(Grid_Height),typeof(double),typeof(CustomChart),0.7);

    public void Draw(ICanvas canvas, RectF dirtyRect)
    {

        float Y_Top   = dirtyRect.Top;
        float Y_Bot   = dirtyRect.Bottom / Density * (float)Grid_Height;
        float X_Right = dirtyRect.Right / Density;
        float X_Left  = dirtyRect.Left;

    }
}

当我尝试时,How to pass variable data into .net MAUI GraphicsView canvas,.Net说=〉没有为“Grid_Height”找到属性、BindableProperty或事件,或者值和属性之间的类型不匹配。

qlzsbp2j

qlzsbp2j1#

上一个代码:

public double Grid_Height
    {
        get => (double)GetValue(Grid_Height_Adjuster);
        set => SetValue(Grid_Height_Adjuster, value);
    }

    public static readonly BindableProperty Grid_Height_Adjuster = BindableProperty.Create
          (nameof(Grid_Height),typeof(double),typeof(CustomChart),0.7);

更改为:

public double Grid_Height
    {
        get => (double)GetValue(Grid_HeightProperty);
        set => SetValue(Grid_HeightProperty, value);
    }

    public static readonly BindableProperty Grid_HeightProperty = BindableProperty.Create
          (nameof(Grid_Height),typeof(double),typeof(CustomChart),0.7);

创建可绑定的属性时,需要保持“PropertyName+ Property“的命名格式,详细内容可以查看此文档。

相关问题