gpt4 book ai didi

c# - 应用程序无法导航到 MainPage

转载 作者:行者123 更新时间:2023-11-30 14:55:04 26 4
gpt4 key购买 nike

我的 WP8.1 RT 应用程序遇到了一个奇怪的问题 - 我的一位 Beta 测试员报告说他在启动画面后看到黑屏。该应用程序不会崩溃、挂断电话或其他 - 只是黑屏而不是 MainPage。我在代码中实现了一些 Trace 方法来跟踪问题。代码如下所示:

// OnLaunched method that gets called when the App starts
protected async override void OnLaunched(LaunchActivatedEventArgs e)
{
await Trace.WriteLineAsync(true, "Launched");
Frame rootFrame = CreateRootFrame();
await Trace.WriteLineAsync(true, "Before - better checkup");
if (rootFrame.Content == null)
{
await Trace.WriteLineAsync(false, "Rootframe content was null, navigating");
if (!rootFrame.Navigate(typeof(MainPage), e.Arguments))
await Trace.WriteLineAsync(false, "Navigation false");
else await Trace.WriteLineAsync(false, "Navigation was ok");
}
await Trace.WriteLineAsync(true, "After - better checkup");
Window.Current.Activate();
}

// Method creating the rootFrame:
private Frame CreateRootFrame()
{
Trace.WriteLineAsync(true, "Create Root frame");
Frame rootFrame = Window.Current.Content as Frame;
if (rootFrame == null)
{
Trace.WriteLineAsync(true, "Root frame was null");
rootFrame = new Frame();
SuspensionManager.RegisterFrame(rootFrame, "AppFrame");
rootFrame.NavigationFailed += OnNavigationFailed;
Window.Current.Content = rootFrame;
Trace.WriteLineAsync(false, "Window content {0}", Window.Current.Content.ToString());
}
return rootFrame;
}

Beta 测试人员获得的日志如下所示:

2014-10-06 13:02:56: Launched
2014-10-06 13:02:56: Create Root frame
2014-10-06 13:02:56: Root frame was null
Window content Windows.UI.Xaml.Controls.Frame
2014-10-06 13:02:57: Before - better checkup
Rootframe content was null, navigating
Navigation false
2014-10-06 13:02:57: After - better checkup
2014-10-06 13:03:01: App.cs suspending event

如您所见,最重要的行是Navigation false,这意味着

if (!rootFrame.Navigate(typeof(MainPage), e.Arguments))
await Trace.WriteLineAsync(false, "Navigation false");

导航 返回 false - 我无法在我的 Frame 内导航,没有崩溃,没有挂起 -(挂起事件有效),只是无法到达 MainPage。我完全不知道问题的根源是什么。此外 - 在我的手机上一切正常,其他设备也一样 - 调试和发布。

有人知道为什么我无法导航到我的主页吗?

编辑 - 添加导航失败事件:

void OnNavigationFailed(object sender, NavigationFailedEventArgs e)
{
Trace.WriteLineAsync(true, "Failed to load page");
throw new Exception("Failed to load Page " + e.SourcePageType.FullName);
}

最佳答案

感谢Nate Diamond comment我开始以不同的方式寻找问题的原因。我添加这个答案是因为它有一天可能会对某人有所帮助。

经过多次尝试和 beta 测试人员的帮助,最终发现问题是由在 Page 的构造函数之前初始化的变量之一引起的。初始化导致异常被 Navigate 方法吞没,因此它返回 false。

另一件事是初始化导致异常的原因,但那是另一回事。

关于c# - 应用程序无法导航到 MainPage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26215704/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com