我在 MasterDetailPage 上有一个 Button,想将 MasterDetailPage 作为 CommandParameter 发送到 Button Command,这样我就可以从 ViewModel 更改 MasterDetailPage 的 Detail 属性。如果我处理这一切都错了,请告诉我。
主明细页面
<?xml version="1.0" encoding="utf-8" ?>
<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:XamarinApp.ViewModels"
xmlns:controls="clr-namespace:XamarinApp.Libraries.Controls"
xmlns:telerikPrimitives="clr-namespace:Telerik.XamarinForms.Primitives;assembly=Telerik.XamarinForms.Primitives"
x:Class="XamarinApp.Pages.MainPage"
Icon="hamburger.png">
<MasterDetailPage.Master>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
Title="Master Page">
<StackLayout>
<Button Text="Users" Command="{Binding GoUserAccountListPage}" CommandParameter="{Binding ThisMasterDetailPage}" />
<Button Text="Working" Command="{Binding GoWhosWorkingPage}" CommandParameter="{Binding ThisMasterDetailPage}" />
<Button Text="Calendar" />
<Button Text="People" />
<Button Text="Profile"/>
</StackLayout>
</ContentPage>
</MasterDetailPage.Master>
视图模型
private RelayCommand<MasterDetailPage> _goUserAccountListPage;
public RelayCommand<MasterDetailPage> GoUserAccountListPage
{
get
{
return _goUserAccountListPage ?? (_goUserAccountListPage = new RelayCommand<MasterDetailPage>(masterDetail =>
{
masterDetail.Detail = NavigationService.GetNavigationPage(ViewModelLocator.UserAccountListPage);
masterDetail.IsPresented = false;
}));
}
}