将ContentView绑定到ContentPage

时间:2020-08-08 19:53:39

标签: c# xaml xamarin mvvm binding

我正在尝试实现一个xamarin应用程序,该应用程序将具有一个类似于容器的MainPage,该容器将托管我的其余页面(作为内容视图?)。

MainPage.xaml

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:d="http://xamarin.com/schemas/2014/forms/design"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         xmlns:views="clr-namespace:TalosLib"
         mc:Ignorable="d"
         x:Class="TalosLib.MainPage">


<ContentPage.Content>
    <StackLayout >

        <StackLayout.Resources>
            <DataTemplate x:Key="login">
                <views:LoginPage />
            </DataTemplate>
        </StackLayout.Resources>
        
        <ContentView Content="{Binding CurrentView}" ControlTemplate="{StaticResource login}"/>
    <!--<CollectionView ItemsSource="{Binding CurrentView}" ItemTemplate="{StaticResource login}"/>-->
    </StackLayout>
</ContentPage.Content>

MainPageModel.cs

public class MainPageModel : FreshBasePageModel
{
    //private ObservableCollection<LoginPageModel> _currentView;

    //public ObservableCollection<LoginPageModel> CurrentView
    //{
    //    get { return _currentView; }
    //    set { _currentView = value; RaisePropertyChanged("CurrentView"); }
    //}

    private LoginPageModel _currentView;

    public LoginPageModel CurrentView
    {
        get { return _currentView; }
        set { _currentView = value; RaisePropertyChanged("CurrentView"); }
    }

    public override void Init(object initData)
    {
        base.Init(initData);
        //CurrentView = new ObservableCollection<LoginPageModel>();
        //CurrentView.Add(new LoginPageModel());
        CurrentView = new LoginPageModel();
        RaisePropertyChanged(nameof(CurrentView));
    }


}

现在我正试图显示LoginPage,但它没有出现。如果我使用了代码的注释部分,我设法使其工作。我正在使用FreshMVVM。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

控制模板可帮助您在所有页面中定义根视图,如导航栏或标题。我不确定如果要使用静态资源,为什么要绑定内容属性。如果您要更改内容,那么我们可以使用数据模板并使用转换器将ViewModel转换为视图。

如果您想更改ContentView的内容,则可以按以下方式使用数据模板:

    <ResourceDictionary>
        <views:DataTemplateToViewConverter x:Key="dataTemplateToViewConverter" />

        <DataTemplate x:Key="Login">
            <views:LoginView />
        </DataTemplate>
        <DataTemplate x:Key="Demo">
            <views:DemoView />
        </DataTemplate>
    </ResourceDictionary>


     <ContentView x:Name="contentview" Content="{Binding MyTemplate, Converter={StaticResource dataTemplateToViewConverter}}" />
        <Button
            Command="{Binding Clicked1}"
            Text="1" />
        <Button
            Command="{Binding Clicked2}"
            Text="2" />

在ViewModel中,您可以使用命令界面并设置模板 关于单击的命令。.不要忘记创建MyTemplate可绑定属性。

    private void Clicked2Called(object obj)
    {
        MyTemplate = "DemoView";
    }

    private void Clicked1Called(object obj)
    {
        MyTemplate = "Login";
    }

在转换器中,您可以执行以下操作:

  public class DataTemplateToViewConverter : IValueConverter
  {
    public DataTemplateToViewConverter()
    {
    }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value.ToString() == "Login")
            return new LoginView();
        else
            return new DemoView();
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
  }

有很多方法可以更好地做到这一点...我已经使用按钮来更改内容,不确定选择菜单项后如何更改视图。希望它可以帮助您解决问题。

相关问题