Page.BottomAppBar не обновляется с включенным NavigationCacheMode

Вот мой текущий поток приложений в Windows Phone 8.1:

  • У меня есть MainPage с Pivot и BottomAppBar.
  • Содержимое сводного элемента заполняется с помощью приведенного выше кода в MainPage:
    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.

Не могли бы вы предложить обходной путь? Заранее спасибо.


person buonaparte    schedule 06.10.2014    source источник


Ответы (1)


Наконец, через 1 день я внезапно пришел к решению, которое поможет. Каким-то образом вызов BottomAppBar.UpdateLayout() перед установкой его DataContext заставит панель перерисовываться в соответствии с привязкой.

Надеюсь, это поможет кому-то.

person buonaparte    schedule 07.10.2014