绑定 Maui .NET 中继命令到单选按钮

huangapple go评论96阅读模式
英文:

Maui .net bind relay command to radio button

问题

  1. 我正在创建一个带有两个单选按钮的视图,唯一的问题是,CheckedChanged属性无法绑定到带有RelayCommand注释的方法。根据Visual Studio上的错误显示,它只能绑定到属性或值。有没有办法将此属性绑定到我的视图模型上的方法?
  2. 这是单选按钮:
  3. <RadioButton Value="Player1"
  4. Grid.Row="1"
  5. Grid.ColumnSpan="2"
  6. Content="{Binding Name1}"
  7. BorderColor="#FF1111"
  8. BorderWidth="20"
  9. VerticalOptions="Center"
  10. IsChecked= "False"
  11. CheckedChanged= "{Binding ChangeCurrentPlayer2Command}"
  12. />
  13. 而且Relay Command
  14. void ChangeCurrentPlayer2()
  15. {
  16. if (currentPlayer == Player1)
  17. {
  18. currentPlayer = this.Player2;
  19. }
  20. else
  21. {
  22. currentPlayer = this.Player1;
  23. }
  24. }
英文:

I'm making a view with two radio buttons, the only problem is that the CheckedChanged property can't be bound to a method with a RelayCommand annotation. According to the error display on Visual Studio it can only be bound to a property or value.
Is there any way I can bind this property to a method on my View Model?

This is the Radio button:

  1. <RadioButton Value="Player1"
  2. Grid.Row="1"
  3. Grid.ColumnSpan="2"
  4. Content="{Binding Name1}"
  5. BorderColor="#FF1111"
  6. BorderWidth="20"
  7. VerticalOptions="Center"
  8. IsChecked= "False"
  9. CheckedChanged= "{Binding ChangeCurrentPlayer2Command}"
  10. />

And the Relay Command:

  1. void ChangeCurrentPlayer2()
  2. {
  3. if (currentPlayer == Player1)
  4. {
  5. currentPlayer = this.Player2;
  6. }
  7. else
  8. {
  9. currentPlayer = this.Player1;
  10. }
  11. }

答案1

得分: 1

你想要将一个viewmodel方法绑定到radio按钮的checkedChanged事件,可以使用EventToCommandBehavior来实现。

英文:

> What I want is to bind a viewmoedl method to the event checkedChanged for the radio buttons, I know how to do this using a normal Button, by using the RelayCommand annotation.

As Jason said, you can use EventToCommandBehavior by .NET MAUI Community Toolkit to achieve it.

Page.xaml:

  1. <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  2.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  3.              xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
  4.              xmlns:viewmodel="clr-namespace:MauiApp1.ViewModels"
  5.              x:Class="MauiApp1.NewPage2"
  6.              Title="NewPage2">
  7.     <ContentPage.BindingContext>
  8.         <viewmodel:VM/>
  9.     </ContentPage.BindingContext>
  10.     
  11.     <VerticalStackLayout>
  12.         <RadioButton Value="Player1"
  13.                      x:Name="this"
  14.                  Content="{Binding Name1}"
  15.                  VerticalOptions="Center"
  16.                  IsChecked= "False">
  17.             <RadioButton.Behaviors>
  18.                 <toolkit:EventToCommandBehavior
  19.                     EventName="CheckedChanged"
  20.                     Command="{Binding ChangeCurrentPlayer2Command}"
  21.                     CommandParameter="{Binding Source={Reference this}}"/>
  22.             </RadioButton.Behaviors>
  23.         </RadioButton>
  24.         
  25.     </VerticalStackLayout>
  26. </ContentPage>

ViewModel:

  1. namespace MauiApp1.ViewModels
  2. {
  3.     public class VM : INotifyPropertyChanged
  4.     {
  5.        public string name1;
  6.        public string Name1
  7.        {
  8.            get => name1;
  9.            set
  10.            {
  11.                name1 = value;
  12.                OnPropertyChanged(nameof(Name1));
  13.            }
  14.        }
  15.        public event PropertyChangedEventHandler PropertyChanged;
  16.        void OnPropertyChanged(string propertyName)
  17.        {
  18.            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  19.        }
  20.         public ICommand ChangeCurrentPlayer2Command { get; set; }
  21.        public VM()
  22.        {
  23.            Name1 = "Player1";
  24.            ChangeCurrentPlayer2Command = new Command<object>(ChangeCurrentPlayer2);
  25.        }
  26.        public void ChangeCurrentPlayer2(object content)
  27.        {
  28.            var radiobutton = content as RadioButton;
  29.            if (Name1 != radiobutton.Value.ToString())
  30.            {
  31.                Name1 = radiobutton.Value.ToString();
  32.            }
  33.            else
  34.            {
  35.                Name1 = "Player2";
  36.            }
  37.        }
  38.     }
  39. }

When the RadioButton is clicked, Content will be changed if Value is the same as the value Content is bound to.

huangapple
  • 本文由 发表于 2023年4月4日 07:21:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75924393.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定