[英]c# xamarin forms master detail page MVVM
我有一個Xamarin Forms應用程序,其中有一個Master Detail Page作為根視圖。 我正在實現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);
}
}
這是我的轉換器,它將頁面值轉換為字符串,這樣可以使用Resource字典將其導航到。
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的母版頁
**<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不了解視圖的情況下執行此操作。 我所認為的實現至少對我來說是可行的。我所關心的是“內容頁面內容”屬性可以顯示所選的“母版頁面”鏈接嗎?
我不知道我是否正確理解您的需求。 如果要在選擇SelectedPageLink時打開ContentPage(詳細信息),我認為您可以使用MessagingCenter(希望它遵循MVVM約定...)
看看這個倉庫
有一個MasterPage,在后面的代碼中有此代碼
protected override void OnAppearing()
{
MessagingCenter.Subscribe<MasterPageViewModel, string>(this, "Detail", (arg1, arg2) => {
((MasterDetailPage)Application.Current.MainPage).Detail = new DetailPage(arg2);
((MasterDetailPage)Application.Current.MainPage).IsPresented = false;
});
base.OnAppearing();
}
protected override void OnDisappearing()
{
MessagingCenter.Unsubscribe<MasterPageViewModel, string>(this, "Detail");
base.OnDisappearing();
}
當在列表中選擇一個項目時,它會從其ViewModel收到一條消息
public MyModel SelectedItem
{
get { return _selectedItem; }
set
{
if (_selectedItem != null)
_selectedItem.Selected = false;
_selectedItem = value;
if (_selectedItem != null)
{
_selectedItem.Selected = true;
MessagingCenter.Send<MasterPageViewModel, string>(this, "Detail", _selectedItem.Name);
}
}
}
在arg2中,在列表中選擇了“名稱”。 使用此名稱創建一個DetailPage(將其傳遞到DetailPageViewMode並綁定到Detail頁面中的Label)
希望能幫助到你
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.