Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.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#_Xaml_Asynchronous_Windows Phone 8_Windows Phone - Fatal编程技术网

C# 在转到主视图之前准备数据

C# 在转到主视图之前准备数据,c#,xaml,asynchronous,windows-phone-8,windows-phone,C#,Xaml,Asynchronous,Windows Phone 8,Windows Phone,当用户触摸应用程序图标时, 我想在用户转到主视图之前执行这些步骤 从URI获取json字符串 使用JArray.Parse获取值 全部完成后,转到主视图 问题是如何防止用户转到主视图 并将所有代码放入 我试图将它放在App.xaml.cs文件中的应用程序启动方法中 // Code to execute when the application is launching (eg, from Start) // This code will not execute when the applicat

当用户触摸应用程序图标时, 我想在用户转到主视图之前执行这些步骤

  • 从URI获取json字符串
  • 使用JArray.Parse获取值
  • 全部完成后,转到主视图
  • 问题是如何防止用户转到主视图 并将所有代码放入
    我试图将它放在App.xaml.cs文件中的应用程序启动方法中

    // Code to execute when the application is launching (eg, from Start)
    // This code will not execute when the application is reactivated
    private void Application_Launching(object sender, LaunchingEventArgs e) 
    {
        // code here
    }
    
    但这并不阻止程序在抓取完成之前转到主视图。
    我发现实际上在MainPage.xaml中,如果我这样写代码

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        while(true) {} 
        // it will prevent the program to go to the main view, 
        // and will stick with the loading screen until this function reach its end
    }
    
    所以我想,我可以把所有的代码都放在这里,当我完成抓取时,我会打断while,它会自动转到主视图。

    我试着,这就是代码

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        bool isFetchFinished = false;
    
        ObservableCollection<PromoViewModel> Promos = new ObservableCollection<PromoViewModel>();
    
        WebClient client = new WebClient();
    
        client.DownloadStringCompleted += (s, evt) =>
        {
            if (evt.Error == null)
            {
                // Retrieve the JSON
                string jsonString = evt.Result;
    
                JArray promos = JArray.Parse(jsonString);
                foreach (JObject promo in promos)
                {
                    string name = promo["name"].Value<string>();
                    string description = promo["description"].Value<string>();
                    string img = promo["image"].Value<string>();
    
                    Promos.Add(new PromoViewModel() { Name = name, Description = description, Img = img });
                }
              isFetchFinished = true;
              System.Diagnostics.Debug.WriteLine("finish fetch");
           }
        };
    
        // run 
        client.DownloadStringAsync(new Uri("the json url")); 
    
        while(true) {
            if(isFetchFinished) {
                App.ViewModel.LoadData(Promos); // pass value to main view model
                break; // after complete, break
            }
        }
    }
    
    产生无限循环

    我想我把获取代码放错了方法。这一切该放在哪里?哎哟,你都做错了。首先,您必须指定起始页。如果您想在导航到数据之前下载一些数据,可以创建一个特殊的“下载”页面,该页面实际上是启动应用程序时导航到的第一个页面。然后,一旦下载完成,就可以导航到主页。这实际上是对扩展启动屏幕的替代


    另外,千万不要在任何UI代码中放入
    while(true)
    ,这样只会冻结应用程序。此外,如果应用程序被冻结,你永远也没有机会“解冻”它。

    谢谢兄弟,我来自iOS世界,所以iOS就是这么做的。我不知道在windows phone中,要下载数据,我们必须创建一个新视图。你能告诉我一些语法吗?我没有说你需要创建一个新的视图,我说你可以创建一个不同的起始页-一个显示一些“下载…”文本的起始页,直到数据下载完成,然后自动将用户带到实际的起始页。
    isFetchFinished = true; // will never executed