question

paramjit-0569 avatar image
0 Votes"
paramjit-0569 asked paramjit-0569 commented

How to pass string as command parameter from c# markup

I am using C# markup for the UI development. I declared two buttons to enable user to select Language. The markup is as follows:

  FuncConverter<string, Color> stringToColorConverter = new FuncConverter<string, Color>(language => language == "English" ? Color.Blue : Color.Gray);
  new Button{Text="English"}.Bind(Button.BackgroundColorProperty,nameof(vm.SelectedLanguage),BindingMode.TwoWay,converter:stringToColorConverter).BindCommand(nameof(vm.ChangeLanguageCommand),"English"),
                           
 new Button{Text="हिंदी"}.Bind(Button.BackgroundColorProperty,nameof(vm.SelectedLanguage),BindingMode.TwoWay,converter:stringToColorConverter).BindCommand(nameof(vm.ChangeLanguageCommand), parameterPath:("हिंदी"))

The code in the ViewModel:

  public ReactiveCommand<string,string> ChangeLanguageCommand { get; set; }
    
 ChangeLanguageCommand = ReactiveCommand.CreateFromTask<string,string>(async(selectedLanguage)=>await ChangeLanguage(selectedLanguage));
    
 public async Task<string> ChangeLanguage(string selectedLanguage)
         {
             CultureInfo language = CultureInfo.GetCultures(CultureTypes.NeutralCultures).ToList().First(element => element.EnglishName.Contains(selectedLanguage));
             Thread.CurrentThread.CurrentUICulture = language;
             AppResources.Culture = language;
             return language.EnglishName;
         }

The button for English language does not invoke command. Whereas the button for Hindi language invoke command but the passed parameter is null. I am new to C# markup and there are very few resources on C# markup. Thanks in advance for any help.




dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

KyleWang-MSFT avatar image
0 Votes"
KyleWang-MSFT answered paramjit-0569 commented

Hi paramjit-0569,

Welcome to our Microsoft Q&A platform!

According to document: Xamarin Community Toolkit C# Markup, we can bind the command as followed.

 new Button{Text="English"}.Row (0) .Column (0)
     .Bind(BackgroundColorProperty,nameof(vm.SelectedLanguage),BindingMode.TwoWay,converter:stringToColorConverter)
     .BindCommand (nameof(vm.TapCommand), vm, nameof(vm.SelectedLanguage)),

Here is the full code demo you can refer to.
MainPage.xaml.cs

 public partial class MainPage : ContentPage
 {
     public MainPage()
     {
         InitializeComponent();
    
         MainPageViewModel vm = new MainPageViewModel();
         this.BindingContext = vm;
    
         FuncConverter<string, Color> stringToColorConverter = new FuncConverter<string, Color>(language => language == "English" ? Color.Blue : Color.Gray);
    
         Content = new Grid
         {
             Children =
             {
                 new Button{Text="English"}.Row (0) .Column (0)
                     .Bind(BackgroundColorProperty,nameof(vm.SelectedLanguage),BindingMode.TwoWay,converter:stringToColorConverter)
                     .BindCommand (nameof(vm.TapCommand), vm, nameof(vm.SelectedLanguage)),
    
                 new Button{Text="हिंदी"} .Row (1) .Column (0)
                     .Bind(Button.BackgroundColorProperty,nameof(vm.SelectedLanguage),BindingMode.TwoWay,converter:stringToColorConverter)
                     .BindCommand(nameof(vm.TapCommand), vm, nameof(vm.SelectedLanguage))
             }
         };
     }
 }

MainPageViewModel.cs

 class MainPageViewModel : INotifyPropertyChanged
 {
     public ICommand TapCommand { get; private set; }
     string selectedLanguage;
     public string SelectedLanguage
     {
         get => selectedLanguage;
         set
         {
             if (selectedLanguage == value || value == null)
                 return;
             selectedLanguage = value;
             OnPropertyChanged("SelectedLanguage");
         }
     }
    
     public MainPageViewModel()
     {
         SelectedLanguage = "English";
         TapCommand = new Command<object>(Tap);
     }
    
     void Tap(object obj)
     {
         Debug.WriteLine("Tap");
         // get parameter
         Debug.WriteLine(obj.ToString());
         SelectedLanguage = SelectedLanguage == "English" ? "हिंदी" : "English";
     }
    
     public event PropertyChangedEventHandler PropertyChanged;
     protected void OnPropertyChanged(string propertyName)
     {
         var handler = PropertyChanged;
         if (handler != null)
             handler(this, new PropertyChangedEventArgs(propertyName));
     }
 }

Regards,
Kyle


If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi Kyle Wang,
Thank you for the support. I think I don't able to convey the problem properly. Please note these points

  1. I want to send string as command parameter. "English" when user click English button and "Hindi" when user click Hindi button.

  2. In the viewmodel I will assign the command parameter to the SelectedLanguage property. SelectedLanguage property is there only to change background colors of the both buttons

  3. I will also use Command parameter to change the UI language of the App. Both the buttons are bind to the same command. The difference is just in the parameter they send.

Regards
Paramjit Singh

.





0 Votes 0 ·

@paramjit-0569 If you want to pass the corresponding string for each button, you can define two string properties in vm as follows.

 public string Engstring { get; set; } = "English";
 public string Hinstring { get; set; } = "हिंदी";

Then bind them like:

 .BindCommand (nameof(vm.TapCommand), vm, nameof(vm.Engstring)),
 // ...
 .BindCommand(nameof(vm.TapCommand), vm, nameof(vm.Hinstring))

And here is the method bound to command

 void Tap(object obj)
 {
     SelectedLanguage = obj.ToString();
 }
0 Votes 0 ·

I able to send string as command parameter after binding to string properties of ViewModel. I was trying to send string directly like I used to do with XAML. Here is some difference between C# markup and XAML.

Thank you

0 Votes 0 ·