How to pass string as command parameter from c# markup

paramjit 276 Reputation points
2021-08-03T09:46:23.723+00:00

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.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,299 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Kyle Wang 5,531 Reputation points
    2021-08-04T05:59:22.187+00:00

    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.