Home > OS >  How to load this viewmodel method to onClick event
How to load this viewmodel method to onClick event

Time:11-28

I am trying to call this GetProductStatus() method on a page button click event, but it's loading before the button click. Means when the ViewModel is loading, this is also load automatically.

I would like to declared this VM method "GetProductStatus()" to be called only when a button click event occurs.

ViewModel method:

private async void GetProductStatus()
{
    try
    {                   
        IsBusy = true;                         
        var status = await ProductStatusService.GetProductStatus(new ProductStatus()
        {
            StoreCode = s_code,
            StartTime = StartDateValue.AddMinutes(time1),
            EndTime = StartDateValue.AddMinutes(time2)
        });
        IsBusy = false;

        if (status != null)
        {           
                //Process happens                           
        }
        else
        {
            //Array is Null         
        }
        ProductStatus = status;
    }
    catch (Exception)
    {
        ProductStatus = null;
    }
}

Here, the method is declared.

public ProductViewModel(INavigation nav, Store store)
{
    _Nav = nav;
    
    GetProductStatus();
}

Here, the clicked event.

private async void ProductTypeButton_Clicked(object sender, EventArgs e)
{   
    await Navigation.PushAsync(new ProductPage(_ViewModel));
}

CodePudding user response:

Set aside the fact that you are working with views and models. Simply think of them like any other class in c#.

If you need to tell class A "do something under these circumstances`, what are your options?

  • Pass a parameter in constructor: public ProductViewModel(..., bool doGetProductStatus)..., usage: new ProductViewModel(..., true);
  • Call a method A.DoSomething(); after you've created it: _ViewModel.DoSomething();
  • Use MessagingCenter Publish/Subscribe.

CodePudding user response:

I would like to declared this VM method "GetProductStatus()" to be called only when a button click event occurs.

   private async void ProductTypeButton_Clicked(object sender, EventArgs e)
  {   
    await Navigation.PushAsync(new ProductPage(_ViewModel));
  }

For above code you posted, we can find that the constructor of your viewmodel will be called as soon as you call code new ProductPage(_ViewModel).

So, you can try to remove code GetProductStatus(); in constructor ProductViewModel

public ProductViewModel(INavigation nav, Store store)
{
    _Nav = nav;
    
    // remove code here
    //GetProductStatus();   
}

and add a command in your ViewModel, and bind it to the button in your page.

Please refer to the following code:

public class ProductViewModel 
{
    public Command LoadDataCommand { get; set; }

    public ProductViewModel() {

        LoadDataCommand = new Command(loadData);

         // remove code here
        //GetProductStatus();
    }

    private void loadData()
    {
        GetProductStatus(); // add your code here
    }

    private async void GetProductStatus()
    {
        // other code
    }

 }

Note:

1.In this condition, you can also navigate as follows:

private async void ProductTypeButton_Clicked(object sender, EventArgs e) {
await Navigation.PushAsync(new ProductPage(_ViewModel)); }

2.I don't add parameter to the constructor of ProductViewModel , you can modify above code I posted according to your needs.

  • Related