我有一个Xamarin Forms应用程序,其根视图为Master Detail Page,正在实现MVVM模式。
我的Root Page加载了一个Detail Page和一个Master Page。Detail Page被包装在导航页面中,Master Page用于显示链接到详情页面的菜单。
Master Page使用List View来展示导航菜单,Detail Page则保存来自Master Page的所有选定项,它会显示一个新的内容页面。
我希望我的detail页面使用其自己的ContentPage Content属性来展示来自Master Page的选定页链接。我想知道是否可能,如果不行,有哪些替代方法。
以下是我的MasterDetailPage的ViewModel。
我的Root Page加载了一个Detail Page和一个Master Page。Detail Page被包装在导航页面中,Master Page用于显示链接到详情页面的菜单。
Master Page使用List View来展示导航菜单,Detail Page则保存来自Master Page的所有选定项,它会显示一个新的内容页面。
我希望我的detail页面使用其自己的ContentPage Content属性来展示来自Master Page的选定页链接。我想知道是否可能,如果不行,有哪些替代方法。
以下是我的MasterDetailPage的ViewModel。
public class PageViewModel: ViewModelBase
{
public event EventHandler<string> ItemSelectedEventHandler;
string _selectedpagelink;
public string SelectedPageLink
{
get { return _selectedpagelink; }
set
{
if (_selectedpagelink != value)
{
_selectedpagelink = value;
OnItemSelected(this,value);
OnPropertyChanged();
}
}
}
public ObservableCollection<string> Links =>
new ObservableCollection<string>
{
"PageOne",
"PageTwo"
};
public PageViewModel()
{
this.SelectedPageLink = this.Links.FirstOrDefault();
}
protected virtual void OnItemSelected(object sender , string newPage)
{
ItemSelectedEventHandler?.Invoke(this, newPage);
}
}
这里是我的转换器,它将页面值转换为字符串,这样就可以使用资源字典进行导航。
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var key = value as string;
if (key == null)
{
throw new ArgumentNullException("Resource key is not found", nameof(value));
}
return Application.Current.Resources[key];
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
这是我的主控页面,使用了内容页面和列表视图。
**<ListView
x:Name="ListViewMenuMaster"
ItemsSource="{Binding Links}"
SelectedItem="{Binding SelectedPageLink}"
SeparatorVisibility="None"
HasUnevenRows="true">
<ListView.Header>
<Grid BackgroundColor="#03A9F4">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="10"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="10"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="80"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="10"/>
</Grid.RowDefinitions>
<Label
Grid.Column="1"
Grid.Row="2"
Text="My App Name here"
Style="{DynamicResource SubtitleStyle}"/>
</Grid>
</ListView.Header>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Padding="15,10" HorizontalOptions="FillAndExpand">
<Label VerticalOptions="FillAndExpand"
VerticalTextAlignment="Center"
Text="{Binding}"
FontSize="24"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>**
这是我的详细页面,应该显示来自主页面的所选链接
**<ContentPage.BindingContext>
<vm:PageViewModel />
</ContentPage.BindingContext>
<ContentPage.Resources>
<ResourceDictionary>
<conv:ResourceLookUpConverter x:Key="resourceLookupConverter"/>
</ResourceDictionary>
</ContentPage.Resources>
*Can the content property below be used to display the selected page links???*
<ContentPage Content="{Binding SelectedPageLink, Converter={Binding resourceLookupConverter}}" />**
我要导航到的内容页面是非常简单的Content Page类型。我希望在不让ViewModel知道任何有关视图的情况下完成此操作。我认为我已经实现了这一点,但我的担忧是Content Page Content属性能否显示所选的Master Page链接?