我正在嘗試向我的 MainPage 添加一個滑塊和一個畫布。滑塊值將控制繪制形狀的高度。但是,我很難嘗試系結屬性。
我不確定如何系結到資源類。
我的觀點
<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();
}
}
}
我的觀點類
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;
}
}
當我嘗試 如何將變數資料傳遞到 .net MAUI GraphicsView 畫布時,.Net 說 => 沒有為“Grid_Height”找到屬性、BindableProperty 或事件,或者值和屬性之間的型別不匹配。
uj5u.com熱心網友回復:
上一個代碼:
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”的命名格式。有關更多詳細資訊,您可以查看此檔案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/520481.html
