在Xamarin Forms中将Page作为CommandParameter发送

时间:2018-02-13 19:35:33

标签: xaml xamarin.forms xaml-binding

我在MasterDetailPage上有一个Button,并希望将MasterDetailPage作为CommandParameter发送到Button Command,以便我可以从ViewModel更改MasterDetailPage的Detail属性。如果我接近这一切都错了,请告诉我。

MasterDetailPage

<?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;
        }));
    }
}

0 个答案:

没有答案