简体   繁体   中英

How do I set page content using Prism and Xamarin.Forms?

So, currently in my MainPageViewModel, I have something like this:

        WrapLayout wrapLayout;

        private string _title;
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }

        public MainPageViewModel()
        {
            wrapLayout = new WrapLayout();

            Content = new ScrollView
            {
                Margin = new Thickness(0, 20, 0, 20),
                Content = wrapLayout
            };
        }

Where MainPageViewModel inherits from BindableBase... However, I also want it to inherit from ContentPage, so I can set the page content to my wraplayout. However, it won't allow me to inherit from two base classes.

I'm confused on how to proceed then?

Here's my XAML:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:VideriMediaViewer"
             x:Class="VideriMediaViewer.MainPage">

    <ScrollView Margin="0,20,0,20">
        <local:WrapLayout x:Name="wrapLayout" />
    </ScrollView>
</ContentPage>

What can I do in this scenario? I want to bind to the tile in my xaml (have MVVM structure) but also set content page.

You've completely missed the entire point of MVVM. Your ViewModel should NEVER EVER have any View or any understanding of the View. The entire point is loosely coupled code allowing a separation of concerns.

I suggest that you head over to the Prism Samples for Xamarin Forms to get a better understanding of how to properly set up an app.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM