How to get "handle" to a view component

Viewed 35

I have a switch. When I click that switch, I catch that false or true value. But when I catch true I want to disable or do something similar with my department entry, but I don't have "handle" to that component. How to do it in MVVM pattern, how to get a "handle" to that component? Because in "behind code" it's so easy. Sorry for my bad english.

internal class RegisterPageViewModel : INotifyPropertyChanged
    {
        private string _name;
        private string _switchValue;

        public string Name
        {
            get => _name;
            set
            {
                _name = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Name)));
            }
        }
        public string SwitchValue
        {
            get => _switchValue;
            set => _switchValue = value;
        }

        public string MiddleName { get; set; }
        public string Surname { get; set; }
        public string Email { get; set; }
        public string PhoneNumber { get; set; }
        public string DepartmentCode { get; set; }
        public ICommand ImAnEmployeeCommand { get; }

        public RegisterPageViewModel()
        {
            ImAnEmployeeCommand = new Command<bool>(AmAnEmployeeExecute);
        }

        private void AmAnEmployeeExecute(bool switchValue)
        {
            if (switchValue)
            {
                App.Current.MainPage.DisplayAlert("sds", switchValue.ToString(), "dsad");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
1 Answers

You can just use data binding to achieve this.

I created a simple demo and achieved this function.

You can refer to the following code:

1.add a field IsChenked and implemente interface INotifyPropertyChanged for RegisterPageViewModel:

      public  class RegisterPageViewModel: INotifyPropertyChanged
    {
        //add new field here
        bool _isChenked;
        public bool IsChenked
        {
            get
            {
                return _isChenked;
            }
            set
            {
                _isChenked = value;
                // Do any other stuff you want here
                OnPropertyChanged(nameof(IsChenked));
                AmAnEmployeeExecute(IsChenked);
            }
        }

        //other code

        public string MiddleName { get; set; }
        public string Surname { get; set; }
        public string Email { get; set; }
        public string PhoneNumber { get; set; }
        public string DepartmentCode { get; set; }
        public ICommand ImAnEmployeeCommand { get; }

        public RegisterPageViewModel()
        {
            ImAnEmployeeCommand = new Command<bool>(AmAnEmployeeExecute);

            IsChenked = true;
        }

        private void AmAnEmployeeExecute(bool switchValue)
        {
            if (switchValue)
            {
                System.Diagnostics.Debug.WriteLine("switchValue is  true");
            }
            else {
                System.Diagnostics.Debug.WriteLine("switchValue is  false");
            }
        }

        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }

   }

2.And bind field IsChenked in YourPage.xaml

  <ContentPage.BindingContext>
    <formapp619:RegisterPageViewModel></formapp619:RegisterPageViewModel>
</ContentPage.BindingContext>

<StackLayout>

    <Entry  Placeholder="test" IsVisible="{Binding IsChenked}"/>
    <Switch  IsToggled="{Binding IsChenked}"/>

</StackLayout>
Related