我正在使用MVVM Community Toolkit在我的ViewModel中创建ObservableProperties。其中一个属性是double[]数组,它在ViewModel的构造函数中初始化。
下面是视图模型:
public partial class LeñaViewModel : BaseViewModel
{
private readonly ICalculatorService _calculatorService;
[ObservableProperty]
private DespachoLeñaModel _despacho;
[ObservableProperty]
private Cliente _cliente;
[ObservableProperty]
private double _totalDespacho;
[ObservableProperty]
private double[] _test;
static Page Page => Application.Current.MainPage;
public LeñaViewModel(ICalculatorService calculatorService)
{
Title = "Despacho Leña";
_calculatorService = calculatorService;
Despacho = new DespachoLeñaModel();
Cliente = new();
Test = new double[8];
}
}
字符串
下面是我试图绑定条目的Text
属性的页面:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
x:Class="ForestalCasablancaApp.Pages.LeñaPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:controls="clr-namespace:ForestalCasablancaApp.Controls"
xmlns:mct="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
xmlns:viewModel="clr-namespace:ForestalCasablancaApp.ViewModels"
Title="{Binding Title}"
x:DataType="viewModel:LeñaViewModel">
<Grid RowDefinitions="150, *">
<Image
Grid.Row="0"
Aspect="AspectFill"
Source="lena_background.jpg" />
<ScrollView Grid.Row="1">
<Grid RowDefinitions="Auto, *, Auto, Auto">
<controls:ClientInfoCard Grid.Row="0" />
<Border
Grid.Row="1"
HeightRequest="{OnIdiom Phone=312,
Tablet=390}"
Style="{StaticResource PageSectionCard}">
<VerticalStackLayout>
<Label Style="{StaticResource PageSubtitle}" Text="Mediciones" />
<Grid
Grid.Row="1"
Padding="10,0"
ColumnDefinitions="Auto, *, Auto, *"
RowDefinitions="Auto, Auto, Auto, Auto, Auto"
RowSpacing="8">
...
<Label
Grid.Row="1"
Grid.Column="0"
HorizontalOptions="Center"
Text="Altura 1: "
VerticalOptions="Center" />
<Grid Grid.Row="1" Grid.Column="1">
<Border Style="{StaticResource EntryCellBorderStyle}" />
<Entry
Style="{StaticResource NumericEntryStyle}"
Text="{Binding Test[0], FallbackValue='', Mode=TwoWay}" />
</Grid>
...
</Grid>
</VerticalStackLayout>
</Border>
</Grid>
</ScrollView>
</Grid>
</ContentPage>
型
每次我从条目中删除绑定时,错误就会消失,应用程序又能正常工作了
1条答案
按热度按时间ukdjmx9f1#
是的,正如你所说的那样。
你可以这样定义你的变量:
字符串
并按如下方式初始化它:
型
使用示例:
型