【发布时间】: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