简体   繁体   English

.NET Maui MVVM Picker 绑定 SelectedIndexChanged 事件

[英].NET Maui MVVM Picker Binding SelectedIndexChanged Event

I am developing a Windows desktop app with .NET Maui using MVVM.我正在使用 MVVM 开发带有 .NET Maui 的 Windows 桌面应用程序。

I have 2 Pickers with ImageSource and SelectedIndex bound to properties in a viewmodel.我有 2 个选择ImageSource Pickers SelectedIndex绑定到视图模型中的属性。 When an item is selected from the first Picker the items in the other need to be changed.当从第一个Picker中选择一个项目时,另一个中的项目需要更改。 I would like to bind the SelectedIndexChanged event to a method in the viewmodel to accomplish this.我想将SelectedIndexChanged事件绑定到视图模型中的方法以完成此操作。

The XAML for the event in the picker Picker looks like this:选择器Picker中事件的 XAML 如下所示:

<Picker SelectedIndexChanged="{Binding OnSelectedIndexChanged}" />

The method in the viewmodel looks like this:视图模型中的方法如下所示:

public void OnSelectedIndexChanged(object sender, EventArgs e)
{
    // do stuff
}

But I get the following error when running the program:但是在运行程序时出现以下错误:

XFC0009 No property, BindableProperty, or event found for "SelectedIndexChanged", or mismatching type between value and property. XFC0009 找不到“SelectedIndexChanged”的属性、BindableProperty 或事件,或者值和属性之间的类型不匹配。 MauiApp1 MauiApp1

My temporary solution is to call the viewmodel method from the code behind when the event fires.我的临时解决方案是在事件触发时从后面的代码中调用 viewmodel 方法。 The code behind looks like this:后面的代码如下所示:

private void Picker_SelectedIndexChanged(object sender, EventArgs e)
{
    (BindingContext as MainViewModel).OnSelectedIndexChanged(sender, e);
}

I would like to keep the code behind as dumb as possible.我想让代码尽可能地愚蠢。 Is there a way to handle the SelectedIndexChanged event by binding directly to a method in the viewmodel?有没有办法通过直接绑定到视图模型中的方法来处理SelectedIndexChanged事件?

Update Trying to implement partial void On<PropertyName>Changed()更新尝试实现partial void On<PropertyName>Changed()

My viewmodel:我的视图模型:

public partial class MainViewModel : ObservableObject
{
    [ObservableProperty]
    private ObservableCollection<ProductGroupRoot> itemSourceProductGroups = new();

    [ObservableProperty]
    private int selectedProductGroup = -1;

    [ObservableProperty]
    private ObservableCollection<ProductRoot> itemSourceProducts = new();

    [ObservableProperty]
    private int selectedProduct = -1;
    
    // other properties

    partial void OnSelectedProductGroupChanged(int value)
    {
        // TODO: change values in ItemSourceProducts
    }        
}

The autogenerated code should have created a definition for the partial method, but I get the error:自动生成的代码应该已经为部分方法创建了一个定义,但我得到了错误:

CS0759 No defining declaration found for implementing declaration of partial method 'MainViewModel.OnSelectedProductGroupChanged(int)' CS0759 未找到用于实现分部方法“MainViewModel.OnSelectedProductGroupChanged(int)”声明的定义声明

I am using CommunityToolkit.Mvvm v7.1.2 (latest stable).我正在使用 CommunityToolkit.Mvvm v7.1.2(最新稳定版)。

Update 2 Posting the working code.更新 2发布工作代码。

My csproj file:我的 csproj 文件:

<Project>
    <ItemGroup>
        <PackageReference Include="CommunityToolkit.Mvvm" Version="8.0.0-preview4" />
    </ItemGroup>
</Project>

My Picker:我的选择器:

<Picker ItemsSource="{Binding ProductGroups, Mode=TwoWay}" 
        SelectedIndex="{Binding ProductGroupsIndex, Mode=TwoWay}" />

My ViewModel:我的视图模型:

[ObservableProperty]
private ObservableCollection<ProductGroupRoot> productGroups = new();

[ObservableProperty]
private int productGroupsIndex = -1;

partial void OnProductGroupsIndexChanged(int value) {}

You can simply change the second picker items when the property bound to SelectedIndex changes.当绑定到SelectedIndex的属性发生变化时,您可以简单地更改第二个选择器项目。

Since you're using MVVM CommunityToolkit, to execute some code after a property changed, you have to implement a partial method which is defined in the autogenerated class. This method is called On<YourPropertyName>Changed() .由于您使用的是 MVVM CommunityToolkit,要在属性更改后执行一些代码,您必须实现在自动生成的 class 中定义的部分方法。此方法称为On<YourPropertyName>Changed()

To be noticed that this feature is available only since version 8.0.0.请注意,此功能仅在 8.0.0 版本后可用。

Check this answer: https://stackoverflow.com/a/72499605/7977288检查这个答案: https://stackoverflow.com/a/72499605/7977288

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM