Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法创建新视图,因为尚未创建主窗口_C#_Uwp_Splash Screen_Lifecycle - Fatal编程技术网

C# 无法创建新视图,因为尚未创建主窗口

C# 无法创建新视图,因为尚未创建主窗口,c#,uwp,splash-screen,lifecycle,C#,Uwp,Splash Screen,Lifecycle,在启动过程中,我的应用程序出现问题。我遇到了一个例外 在意外时间调用了一个方法。无法创建一个 新视图,因为尚未创建主窗口 首先,我会显示一个启动屏幕,这样我可以在后台从互联网上获取一些数据。我的启动屏幕工作正常,我按照文档中的说明正确地实现了它 在App.xaml.ca中,我有一些启动屏幕的标准代码 protected override void OnLaunched(LaunchActivatedEventArgs e) { ... if (e.PreviousExecutionState !

在启动过程中,我的应用程序出现问题。我遇到了一个例外

在意外时间调用了一个方法。无法创建一个 新视图,因为尚未创建主窗口

首先,我会显示一个启动屏幕,这样我可以在后台从互联网上获取一些数据。我的启动屏幕工作正常,我按照文档中的说明正确地实现了它

在App.xaml.ca中,我有一些启动屏幕的标准代码

protected override void OnLaunched(LaunchActivatedEventArgs e)
{
...
if (e.PreviousExecutionState != ApplicationExecutionState.Running)
    {
        bool loadState = (e.PreviousExecutionState == ApplicationExecutionState.Terminated);
        ExtendedSplash extendedSplash = new ExtendedSplash(e.SplashScreen, loadState);
        Window.Current.Content = extendedSplash;
    }
...
Window.Current.Activate();
}
然后在我的应用程序构造函数中,我有这个

public static Notifications notifications;

public App()
{
    Microsoft.ApplicationInsights.WindowsAppInitializer.InitializeAsync(
        Microsoft.ApplicationInsights.WindowsCollectors.Metadata |
        Microsoft.ApplicationInsights.WindowsCollectors.Session);

    this.InitializeComponent();
    this.Suspending += OnSuspending;


    SomeClass.RunTasks();  //acquire data from a REST service

     //initializing the object for subscribing to push notification, not sure if this is the best place to put this.
    App.notifications = new Notifications("hubname", "myEndpoint");


}
异常发生在我的RunTasks()方法中,如下所示

public class SomeClass
{
    GetHTTPResponse _aggregateData = new GetHTTPResponse("http://someRestService");

    public async void RunTasks()
    {
        try
        {
            HttpResponseMessage aggregateData = await _aggregateData.AcquireResponse();
            await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
            () =>
            {

                //do a bunch of stuff with the data
             //NOTE: I am making updates to my ViewModel here with the data I acquired
             //for example App.ViewModel.Time = somevalue

                //when finished dismiss the splash screen
                ExtendedSplash.Instance.DismissExtendedSplash();

            }
            );

        }
        catch (System.Exception ex)
        {

        }
    }



}
有什么办法可以改进这一点并消除错误吗? 它是否与我更新ViewModel项(绑定到UI组件的数据)有关

编辑当我从App.cs构造函数中删除创建通知对象(并将其移动到RunTasks()方法中)时,错误消失

 App.notifications = new Notifications("hubname", "myEndpoint");

出现异常的原因是Windows.ApplicationModel.Core.CoreApplication.MainView在应用程序的构造函数中无效,因为尚未创建主视图

您可以在收到应用程序后访问它。OnLaunched/OnActivated事件

谢谢

斯特凡·威克
Windows Developer Platform

出现异常的原因是Windows.ApplicationModel.Core.CoreApplication.MainView在应用程序的构造函数中无效,因为尚未创建主视图

您可以在收到应用程序后访问它。OnLaunched/OnActivated事件

谢谢

斯特凡·威克
Windows Developer Platform

在应用程序的构造函数和OnLaunched()中运行的任务在几秒钟内完成,或者系统可能认为您的应用程序没有响应并将其终止。如果应用程序需要从网络请求数据或需要从磁盘检索大量数据,这些活动应在启动之外完成。
来源:@XavierXie MSFT“如果应用程序需要从网络请求数据或需要从磁盘检索大量数据,则这些活动应在启动之外完成。应用程序可以在等待长时间运行的操作完成时使用自己的自定义加载UI或扩展启动屏幕“-但它并没有说在哪里执行这些任务?有什么建议吗?例如,您可以在扩展的启动屏幕PageLoaded事件中执行这些任务。
在应用程序的构造函数和OnLaunched()中运行的任务”在几秒钟内完成,或者系统可能认为您的应用程序没有响应并将其终止。如果应用程序需要从网络请求数据或需要从磁盘检索大量数据,这些活动应在启动之外完成。
来源:@XavierXie MSFT“如果应用程序需要从网络请求数据或需要从磁盘检索大量数据,则这些活动应在启动之外完成。应用程序可以在等待长时间运行的操作完成时使用自己的自定义加载UI或扩展启动屏幕“-但它并没有说在哪里执行这些任务?有什么建议吗?例如,您可以在扩展的启动屏幕页面加载事件中执行这些任务。”。