简体   繁体   English

在Windows Phone 8上从App.xaml.cs运行任务

[英]Running a Task from App.xaml.cs on the Windows Phone 8

I have the following task i would love to initialize once the app is running, and just populate a value in my App.xaml.cs 我有以下任务,一旦应用程序运行,我想初始化,然后在App.xaml.cs中填充一个值

public partial class App : Application
{
    public bool ProcessedTask = false;
    public List<Item> Items = new List<Item>();

    public App()
    {
         ...

        // Standard XAML initialization
        InitializeComponent();

        // NOW HERE I WOULD LIKE TO INVOKE MY TASK
    }

    public async Task<string> RequestDataTask()
    {
        HttpClient client = new HttpClient();

        Task<string> getStringRequests = client.GetStringAsync("http://test.com/get/");

        ItemsRootObject responseObject = Newtonsoft.Json.JsonConvert.DeserializeObject<ItemsRootObject>(getStringRequests);

        foreach (Item item in responseObject.rows)
        {
           Items.Add(item);
        }

        ProcessedTask = true;

        string Message = "Processed Task";

        return Message;

    }
}

public class ItemsRootObject
{
    public List<Item> rows { get; set; }
    public bool success { get; set; }
    public int code { get; set; }
    public string msg { get; set; }
}

public class Item 
{
    public string value { get; set; }
}

I already know the JSON return and objects are there correctly (works in a regular call), but i don't know (understand) how to invoke my task while keeping the phone app going (load MainPage.xaml). 我已经知道JSON返回和对象正确存在(可以在常规调用中工作),但是我不知道(不明白)如何在保持电话应用程序运行的同时调用我的任务(加载MainPage.xaml)。

Anyone can tell how i get my task started asynchronously? 任何人都可以告诉我如何异步启动我的任务?

You can use async and await where you need to call the task. 您可以使用async并在需要调用任务的地方等待。

private async void yourFunction(object sender, RoutedEventArgs e)
{
    string result = await RequestDataTask();
}

http://msdn.microsoft.com/it-it/library/hh191443.aspx http://msdn.microsoft.com/it-it/library/hh191443.aspx

But if you want to call an async method inside a constructor use something like this: 但是,如果要在构造函数中调用异步方法,请使用以下代码:

public partial class App
{
  private readonly Task _initializingTask;

  public App()
  {
    _initializingTask = Init();
  }

  private async Task Init()
  {
    /*
    Initialization that you need with await/async stuff allowed
    */
    string result = await RequestDataTask();
  }
}

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

相关问题 从Windows Phone 8中的App.Xaml.cs到达MainPage.Xaml.cs单击处理程序函数 - Reaching MainPage.Xaml.cs click handler functions from App.Xaml.cs in windows phone 8 Windows Phone 8 - MVVM ViewModels和App.xaml.cs - Windows Phone 8 - MVVM ViewModels and App.xaml.cs Windows Phone-通过app.xaml.cs的NavigationService - Windows Phone - NavigationService through app.xaml.cs 如何从Windows Phone App中的代码动态实例化App.xaml.cs中的集合对象? - How to instantiate object of a collection in App.xaml.cs dynamically from code in Windows Phone App? Windows Phone 8无法从App.xaml.cs调用WCF服务 - Windows Phone 8 unable to call a WCF Service from App.xaml.cs windows 8 App从App.xaml.cs访问页面方法 - windows 8 App accessing page method from App.xaml.cs 在Windows应用商店中从app.xaml.cs显示MessageDialog - Show MessageDialog from app.xaml.cs in Windows Store app 从 App.xaml.cs WPF 依次启动两个窗口 - Start two windows in sequence from App.xaml.cs WPF Windows Store是否从App.xaml.cs导航? - Windows Store Navigating from App.xaml.cs? 如何在通用应用程序/ Windows 8.1和Windows Phone的所有页面上的App.xaml.cs上绑定属性? - How to bind a property on App.xaml.cs on all pages of universal app / Windows 8.1 and Windows Phone?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM