【问题标题】:Page.BottomAppBar not being refreshed with NavigationCacheMode enabledPage.BottomAppBar 未在启用 NavigationCacheMode 的情况下刷新
【发布时间】:2014-10-06 19:46:23
【问题描述】:

这是我当前在 windows phone 8.1 中的应用程序流程:

  • 我有一个带有 Pivot 和 BottomAppBar 的 MainPage
  • 在 MainPage 中使用上述代码填充 Pivot Item 内容:
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        if (RootPivot.Items == null) return;
        RootPivot_OnPivotItemLoaded(RootPivot, new PivotItemEventArgs { Item = RootPivot.Items[RootPivot.SelectedIndex] as PivotItem });
    }

    private void RootPivot_OnPivotItemLoaded(Pivot sender, PivotItemEventArgs args)
    {
        if (args.Item.Content == null) args.Item.Content = CreateUserControlForPivotItem((string)args.Item.Header, sender);

        var content = args.Item.Content as UserControl;
        if (content == null || content.DataContext == null)
        {
            BottomAppBar.DataContext = null;
            return;
        }

        BottomAppBar.DataContext = null;
        BottomAppBar.DataContext = content.DataContext;

        var viewModel = content.DataContext as IRefreshableViewModel;
        if (viewModel != null) viewModel.Refresh();
    }

    private static UserControl CreateUserControlForPivotItem(string pivotItemHeader, Pivot pivot)
    {
        UserControl item = null;
        switch (pivotItemHeader)
        {
            case "Appointments":
                item = new AppointmentsPivotItem();
                break;

            case "Profile":
                item = new ProfilePivotItem();
                break;
        }

        return item;
    }

每个UserControl都有一个ViewModel作为DataContext,负责处理BottomAppBar:

    <Page.BottomAppBar>
    <CommandBar x:Name="BottomAppBar" Foreground="#FFFEFEFE" Background="{StaticResource BlueColorBrush}" Visibility="{Binding Converter={StaticResource BooleanToVisibilityConverter}, Path=RequiresBottomAppBar, FallbackValue=Collapsed}">
        <CommandBar.PrimaryCommands>
            <AppBarButton Icon="Add" Label="{Binding AppBarButtonAdd}" Command="{Binding AppBarButtonAddCommand}" Visibility="{Binding Converter={StaticResource BooleanToVisibilityConverter}, Path=RequiresAppBarButtonAdd, FallbackValue=Collapsed}">            </AppBarButton>
            <AppBarButton Icon="Find" Label="{Binding AppBarButtonFind}" Command="{Binding AppBarButtonFindCommand}" Visibility="{Binding Converter={StaticResource BooleanToVisibilityConverter}, Path=RequiresAppBarButtonFind, FallbackValue=Collapsed}"></AppBarButton>
        </CommandBar.PrimaryCommands>
    </CommandBar>
</Page.BottomAppBar>

在 AppointmentsPivotItem 上,我有一个 ListView,当我尝试从项目详细信息页面导航回列表时,问题就开始了。 MainPage 具有 NavigationCacheMode="Enabled" 并且在导航回时正在调用 AppBar 的绑定,但是,直到我再次浏览 Pivot 项时它才会出现。

你能建议一个解决方法吗? 提前致谢。

【问题讨论】:

    标签: c# xaml mvvm windows-phone-8.1


    【解决方案1】:

    最后,在 1 天后,我突然想到了一个可以解决问题的解决方案。 不知何故,在设置 DataContext 之前调用 BottomAppBar.UpdateLayout() 将强制栏根据绑定重新绘制。

    希望这会对某人有所帮助。

    【讨论】:

      猜你喜欢
      • 2020-12-23
      • 2017-05-11
      • 2012-07-17
      • 1970-01-01
      • 1970-01-01
      • 2018-01-08
      • 1970-01-01
      • 2020-01-14
      • 1970-01-01
      相关资源
      最近更新 更多