【问题标题】:Request NavigationView refresh from another part of form从表单的另一部分请求 NavigationView 刷新
【发布时间】:2019-10-29 15:49:18
【问题描述】:

我是 UWP 编码的新手。我有一个使用 VB.NET 的简单 UWP 应用程序。同一“表单”上其他地方的 ContentGrid 后面的代码会更新一些影响 NavigationView 的设置。如何让 NavigationView 刷新?

我似乎无法将它从一个视图引用到另一个视图

导航视图的 XAML 片段:

<winui:NavigationView.MenuItems>
    <winui:NavigationViewItem x:Uid="Shell_Main" Name="MainMenu" Tag="" Icon="Home" helpers:NavHelper.NavigateTo="views:MainPage" />
    <winui:NavigationViewItem x:Uid="Shell_ContentGrid" Name="Video1" Tag="" Icon="GotoStart" helpers:NavHelper.NavigateTo="views:ContentGridPage" />
    <winui:NavigationViewItem x:Uid="Shell_ContentGrid2" Name="Video2" Tag="\KES\" Icon="Slideshow" helpers:NavHelper.NavigateTo="views:ContentGridPage2" />
</winui:NavigationView.MenuItems>
<winui:NavigationView.PaneFooter>
    <NavigationViewItem Icon="Help" Content="Help" Tag="User"/>
</winui:NavigationView.PaneFooter>

表单/视图后面的 VB 代码用于禁用 OnLoaded 事件中的菜单项,工作正常

For Each topic In navigationView.MenuItems
    strDepends = topic.tag
    bAllowed = Helpers.ContentAllowed(strDepends)
    If Not bAllowed Then topic.IsEnabled = False
Next

另一个视图上的代码更改了设置,启用了菜单设置,但我似乎无法告诉菜单刷新。

我希望 NavigationView 刷新/重绘/无论什么

【问题讨论】:

  • 只是一个小小的挑剔:如果你愿意,你可以使用topic.IsEnabled = bAllowed 或相反的逻辑。现在我看多了,你可能可以直接删掉 bAllowed 除非它在其他地方使用,然后将 topic.IsEnabled 设置为 Helpers.ContentAllowed() 的结果。
  • 要添加到@JohnPete22 帖子,可以是一行topic.IsEnabled = Helpers.ContentAllowed(topic.tag)

标签: vb.net uwp uwp-xaml


【解决方案1】:

另一个视图上的代码更改了设置,启用了菜单设置,但我似乎无法告诉菜单刷新。

如果我们想让NavigationView 可以在其他视图中更新,我们在调用 Navigate 方法时将 NavigationView 实例传递给另一个视图。我发现你用过VB UWP模板工作室,请参考以下代码。

Private Sub OnItemInvoked(ByVal sender As WinUI.NavigationView, ByVal args As WinUI.NavigationViewItemInvokedEventArgs)
    Dim item = navigationView.MenuItems.OfType(Of WinUI.NavigationViewItem).First(Function(menuItem) CStr(menuItem.Content) Is CStr(args.InvokedItem))
    Dim pageType = TryCast(item.GetValue(NavHelper.NavigateToProperty), Type)
    NavigationService.Navigate(pageType, navigationView)
End Sub

其他页面

Protected Overrides Sub OnNavigatedTo(ByVal e As NavigationEventArgs)
    MyBase.OnNavigatedTo(e)
    Dim nav = TryCast(e.Parameter, Microsoft.UI.Xaml.Controls.NavigationView)

    If nav IsNot Nothing Then

        For Each item As Microsoft.UI.Xaml.Controls.NavigationViewItem In nav.MenuItems
            // update navigateview's status 
        Next
    End If
End Sub

【讨论】:

  • 作为我在 UWP 中的新手...我很困惑为什么您要更改发送页面上的 OnNavigatedTo 事件而不是带有 NavigationView 的页面
  • 好吧,我真的很笨,现在想通了。非常感谢!
猜你喜欢
  • 2012-05-25
  • 2015-01-16
  • 1970-01-01
  • 1970-01-01
  • 2015-08-11
  • 1970-01-01
  • 1970-01-01
  • 2016-05-12
  • 1970-01-01
相关资源
最近更新 更多