【发布时间】:2017-08-13 17:57:51
【问题描述】:
我有一个 Xamarin Forms 应用程序,其中主详细信息页面作为根视图。 我正在实现 MVVM 模式。
我的根页面加载一个包含在导航页面中的详细信息页面和一个用于显示带有详细页面链接的菜单的母版页面。
母版页使用列表视图保存导航菜单 详细信息页面包含从母版页中选择的所有项目,这将显示一个新的内容页面。
我希望我的详细信息页面使用其自己的 ContentPage 内容属性来显示从 MasterPage 中选择的页面链接。 我想知道这是否可行,如果不可行,还有哪些替代方案。
这是我的 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}}" />**
我要导航到的内容页面是非常简单的内容页面类型。我想在 ViewModel 不了解视图的情况下执行此操作。我的实现至少对我来说是可行的。我担心的是 Content Page Content 属性能否显示选定的母版页链接?
【问题讨论】:
标签: c# xaml xamarin xamarin.forms