Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 91519

Dynamic ContentViews with MVVM

$
0
0

I'm struggling to get content on ContentPage to change based on a property in my view model. On Screen I see "My App" and "Bottom" but I see nothing for the middle content (expecting that to appear in the place of the ContentView).

XAML:
`
<?xml version="1.0" encoding="utf-8" ?>

<StackLayout Orientation="Vertical">
    <Label Text="{Binding MainText}" VerticalOptions="Center" HorizontalOptions="Center" />
    <ContentView Content="{Binding ActiveView}" />
    <Label Text="Bottom" VerticalOptions="Center" HorizontalOptions="Center" />
</StackLayout>

`

C# App class:

public class App : Application
        {
            public App()
            {
                // The root page of your application
                MainPage = new MainGamePage();
                MainPage.BindingContext = new MainPageViewModel();
            }
        }

C# ViewModel:
`
public class MainPageViewModel : BaseViewModel
{
public string MainText { get; set; }
public BaseViewModel Model { get; set; }
public ContentView ActiveView { get; set; }

    public MainPageViewModel()
        : base(null)
    {

        MainText = "My App";

        Model = new ListOfStuffViewModel();
        FirePropertyChanged("Model");

        var view = new ListOfStuffContentView();
        view.BindingContext = Model;
        ActiveView = view;
        Model.Initialize();     

        FirePropertyChanged("ActiveView");
    }

}`

I'm not sure how to write XAML to have a body that is replaced by a ContentView created in code.

Thnx
Matt


Viewing all articles
Browse latest Browse all 91519

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>