ContentView disappears when changing Content via bindings

Marius A 26 Reputation points
2021-07-06T11:44:43.43+00:00

I have a XAML file with a ContentView:

<ContentView Content="{Binding RoutineView}" />

This is the representation of the ContentView in my ViewModel:

private RoutineView _routineView;
public RoutineView RoutineView {
    get => _routineView;
    set => SetProperty(ref _routineView, value);
}

When an event occurs, I want to replace the content with a new ContentView. This is how I update the ContentView:

MessagingCenter.Subscribe<RoutineDatabase>(this, "Update", sender => {
    MainThread.InvokeOnMainThreadAsync(() => {
        RoutineView = new RoutineView();
    }).Wait();
});

This does work on iOS but it doesn't on Android (the ContentView disappears instead). Am I doing something wrong?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,291 questions
0 comments No comments
{count} votes

Accepted answer
  1. Kyle Wang 5,531 Reputation points
    2021-07-07T02:37:07.163+00:00

    Hi MariusA-0359,

    Welcome to our Microsoft Q&A platform!

    In my test, the data binding works fine. And here is the simple demo.

    xaml:

    <StackLayout>  
        <Button Text="Change" Command="{Binding ChangeCommand}"/>  
        <ContentView Content="{Binding RoutineView}" />  
    </StackLayout>  
    

    Not clear how you update the Content, so I added a button here.

    viewmodel:

    class MainPageViewModel : BaseViewModel  
    {  
        public ICommand ChangeCommand { get; set; }  
        private RoutineView _routineView;  
        public RoutineView RoutineView  
        {  
            get => _routineView;  
            set => SetProperty(ref _routineView, value);  
        }  
      
        public MainPageViewModel()  
        {  
            ChangeCommand = new Command(Change);  
            RoutineView = new RoutineView();  
      
            MessagingCenter.Subscribe<string>("", "Update", sender => {  
                MainThread.InvokeOnMainThreadAsync(() => {  
                    RoutineView = new RoutineView(Color.Red);  
                }).Wait();  
            });  
        }  
      
        void Change()  
        {  
            MessagingCenter.Send<string>("", "Update");  
      
            // Or  
            //RoutineView = new RoutineView(Color.Green);  
        }  
    }  
    

    Regardless of whether MessagingCenter is used or not, the Content will be modified.

    It seems that you are modifying RoutineView in the viewmodel. It doesn't need to use MessagingCenter and InvokeOnMainThreadAsync.

    In addition, it is not recommended to use MessagingCenter.

    Regards,
    Kyle


    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.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful