How to reference an Entry with defined x:Name="..." in ViewModel - "... does not exist in current context"

问题描述 投票:0回答:0

在 .NET MAUI 项目中,我有一个视图,其中包含一个条目和一个具有 x:Name="myTextValidationBehavior" 的 EntryBehavior,我想在我的一个 ViewModels 中使用它

<?xml version="1.0" encoding="utf-8" ?>
<ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
             x:Class="MyApp.Pages.Views.MyFoodInputControlView"
             x:Name="this">

    <StackLayout BindingContext="{x:Reference this}">
        <Grid Margin="20, 0, 20, 0">
            ...

            <StackLayout Grid.Row="0" Grid.Column="0" VerticalOptions="Center">
                <Label Text="{Binding NameLabelString}" />
                <Label Text="{Binding IsOptionalLabelString}" FontSize="12" />
            </StackLayout>

            <StackLayout Grid.Row="0" Grid.Column="1" VerticalOptions="Center" >
                <Entry Text="{Binding EntryInput}" Placeholder="{Binding PlaceholderString}" Keyboard="{Binding KeyboardSetting}" Margin="5, 0, 5, 15">
                    <Entry.Behaviors>
                        <toolkit:TextValidationBehavior
                            Flags="ValidateOnValueChanged"
                            x:Name="myTextValidationBehavior"
                            toolkit:MultiValidationBehavior.Error="Entry may not be empty">                            
                        </toolkit:TextValidationBehavior>
                    </Entry.Behaviors>
                </Entry>
            </StackLayout>

        </Grid>
    </StackLayout>

</ContentView>

这个 ContentView 在一个 ContentPage 中使用:

<?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:viewModels="clr-namespace:MyApp.ViewModels"
             xmlns:controls="clr-namespace:MyApp.Pages.Views"
             x:DataType="viewModels:ManageItemsViewModel"
             x:Class="MyApp.Pages.ManageItem"
             Title="My Title">

    <VerticalStackLayout>

        ...

        <StackLayout>
            <controls:MyFoodInputControlView NameLabelString="MyLabel1:"
                                               IsOptionalLabelString="Mandatory"
                                               PlaceholderString="e.g. This Placeholder"
                                               EntryInput="{Binding FoodNameString}" />

            <controls:MyFoodInputControlView NameLabelString="MyLable2:"
                                               IsOptionalLabelString="Optional"
                                               PlaceholderString="z.B. This Placeholder" 
                                               EntryInput="{Binding BrandNameString}" />

            ...

            <StackLayout Margin="20, 50, 15, 0">
                <Grid RowSpacing="10">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>

                    <Button Grid.Row="0" Grid.Column="0" Margin="5"
                            Text="OK"
                            Command="{Binding SaveItemCommand}" />

                    <Button Grid.Row="0" Grid.Column="1" Margin="5"
                            Text="Cancel"
                            Command="{Binding CancelItemCommand}" />
                </Grid>
            </StackLayout>
        </StackLayout>

    </VerticalStackLayout>

</ContentPage>

“SaveItemCommand”、“CancelItemCommand”、“FoodNameString”和“BranchNameString”的数据绑定在 ManageItemsViewModel 中正确执行。

namespace MyApp.ViewModels
{
    public partial class ManageItemsViewModel : ObservableObject
    {
        
        [ObservableProperty]
        private string foodNameString;

        [ObservableProperty]
        private string brandNameString;

        

        [RelayCommand]
        async Task SaveItem()
        {
            try
            {
                if (!myTextValidationBehavior.IsValid)
                {
                    ...
                }

                ...
            }
            catch (Exception e)
            {
                await App.Current.MainPage.DisplayAlert("Error", e.Message, "OK");
            } 
        }

        [RelayCommand]
        async Task Canceltem()
        {
            ...
        }
    }
}

SaveItemCommand-Handler中,我想访问在MyFoodInputControlView中被命名为myTextValidationBehavior的TextValidationBehavior。

但是,我无法在 ManageItemsViewModel 中引用 myTextValidationBehavior。它只是找不到它并说当前上下文中不存在名称“myTextValidationBehavior”

如何参考myTextValidationBehavior

如上所述尝试在 ViewModel 中引用它

.net viewmodel maui xname
© www.soinside.com 2019 - 2024. All rights reserved.