简体   繁体   English

如何在Windows 10 UWP应用中更改根框架导航的页面名称?

[英]How do I change the page name for the root frame navigation in a Windows 10 UWP app?

I have an App.xaml file that sets the root visual to be a Frame and then immediately navigates to a page called Shell.xaml . 我有一个App.xaml文件,该文件将根视觉设置为Frame ,然后立即导航到名为Shell.xaml的页面。 Inside Shell.xaml I have implemented a hamburger menu and have another Frame control where the main application's navigation is taking place. Shell.xaml我实现了一个汉堡菜单,并使用另一个Frame控件在其中进行主应用程序的导航。

I am using Application Insights to track telemetry within the application. 我正在使用Application Insights跟踪应用程序内的遥测。 I wired up the Navigated event on the Frame nested within Shell.xaml and add a TrackPageView event within there to track all page navigations in one place. 我在嵌套在Shell.xamlFrame上连接了Navigated事件,并在Shell.xaml添加TrackPageView事件以在一个位置跟踪所有页面导航。 This is really nice because I don't have to do it in the OnNavigatedTo override in every single page. 这真的很好,因为我不必在每个页面的OnNavigatedTo重写中都这样做。

Here is the implementation of that OnNavigatedToPage event: 这是该OnNavigatedToPage事件的实现:

private void OnNavigatedToPage(object sender, NavigationEventArgs e)
{
    var pageName = e.Content.GetType().Name;
    ViewModelDispatcher.Telemetry.TrackPageView(pageName);

    // After a successful navigation set keyboard focus to the loaded page
    if (e.Content is Page && e.Content != null)
    {
        var control = (Page)e.Content;
        control.Loaded += Page_Loaded;
    }
}

The problem is this works for every page except for the initial navigation to the Shell.xaml . 问题是, 除了Shell.xaml的初始导航之外 ,这对每个页面 Shell.xaml

When I look at Application Insights I get a nice list of pages that have been browsed to: 当我查看Application Insights时,会得到一个不错的浏览页面列表:

  • AwesomeView AwesomeView
  • AnotherCoolView AnotherCoolView
  • MyView 我的观点
  • application:MyRootNamespacePrefix.Shell 应用:MyRootNamespacePrefix.Shell

Call me OCD but my elegant implementation in the nested shell produces very nice and clean page names that correspond to just the class name of the XAML view. 称我为OCD,但是我在嵌套shell中的优雅实现产生了非常漂亮和干净的页面名称,这些页面名称仅与XAML视图的类名称相对应。 I omit all the nasty namespace prefix. 我忽略了所有讨厌的名称空间前缀。 However, the navigation to Shell does not. 但是,导航到Shell却没有。

It should be noted that I am not calling the TrackPageView anywhere else in the application so I am assuming that somehow the Application Insights framework code is picking it up auto-magically. 应该注意的是,我没有在应用程序中的任何其他地方调用TrackPageView ,因此我假设Application Insights框架代码以某种方式自动神奇地将其拾取。

How do I change it so that my Shell doesn't have the nasty application:MyRootNamespacePrefix. 我如何更改它,以使我的Shell没有讨厌的application:MyRootNamespacePrefix. in front of the actual class name? 在实际的班级名称前面?

UPDATE: Eureka! 更新:尤里卡! It looks like I was right. 看来我是对的。 The Application Insights Framework must be adding a hook to the root visual's Navigated event if it happens to be a Frame. 如果Application Insights框架恰好是框架,则必须在根视觉的Navigated事件中添加一个挂钩。 All I did was add the same handler to the root frame that I added to the nested frame and it started showing up as "Shell". 我所做的就是在根框架中添加了与嵌套框架相同的处理程序,该处理程序开始显示为“ Shell”。 Unfortunately, application insights appears to add its own handler for the Navigated event of the root visual so while I get my nice clean PageView for "Shell" I also get the one with the nasty prefix. 不幸的是,应用程序见解似乎为根视觉的Navigated事件添加了自己的处理程序,因此,当我得到“ Shell”的漂亮干净的PageView时,我也得到了带有讨厌前缀的页面。

Since you are doing your own page view telemetry, you probably want to remove the initializer that collects them automatically: 由于您正在执行自己的页面视图遥测,因此您可能想要删除自动收集它们的初始化程序:

From https://azure.microsoft.com/en-us/documentation/articles/app-insights-windows-get-started/ https://azure.microsoft.com/zh-cn/documentation/articles/app-insights-windows-get-started/

you probably have something like this: 你可能有这样的事情:

WindowsAppInitializer.InitializeAsync();

but what you want is probably the explicit one, like: 但是您想要的可能是显式的,例如:

WindowsAppInitializer.InitializeAsync( 
    yourIKey, // not sure if you can leave this null to get the key from config file?
    WindowsCollectors.Metadata
    | WindowsCollectors.PageView // remove this one?
    | WindowsCollectors.Session
    | WindowsCollectors.UnhandledException);

and take out the pageview auto collector? 并取出综合浏览量自动收集器?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM