0

我是一名 Android 开发人员,正在尝试学习 UWP 开发,并尝试使用Prism 库Windows Template Studio来帮助我使用样板功能。

我的目标是拥有一个所有视图都可以访问的 ProgressCircle 控件,这样每当我执行异步调用时,我就可以弹出 ProgressCircle。为了保持干燥,我不想在每个视图中都有一个 ProgressCircle。我怎样才能做到这一点?

据我了解,Windows Template Studio 构建的 (Prism) 有一个 ShellPage 视图,其中包含所有其他视图,并且它有自己的 viewModel。这似乎有点类似于 Android 的 Activity/Fragment 模型。我最初的想法是将 ProgressCircle 放在 ShellPage 视图中,然后在需要时从我的子视图 (MainPage) 中调用它。但是我还没有弄清楚如何从子/其他视图/viewModel 调用其他视图 viewModel 方法。这是正确的方法吗?如果是,我该如何做到这一点?

我的另一个想法是创建一个可以添加到 viewModel 基类的 Progress 应用服务,但我还没有了解应用服务。

4

1 回答 1

1

好的,所以我不确定 Prism 是否有任何不同,因为我正在使用代码行为模式(我对此一无所知,但据我所知,它并没有那么不同)。无论如何,由 Windows Template Studio 创建的 Shell Page 应该类似于:

<Page
    x:Class="MyProject.Views.ShellPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:i="using:Microsoft.Xaml.Interactivity"
    xmlns:behaviors="using:armada_vpn.Behaviors"
    xmlns:winui="using:Microsoft.UI.Xaml.Controls"
    xmlns:helpers="using:MyProject.Helpers"
    xmlns:views="using:MyProject.Views"
    Loaded="OnLoaded"
    mc:Ignorable="d" Background="Black">

    <winui:NavigationView
        x:Name="navigationView"
        IsBackButtonVisible="Visible"
        IsBackEnabled="{x:Bind IsBackEnabled, Mode=OneWay}"
        SelectedItem="{x:Bind Selected, Mode=OneWay}"
        ItemInvoked="OnItemInvoked"
        IsSettingsVisible="False"
        Background="White" RequestedTheme="Light"
        OpenPaneLength="200">

        <winui:NavigationView.MenuItems>
            <!--
            TODO WTS: Change the symbols for each item as appropriate for your app
            More on Segoe UI Symbol icons: https://docs.microsoft.com/windows/uwp/style/segoe-ui-symbol-font
            Or to use an IconElement instead of a Symbol see https://github.com/Microsoft/WindowsTemplateStudio/blob/master/docs/projectTypes/navigationpane.md
            Edit String/en-US/Resources.resw: Add a menu item title for each page
            -->

            <winui:NavigationViewItem x:Uid="Shell_Main" Icon="Home" helpers:NavHelper.NavigateTo="views:MainPage" />
        </winui:NavigationView.MenuItems>
        <i:Interaction.Behaviors>
            <behaviors:NavigationViewHeaderBehavior
                x:Name="navigationViewHeaderBehavior"
                DefaultHeader="{x:Bind ViewModel.Selected.Content, Mode=OneWay}">
                <behaviors:NavigationViewHeaderBehavior.DefaultHeaderTemplate>
                    <DataTemplate>
                        <Grid>
                            <TextBlock
                                Text="{Binding}"
                                Style="{ThemeResource TitleTextBlockStyle}"
                                Margin="{StaticResource SmallLeftRightMargin}" />
                        </Grid>
                    </DataTemplate>
                </behaviors:NavigationViewHeaderBehavior.DefaultHeaderTemplate>
            </behaviors:NavigationViewHeaderBehavior>
            <ic:EventTriggerBehavior EventName="ItemInvoked">
                <ic:InvokeCommandAction Command="{x:Bind ViewModel.ItemInvokedCommand}" />
            </ic:EventTriggerBehavior>
        </i:Interaction.Behaviors>
        <Grid> #This here is important for you
            <Frame x:Name="shellFrame"/>
        </Grid>
    </winui:NavigationView>
</Page>

页面 XAML 中指定的网格区域是显示不同视图的地方,因为 ShellPage 控制此框架中显示的内容。

无论如何,您想要做的是将进度环添加到此框架的顶部(并具有透明背景)。为此,您可以为两个元素指定 ZIndex(具有最高 ZIndex 的元素将显示在顶部:

<Grid>
    <ProgressRing x:Name="ProgressRing" Canvas.ZIndex="2" Background="Transparent"/>
    <Frame x:Name="shellFrame" Canvas.ZIndex="1"/>
</Grid>

或者,您可以简单地将 ProgressRing 定义为此处的最后一个元素(因为没有指定任何 ZIndex,呈现顺序是从上到下):

<Grid>
    <Frame x:Name="shellFrame"/>
    <ProgressRing x:Name="ProgressRing" Background="Transparent"/>
</Grid>

然后,可以使用您为 ProgressRing 指定的名称在 ShellPage 中访问它,但从其他视图访问它可能会很棘手,因为您需要直接从它们访问 ShellPage 实例。您可以做的一件事是实现用于激活和停用可以从代码中的任何位置引发的 ProgressRing 的事件,并在您的 ShellPage 类中实现将订阅这些事件的处理程序。

希望能帮助到你。

于 2019-03-27T03:49:33.213 回答