How to update Label in real time

Ika777Ika777 Member ✭✭

I need to update label in real time. Have several entry fields where user inputs amount and I have a total amount label which should calculate total value in real time and display it while user is entering data.

Tagged:

Answers

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    You should be designing according to MVVM pattern.
    With that, your Label is binded to a property on your view model.
    When the property changes, your UI updates automatically.

    That you had to ask this indicates you haven't done any tutorials, or worked through the basic walk-throughs or ... well, any ... of the documentation out there on Xamarin. I strongly urge you to stop building until you've worked through some learning.

    If you have no experience with MVVM design... no experience with databinding code to UI... etc. etc. then you need to first get a handle on that.

    Start with working the free textbook from cover to cover.
    https://blogs.msdn.microsoft.com/microsoft_press/2016/03/31/free-ebook-creating-mobile-apps-with-xamarin-forms/

    Feel free to hit up on-line tutorials sites. There are plenty. Here's mine:

    Then just tackle one issue at a time in a "Sandbox" or R&D app.
    Start with the first basic need: People
    That's not Xamarin in any way. The need to define a Person class. And then inherit from that for a User class. And you'll want a Rights class to be the permissions for that User object.

    Then probably some type of grouping... whether its groups of people or groups of things. Learn how to work with collections of objects.

    List products and Add Products to cart. ( Where to store the images of the products?)

    Okay. Make a Product class. Then make a collection of them in your ViewModel: ObservableCollection<Product

    My application need to work when it is offline

    Okay. Next work on local databases such as SQLite

    But learn each of your bullet points one at a time in a little R&D app before you try to take on putting them all together. You need to understand them as a single thing before you can architect how they will interact.

    In the end... 90% of of the typical app has nothing to do with Xamarin. Its all basic-to-intermediate C#, application architecture and design that would be the same in Xamarin, WinForms, WPF, Android, ...

  • Ika777Ika777 Member ✭✭

    @ClintStLaurent thank you. Will do. When I navigate between pages and come back to it - total amount value is getting displayed. The issue is why it is not happening in real time.
    Here is my code:
    <Label Text="{Binding CurrentItem.Amount, StringFormat='{0:c}'}" Style="{DynamicResource LabelStyle}" TextColor="White" HorizontalOptions="EndAndExpand" Margin="0,5,15,5" />

    Entries:

    <renderer:CustomKeyEntry x:Name="RateAmount" ReturnType="Next" Text="{Binding CurrentItem.RateAmount, Converter={StaticResource cnvDecimal}}" Keyboard="Numeric" WidthRequest="250" BackgroundColor="#FFFFFF" TextColor="Black" HeightRequest="40" IsEnabled="{Binding IsThisEnabled}" Margin="15, 0" />

    <renderer:CustomKeyEntry x:Name="AdvancesAmount" ReturnType="Next" Text="{Binding CurrentItem.AdvancesAmount, Converter={StaticResource cnvDecimal}}" Keyboard="Numeric" WidthRequest="250" BackgroundColor="#FFFFFF" TextColor="Black" HeightRequest="40" IsEnabled="{Binding IsThisEnabled}" Margin="15, 0" />

    private void CalcCurrentItemTotal()
    {    
      CurrentItem.Amount = (CurrentItem.RatelAmount ?? 0) - (CurrentItem.AdvancesAmount ?? 0); 
      OnPropertyChanged(nameof(CurrentItem.Amount));
    } 
    private void Item_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
      switch (e.PropertyName)
    {                    
       case "RateAmount":
       case "AdvancesAmount":                    
       CalcCurrentItemTotal();
       break;                    
    }
    }
    
  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Okay.... {looking over code} ...
    So this is happening in a custom control.... ?
    I'm going to guess that Amount is a standard CLR property and not a BindableProperty... so the change events are not bubbling up

  • Ika777Ika777 Member ✭✭

    @ClintStLaurent The thing is it used to work just fine, the only major change was updating Xamarin for Visual Studio. Thank you for your help, will read about it more.

     private PSItemListDTO _currentItem;
            public PSItemListDTO  CurrentItem
            {
                get { return _currentItem; }
                set
                {
                    SetProperty(ref _currentItem, value);                
                }
            }    
    

    And my DTO Class:

         public class PSItemListDTO : INotifyPropertyChanged    
            {
                public event PropertyChangedEventHandler PropertyChanged;
    
Sign In or Register to comment.