question

jonatabiondi-3461 avatar image
0 Votes"
jonatabiondi-3461 asked LeonLu-MSFT commented

stepper_valueChanged

I have a classic products catalog page with detail page in Xamarin forms 4 with MVVM pattern with appshell
Is my first app so i have some problems

I noted that the event Stepper_ValueChanged is executed twice:
-on stepper click (obviously)
-going on detail page (i use IQueryAttributable) from catalog

in ProductViewModel i do:

public void ApplyQueryAttributes(IDictionary<string, string> query)
        {
            string productCode = HttpUtility.UrlDecode(query["productCode"]);
            Title = productCode;
            Task.Run(async () => await LoadProduct(productCode));
        }


question is how can fire valueChanged only on stepper click

void stepperQty_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            var product = vm.Product;
            var cartItem = new CartItem
            {
               ....
            };
            vm.ChangeCartQuantity(cartItem);
        }
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.

LeonLu-MSFT avatar image
0 Votes"
LeonLu-MSFT answered LeonLu-MSFT commented

Hello,​

Welcome to our Microsoft Q&A platform!

You can try to compare new&old value in the stepperQty_ValueChanged, If you click on stepper, new&old value are different. then you can execute the code. if the new&old value are the same, we do not execute this code.

private void Stepper_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            if (e.NewValue != e.OldValue)
            {
                 var product = vm.Product;
            var cartItem = new CartItem
            {
               ....
            };
            vm.ChangeCartQuantity(cartItem);
            }
           
        }


Best Regards,

Leon Lu



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.


· 2
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 LeonLu
is first thing i thinked to do but new and old are never equals due to the binding

<Entry Grid.Column="0"
x:Name="qtyEntry"
HorizontalTextAlignment="Center"
Text="{Binding Product.Quantity, Mode=TwoWay}"/>

                 <Stepper Grid.Column="1" 
                          x:Name="stepperQty"
                          ValueChanged="stepperQty_ValueChanged"
                          Value="{Binding Source={x:Reference qtyEntry}, Path=Text}"
                          Maximum="50"
                          Increment="1"
                          HorizontalOptions="Center"/>
0 Votes 0 ·

Could you share a demo that reproduce this issue?

0 Votes 0 ·
jonatabiondi-3461 avatar image
0 Votes"
jonatabiondi-3461 answered LeonLu-MSFT commented

As i said is my first app so i have to understand in depth what i do but i saw this:
if i use IQueryAttributable in ProductViewModel to navigate in product detail from products i got the behavior above


  public void ApplyQueryAttributes(IDictionary<string, string> query)
     {
         string productCode = HttpUtility.UrlDecode(query["productCode"]);
         Task.Run(async () => await LoadProduct(productCode));
   }
   private async Task LoadProduct(string productCode)
     {
          Product = await _productRepository.GetProductByCode(productCode);
     }

Insted if i use a property in ProductsViewModel all works fine:

 public ProductViewModel SelectedProduct
         {
             get => null;
             set
             {
                 Device.BeginInvokeOnMainThread(async () => await NavigateToProduct(value));
                 OnPropertyChanged(nameof(SelectedProduct));
             }
         }
    
         private async Task NavigateToProduct(ProductViewModel product)
         {
             if (product == null) return;
    
             var productDetailPage = new ProductDetailPage();
             var vm = productDetailPage.BindingContext as ProductViewModel;
             vm.Product = product;
             productDetailPage.Title = product.Code;
    
             await PageService.PushModal(productDetailPage);
         }

What that means?
Thanks




· 1
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.

I notice you use Text="{Binding Product.Quantity, Mode=TwoWay}, change it to OneWay to make a test

0 Votes 0 ·