0

我正在尝试从我的 Xamarin.Forms 应用程序中的 ViewModel 更改菜单选定项。我该如何改变呢?

我已将 ListView SelectedItem 属性绑定到 ViewModel 中的字段,模式为“TwoWay”。我还使用了 BeginInvokeOnMainThread 和 ContinueWith。为了绑定事件,我创建了一个行为并将命令绑​​定到事件。所有的方式都没有改变选定的项目。

<ListView
            x:Name="ListViewMenu"
            HasUnevenRows="True"
            ItemsSource="{Binding menuItems}"
            SelectedItem="{Binding SelectedItem, Mode=TwoWay}">
<ListView.Behaviors>
                <behaviors:EventToCommandBehavior
                    Command="{Binding command}"
                    Converter="{StaticResource SelectedItemConverter}"
                    EventName="ItemSelected" />
            </ListView.Behaviors>
private HomeMenuItem selectedItem { get; set; }
        public HomeMenuItem SelectedItem
        {
            get { return selectedItem; }
            set
            {
                selectedItem = value;
                this.OnPropertyChanged();
            }
        }
command = new AsyncRelayCommand((sender) => this.ItemSelected(sender).ContinueWith((arg) =>
            {
                HomeMenuItem menuItem = sender as HomeMenuItem;

                if(menuItem.Id != SelectedItem.Id)
                    Device.BeginInvokeOnMainThread(() =>
                    {
                        SelectedItem = menuItems.Where(s => s.Id.Equals(menuItem.Id)).FirstOrDefault();
                    });
            }));

我希望将选定的项目从 Id 2 更改为 Id 0,但这始终保持在 Id 2 上,即使 SelectedItem 变量更改为 Id 0。我的意思是视觉表示不会改变。

4

1 回答 1

0

我写了一个简单的演示来更新菜单选择项,您可以参考它。当我单击按钮时,listview selectedItem 将返回到第一个无论我单击哪个

在此处输入图像描述

有 MainPage.xml

   <StackLayout>
    <Button Command="{Binding UpdateCommand}"></Button>
    <ListView
        x:Name="ListViewMenu"
        HasUnevenRows="True"
        ItemsSource="{Binding menuItems}"
        SelectedItem="{Binding SelectedItem, Mode=TwoWay}">

        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <StackLayout>
                        <Label Text="{Binding Name}" />
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>   
    </ListView>
</StackLayout>

MainPage.xml.cs

公共部分类 MainPage : ContentPage {

    public MainPageModelView model { get; set; }
    public MainPage()
    {
        InitializeComponent();



        model = new MainPageModelView();
        BindingContext = model;
    }


}

MainPageModelView.cs

public class MainPageModelView: INotifyPropertyChanged
{
    public ObservableCollection<HomeMenuItem> menuItems { set; get; }
    public event PropertyChangedEventHandler PropertyChanged;

    public MainPageModelView()
    {
        menuItems = new ObservableCollection<HomeMenuItem>();
        menuItems.Add(new HomeMenuItem() { Name = "Mr. Mono1", Id = 1 });
        menuItems.Add(new HomeMenuItem() { Name = "Mr. Mono2", Id = 2 });
        menuItems.Add(new HomeMenuItem() { Name = "Mr. Mono3", Id = 3 });
        menuItems.Add(new HomeMenuItem() { Name = "Mr. Mono4", Id = 4 });
    }

    private HomeMenuItem selectedItem { get; set; }
    public HomeMenuItem SelectedItem
    {
        get { return selectedItem; }
        set
        {
            selectedItem = value;
            this.OnPropertyChanged();
        }
    }


    Command _updateCommand;

    public Command UpdateCommand
    {
        get
        {
            return _updateCommand ?? (_updateCommand = new Command(ExecuteSaveCommand));
        }
    }
    void ExecuteSaveCommand()
    {
        Device.BeginInvokeOnMainThread(() =>
        {
               SelectedItem = menuItems[0];
        });
        //SelectedItem = menuItems.Where(s => s.Id.Equals(menuItem.Id)).FirstOrDefault();

    }
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this,
        new PropertyChangedEventArgs(propertyName));
    }
}
于 2019-07-02T14:48:11.057 回答