C# App.xaml.cs中的函数在应用程序启动时被调用两次

C# App.xaml.cs中的函数在应用程序启动时被调用两次,c#,windows-phone-8,app.xaml,C#,Windows Phone 8,App.xaml,我正在构建一个Windows phone应用程序,我面临着这个问题:App.xaml.cs文件中有一个FirstRun函数,用于检查应用程序是否首次运行。我有两个信息框,每次都会显示首次跑步和非首次跑步。问题是,当第一次运行应用程序时,我会收到消息first run,但在此之后,如果不继续,我会收到另一条消息Not first run,并且程序不会继续执行第一次运行时需要执行的功能。是什么导致对IsFirstRun函数的双重调用,并返回不同的结果?以下是功能: public static bo

我正在构建一个Windows phone应用程序,我面临着这个问题:App.xaml.cs文件中有一个FirstRun函数,用于检查应用程序是否首次运行。我有两个信息框,每次都会显示首次跑步和非首次跑步。问题是,当第一次运行应用程序时,我会收到消息first run,但在此之后,如果不继续,我会收到另一条消息Not first run,并且程序不会继续执行第一次运行时需要执行的功能。是什么导致对IsFirstRun函数的双重调用,并返回不同的结果?以下是功能:

 public static bool IsFirstRun()
    {
        if (!settings.Contains(FIRST_RUN_FLAG)) //First time running
        {
            settings.Add(FIRST_RUN_FLAG, false);
            return true;
        }
        return false;   
    }
以及帮助我进入正确页面的UriMapper:

公共类FirstRunUriMapper:UriMapperBase { 公共覆盖Uri映射Uri {

        if (uri.OriginalString == "/LaunchPage.xaml")
        {
            MessageBox.Show("Searching for LaunchPage...");
            if (App.IsFirstRun())
            {
                MessageBox.Show("First run...Should show introductionPage...");
                uri = new Uri("/MainPage.xaml", UriKind.Relative);
            }
            else
            {
                MessageBox.Show("...Should show MainPage...");
                uri = new Uri("/IntroductionPage.xaml", UriKind.Relative);
            }
        }
        return uri;
    }
}
编辑: 正如所问的,我为整个类提供了修改后的UriMapper,现在可以正常工作了,但仍然找不到导致double MapUri方法调用的原因:

  public class FirstRunUriMapper : UriMapperBase
{

    public override Uri MapUri(Uri uri)
    {

        if (uri.OriginalString == "/LaunchPage.xaml")
        {
            if (App.FirstRun)
            {
                if (App.IsFirstRun())
                {
                    uri = new Uri("/IntroductionPage.xaml", UriKind.Relative);
                }
                else
                {
                    uri = new Uri("/IntroductionPage.xaml", UriKind.Relative);
                    App.FirstRun = false;
                }
            }
            else
            {
                uri = new Uri("/MainPage.xaml", UriKind.Relative);
            }
        }
        return uri;
    }
}
以及App.xaml.cs类:

公共部分类应用程序:应用程序 {

        if (uri.OriginalString == "/LaunchPage.xaml")
        {
            MessageBox.Show("Searching for LaunchPage...");
            if (App.IsFirstRun())
            {
                MessageBox.Show("First run...Should show introductionPage...");
                uri = new Uri("/MainPage.xaml", UriKind.Relative);
            }
            else
            {
                MessageBox.Show("...Should show MainPage...");
                uri = new Uri("/IntroductionPage.xaml", UriKind.Relative);
            }
        }
        return uri;
    }
}

}

请看下面的示例:

您是否尝试连接RootFrame.UriMapper而不是重写

private void Application_Launching(object sender, LaunchingEventArgs e)
{
     RootFrame.UriMapper = new YourUriMapper();
}

这不是问题,伙计…MessageBox上不存在问题,但事实上,MapUri方法在应用程序启动时被调用了两次…但你不能正确更改,除非它是从你自己的代码中调用的。设置对象是一个吗?它可能在平台启动代码中被初始化两次是的,我完全按照这个问题的步骤…问题是MapUri方法被调用了两次,这导致了我的问题…你知道为什么这个函数被调用了两次吗?我已经搜索了所有内容,但仍然找不到答案…谢谢你的帮助!示例代码中没有看到静态方法。请提供与示例包括您从app.xaml访问隔离存储和应用程序事件,以便我可以看到您的偏差是哪一部分导致了您的问题。我会看看自己是否可以在这期间重现。
private void Application_Launching(object sender, LaunchingEventArgs e)
{
     RootFrame.UriMapper = new YourUriMapper();
}