如何在 Prism 中指向 MasterDetailPage

How to point to MasterDetailPage in Prism

我们有一个应用程序 运行ning 在 MVVM 上,没有任何框架。尝试使用 Prism 库复制一个非常基本的 MasterDetailPage。

从之前的项目中复制了 MasterPage 并做了一些调整。

当我运行这个的时候,出现了一个白色的空白页?!?!有什么想法吗?

MasterPage.xaml

<?xml version="1.0" encoding="utf-8" ?>

<MasterDetailPage.Master>
    <ContentPage Title="Master" >
        <StackLayout BackgroundColor="AliceBlue">
            <Grid BackgroundColor="RoyalBlue">
                <BoxView  HeightRequest="120" />
                <Label Text="Welcome to Athlosify" TextColor="White" FontSize="Small" Margin="20, 50, 0, 0" />
            </Grid>
            <StackLayout Orientation="Horizontal" Margin="20,20,0,0" Spacing="10">
                <ImageButton>
                    <ImageButton.Source>
                        <FontImageSource FontFamily="{StaticResource MaterialFontFamily}"  Glyph="&#xf6a0;" Size="25" />
                    </ImageButton.Source>
                </ImageButton>
                <Label Text="Home" FontSize="Small" VerticalOptions="Center" TextColor="#707070"></Label>
                <StackLayout.GestureRecognizers>
                    <TapGestureRecognizer Command="{Binding HomeCommand}" />
                </StackLayout.GestureRecognizers>
            </StackLayout>
            <StackLayout Orientation="Horizontal" Margin="20,20,0,0" Spacing="10">
                <ImageButton>
                    <ImageButton.Source>
                        <FontImageSource FontFamily="{StaticResource MaterialFontFamily}"  Glyph="&#xf224;" Size="25" />
                    </ImageButton.Source>
                </ImageButton>
                <Label Text="Activities" FontSize="Small" VerticalOptions="Center" TextColor="#707070"></Label>
                <StackLayout.GestureRecognizers>
                    <TapGestureRecognizer Command="{Binding ActivitiesCommand}" />
                </StackLayout.GestureRecognizers>
            </StackLayout>
            <StackLayout Orientation="Horizontal" Margin="20,20,0,0" Spacing="10">
                <ImageButton>
                    <ImageButton.Source>
                        <FontImageSource FontFamily="{StaticResource MaterialFontFamily}"  Glyph="&#xf30b;" Size="25" />
                    </ImageButton.Source>
                </ImageButton>
                <Label Text="Change Password" FontSize="Small" VerticalOptions="Center" TextColor="#707070"></Label>
                <StackLayout.GestureRecognizers>
                    <TapGestureRecognizer Command="{Binding ChangePasswordCommand}" />
                </StackLayout.GestureRecognizers>
            </StackLayout>
            <StackLayout Orientation="Horizontal" Margin="20,20,0,0" Spacing="10">
                <ImageButton>
                    <ImageButton.Source>
                        <FontImageSource FontFamily="{StaticResource MaterialFontFamily}"  Glyph="&#xf343;" Size="25" />
                    </ImageButton.Source>
                </ImageButton>
                <Label Text="Logout" FontSize="Small" VerticalOptions="Center" TextColor="#707070"></Label>
                <StackLayout.GestureRecognizers>
                    <TapGestureRecognizer Command="{Binding LogoutCommand}" />
                </StackLayout.GestureRecognizers>
            </StackLayout>
            <StackLayout Orientation="Horizontal" Margin="20,20,0,0" Spacing="10">
                <ImageButton>
                    <ImageButton.Source>
                        <FontImageSource FontFamily="{StaticResource MaterialFontFamily}"  Glyph="&#xf343;" Size="25" />
                    </ImageButton.Source>
                </ImageButton>
                <Label Text="About" FontSize="Small" VerticalOptions="Center" TextColor="#707070"></Label>
                <StackLayout.GestureRecognizers>
                    <TapGestureRecognizer Command="{Binding AboutCommand}" />
                </StackLayout.GestureRecognizers>
            </StackLayout>
        </StackLayout>
    </ContentPage>
</MasterDetailPage.Master>
<MasterDetailPage.Detail>
    <NavigationPage>
        <x:Arguments>
            <pages:HomePage></pages:HomePage>
        </x:Arguments>
    </NavigationPage>
</MasterDetailPage.Detail>

MasterViewModel.cs:

public class MasterPageViewModel : BindableBase
{
    private DelegateCommand _homeCommand;
    private readonly INavigationService _navigationService;

    public DelegateCommand HomeCommand => _homeCommand ?? (_homeCommand = new DelegateCommand(ExecuteGoToHome));

    public MasterPageViewModel(INavigationService navigationService)
    {
        _navigationService = navigationService;
    }

    async void ExecuteGoToHome()
    {
        await _navigationService.NavigateAsync("NavigationPage/HomePage");
    }
}

App.xaml.cs:

public App() : this(null) { }

    public App(IPlatformInitializer initializer) : base(initializer) { }

    protected override async void OnInitialized()
    {
        InitializeComponent();

        await NavigationService.NavigateAsync("MasterDetailPage/NavigationPage/HomePage");
    }

    protected override void RegisterTypes(IContainerRegistry containerRegistry)
    {
        containerRegistry.RegisterForNavigation<NavigationPage>();
        containerRegistry.RegisterForNavigation<HomePage, HomePageViewModel>();
        containerRegistry.RegisterForNavigation<MasterPage, MasterPageViewModel>();
    }

没关系。由于默认情况下设置的异常设置,找出了导致它的原因。一旦将捕获Common Language RunTime Exception集设置为all,发现错误与Master详细信息页面中的XAML格式有关。修复它并正常工作。