绑定更新后,如何以编程方式选择"NavigationView"的绑定"NavigationViewItem" [英] How to programmatically select a bound `NavigationViewItem` of `NavigationView` after binding is updated

查看:42
本文介绍了绑定更新后,如何以编程方式选择"NavigationView"的绑定"NavigationViewItem"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的设置基本上如下:

  • NavigationView MenuItemsSource 已绑定到 viewModel.NavItems .

  • NavItems viewModel 的计算属性.
  • NavItems is a computed property of viewModel.

视图模型类出于绑定目的实现 INotifyPropertyChanged

The view model class implements INotifyPropertyChanged for binding purpose

到达页面后,会显示 NavigationViewItem .

我需要将指定的 NavigationViewItem 设置为

I need to set a specified NavigationViewItem as the SelectedItem of the NavigationView. But there's no NavigationViewItem (from viewModel) to use inside OnNavigatedTo(NavigationEventArgs e), because at that point viewModel.NavItems is not ready yet.

那么在这种异步情况下,是否有一种选择 NavigationViewItem 的模式?

So is there a pattern for selecting a NavigationViewItem in this async situation?

<NavigationView x:Name="navView" 
                MenuItemsSource="{x:Bind viewModel.NavItems, Mode=OneWay}"
                SelectionChanged="NavView_SelectionChanged" >
…

视图模型

internal class MainPageViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        // The data service
        private MainDataService mainDataService = new MainDataService();

        private List<Book> books = new List<Book>();
        public List<Book> Books
        {
            get
            {
                return this.books;
            }
            set
            {
                this.books = value;
                this.OnPropertyChanged();
                this.OnPropertyChanged("NavItems");
            }
        }

        public IEnumerable<NavigationViewItemBase> NavItems
        {
            get
            {
                return Books.SelectMany(
                    b => (new List<NavigationViewItemBase> { new NavigationViewItemHeader {
                        Content = b.Title,
                        Tag = b.Title
                    } })
                    .Concat(
                        b.Sections.Select(s => new NavigationViewItem
                        {
                            Content = s.Title,
                            Icon = new FontIcon { Glyph = "\uE8B7", FontFamily = new FontFamily("Segoe MDL2 Assets") }
                        })
                    )
                );
            }
        }

        // @param selectedBookIndex: the index of the book whose first section
        // should be selected.
        public async Task UpdateBooks(int selectedBookIndex)
        {
            await mainDataService.PrepareData();
            this.Books = mainDataService.Books;
        }

        …
    }

推荐答案

在这种异步情况下,有没有一种选择NavigationViewItem的模式?

So is there a pattern for selecting a NavigationViewItem in this async situation?

对于异步情况,您需要使用

For async situation, you need use ObservableCollection but not List. And it represents a dynamic data collection that provides notifications when items get added, removed, or when the whole list is refreshed.

但是在OnNavigatedTo(NavigationEventArgs e)内部没有要使用的NavigationViewItem(来自viewModel),因为那时viewModel.NavItems尚未准备就绪.

But there's no NavigationViewItem (from viewModel) to use inside OnNavigatedTo(NavigationEventArgs e), because at that point viewModel.NavItems is not ready yet.

在数据未​​准备好之前,您可以将 Frame 导航保留到 NavigationView Loaded事件中用作放置器的默认页面.有关更多信息,请参考数据绑定深度.

Before the data is not ready, you could keep the Frame navigation to the default page that used as placehoder within NavigationView Loaded event. For more you could refer Data binding in depth.

这篇关于绑定更新后,如何以编程方式选择"NavigationView"的绑定"NavigationViewItem"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆