Home > Blockchain >  How to render the changes in view from view model in Xamarin forms
How to render the changes in view from view model in Xamarin forms

Time:03-12

I tried several things like adding

Device.BeginInvokeOnMainThread(async () =>
           {
               Title = TableCategory.name;
           });

but didn't work. my viewmodel looks like this

 private string _title;
        public string Title
        {
            get
            {
                return _title;
            }
            set
            {
                if (_title != value)
                {
                    _title = value;
                    OnPropertyChanged(nameof(Title));
                }
            }
        }

every time I select data from my collectionView below function will set data to Title and what I want is to rendered the data present in Title directly to View.

 async Task TableSelected(object obj)
        {
            TableCategory = (TableCategory)obj;
            Device.BeginInvokeOnMainThread(async () =>
            {
                Title = TableCategory.name;
                await App.Current.MainPage.DisplayAlert("Success", $"{Title}", "Ok");
            });
            await PopupNavigation.Instance.PopAsync();

        }

My XAML code looks like this:

                       Text="{Binding Title,Mode=TwoWay}"
                       HeightRequest="20"
                           VerticalOptions="Center"
                           TextColor="#FF464859" FontSize="Small">

What can I try next?

CodePudding user response:

Based on the details you provided in your question, I don't see any issues. Check that TableCategory.name and Title in the TableSelected method are different

It seems likely that the values for TableCategory.name/value and Title/_title would be the same after TableSelected, so the UI won't be different.

if (_title != value)
{
     _title = value;
     OnPropertyChanged(nameof(Title));
}

Please debug the above if (_title != value) condition in your code. Provide a minimal reproducible code sample if the issue still persists.

CodePudding user response:

Does it work if you replace Title = TableCategory.name; with Title = "HardcodedName";?

If that works, then something is wrong with TableCategory.name.

If that does not work, then remove await PopupNavigation.Instance.PopAsync();.


Important: any code inside of BeginInvokeOnMainThread gets executed AFTER method TableSelected returns.

What is PopAsync supposed to be removing?

  • Related