Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Button Command not triggering inside view model

$
0
0

I have a Button in my view and I have a View model bind to that view. My problem is, command not triggering inside the view model.

My view Model.....

**iPayBtnCommand ** is the command ....

namespace CustApp.CusApp.Dushan.ViewModel
{
    public class LifeDemandVM : INotifyPropertyChanged
    {     
        public LifeDemandVM() {

            paymentEnabled = false;
            advancedPayment = null;

            MyCommand = new Command((object sender) =>
            {
                selectedList = new ObservableCollection<LifeDemandData>();
                selectedList.Clear();
                double summ = 0;

                if (((CheckBox)sender).IsChecked)
                {

                }
                else
                {


                }

                foreach (LifeDemandData ss in selectedList)
                {

                    summ = Convert.ToDouble(ss.PREMIUM) + Convert.ToDouble(ss.LATEFEE) + summ;

                }

                if (summ > 0)
                {
                    canPay = true;
                    sumValue = summ.ToString("N", CultureInfo.InvariantCulture);
                    demandSum = summ;
                }
                else
                {
                    canPay = false;
                    sumValue = "0.00";
                }

                if (selectedList.Count > 1)
                {
                    txtDemand = "Demands Rs. ";
                }
                else
                {
                    txtDemand = "Demand Rs. ";
                }


            });


        }


        public async Task getLifeDemands(string polNo)
        {

            IsBusy = true;

            string demandRequest = await _apiServices.getLifeDemandList(polNo);
            var varModel = JsonConvert.DeserializeObject<List<LifeDemandData>>(demandRequest);
            LifeDemandList = new ObservableCollection<LifeDemandData>();
            LifeDemandList.Clear();

            foreach (LifeDemandData ss in varModel)
            {
                LifeDemandData lifeData = new LifeDemandData();
                lifeData.DEMAND = ss.DEMAND;
                lifeData.LATEFEE = ss.LATEFEE;
                lifeData.PREMIUM = ss.PREMIUM;
                lifeData.ROWCHECK = ss.ROWCHECK;
                lifeData.CheckedCommand = MyCommand;

                LifeDemandList.Add(lifeData);
            }

            LifeDemandListSource = LifeDemandList;

            IsBusy = false;

        }


        public void setSwitchToggle(bool value)
        {
            if (value)
            {
                paymentEnabled = true;
            }
            else
            {
                paymentEnabled = false;
                advancedPayment = null;
            }
        }



        public ICommand iPayBtnCommand
        {
            get
            {
                return new Command(async () =>
                {

                    await Application.Current.MainPage.Navigation.PushAsync(new ContactUs());                 

                });
            }
        }

        private async void PromptCommand(string action)
        {
            double totalSum = 0;

            if (advancedPayment == null || advancedPayment.Equals("") || advancedPayment.Equals("."))
            {
                totalSum = demandSum;
            }
            else
            {
                totalSum = demandSum + Double.Parse(advancedPayment);
            }


            if (action.Equals("Frimi"))
            {
                await Application.Current.MainPage.Navigation.PushAsync(new PayViaFrimi_Life(totalSum));
            }
            else if (action.Equals("mCash"))
            {
                await Application.Current.MainPage.Navigation.PushAsync(new PayViaMcash_Life(totalSum));
            }
            else
            {
            }
        }


        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

    }
}

View part.......

                    <StackLayout Margin="0,10,0,5" HorizontalOptions="CenterAndExpand">

                        <Button Command="iPayBtnCommand" Style="{StaticResource homeBtn}" Text="Pay now"/>

                    </StackLayout>

CS class....

namespace CustApp.CusApp.Dushan.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class SelectLifeDemands : ContentPage
    {

        private LifeDemandVM vm;

        public SelectLifeDemands()
        {
            InitializeComponent();
        }

        public SelectLifeDemands(string polNo)
        {
            InitializeComponent();
            Title = "Life Demand";

            BindingContext = new LifeDemandVM();
            vm = BindingContext as LifeDemandVM;

            Task.Run(async () =>
            {

                await vm.getLifeDemands(polNo);

            });

        }

    }
}

Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>