【问题标题】:How to programmatically select a bound `NavigationViewItem` of `NavigationView` after binding is updated绑定更新后如何以编程方式选择`NavigationView`的绑定`NavigationViewItem`
【发布时间】:2018-06-26 17:49:00
【问题描述】:

我的设置基本如下:

  • NavigationViewMenuItemsSource 绑定到viewModel.NavItems

    • NavItemsviewModel 的计算属性。
  • 视图模型类实现INotifyPropertyChanged用于绑定目的

  • 视图模型的Books异步加载。

什么有效

到达页面后会显示NavigationViewItems。

问题

我需要将指定的NavigationViewItem 设置为NavigationViewSelectedItem。但是NavigationViewItem(来自viewModel)不能在OnNavigatedTo(NavigationEventArgs e) 中使用,因为此时viewModel.NavItems 还没有准备好。

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

XAML

<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;
        }

        …
    }

【问题讨论】:

    标签: c# asynchronous data-binding uwp windows-10


    【解决方案1】:

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

    对于异步情况,您需要使用ObservableCollection 而不是List。它代表一个动态数据集合,在添加、删除项目或刷新整个列表时提供通知。

    但是在 OnNavigatedTo(NavigationEventArgs e) 中没有可以使用的 NavigationViewItem(来自 viewModel),因为此时 viewModel.NavItems 还没有准备好。

    在数据未​​准备好之前,您可以将Frame 导航保留到在NavigationView Loaded 事件中用作占位符的默认页面。 更多内容可以参考Data binding in depth

    【讨论】:

      猜你喜欢
      • 2017-04-04
      • 1970-01-01
      • 2011-07-30
      • 2013-11-01
      • 2012-12-03
      • 2011-10-22
      • 1970-01-01
      • 2014-08-18
      • 2012-04-23
      相关资源
      最近更新 更多