XAML 当我试图将Entry的Text属性绑定到数组中的一个项时,得到“Object reference not set to an instance of an object”

qni6mghb  于 2023-08-01  发布在  其他
关注(0)|答案(1)|浏览(99)

我正在使用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>


每次我从条目中删除绑定时,错误就会消失,应用程序又能正常工作了

ukdjmx9f

ukdjmx9f1#

是的,正如你所说的那样。
你可以这样定义你的变量:

[ObservableProperty]  
    private List<double> test;

字符串
并按如下方式初始化它:

Test = new List<double>();
   Test.Add(101);
   Test.Add(102);
   Test.Add(103);


使用示例:

<Entry Text="{Binding Test[0], Mode=TwoWay}" />

相关问题