C# 将ContentView绑定到ContentPage
我正在尝试实现一个xamarin应用程序,它将有一个像容器一样的主页,它将承载我的其余页面(如内容视图?) MainPage.xamlC# 将ContentView绑定到ContentPage,c#,xaml,xamarin,mvvm,binding,C#,Xaml,Xamarin,Mvvm,Binding,我正在尝试实现一个xamarin应用程序,它将有一个像容器一样的主页,它将承载我的其余页面(如内容视图?) MainPage.xaml <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:d="http://xamari
<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));
}
}
公共类MainPageModel:FreshBasePageModel
{
//私有可观测集合currentView;
//公共可观测集合当前视图
//{
//获取{return\u currentView;}
//设置{u currentView=value;RaisePropertyChanged(“currentView”);}
//}
private LoginPageModel\u currentView;
公共登录模型CurrentView
{
获取{return\u currentView;}
设置{u currentView=value;RaisePropertyChanged(“currentView”);}
}
公共重写void Init(对象initData)
{
base.Init(initData);
//CurrentView=新的ObservableCollection();
//添加(新的LoginPageModel());
CurrentView=new LoginPageModel();
RaisePropertyChanged(名称(当前视图));
}
}
现在我只是想显示登录页面,但它没有出现。如果我使用代码的注释部分,我就成功了。我正在使用FreshMVVM。有什么想法吗?控件模板可以帮助您在所有页面中定义根视图,如导航栏或标题。如果要使用静态资源,我不确定为什么要绑定内容属性。如果要更改内容,则可以使用数据模板和转换器将ViewModel转换为view 如果您有兴趣更改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" />
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();
}
}
在转换器中,您可以执行以下操作:
<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" />
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();
}
}
有很多方法可以做得更好…我使用了按钮来更改内容,我不确定在选择菜单项时您希望如何更改视图。希望它能帮助您解决问题。LoginPage是ContentPage还是ContentView?LoginPage是ContenView@kaza.ma这就是,你似乎也有同样的问题。但是,它没有使用FreshMVVM。也许我的问题不够清楚。LoginPage就是一个例子。我试图做的是让MainPage具有一个菜单,其余区域将用于显示我的所有视图,具体取决于用户选择的内容,所有处理将由MainPageModel完成,只需更改与MainPage中的控件绑定的CurrentView属性。CurrentView不是LoginPageModel.@kaza.ma类型。请检查编辑的答案是否适合您的需要。