从异步方法获取价值?

时间:2016-01-31 17:23:23

标签: c# win-universal-app

我想从send操作中获取返回值,该值是一个字符串,并在公共MainPage()部分中使用它。我试过这种方式,机器人不起作用。知道如何从send()方法中获取这个值吗?

public sealed partial class MainPage : Page
{

    public MainPage()
    {
        this.InitializeComponent();

        string stringData = "";

        stringData = "aktion=getBenutzer&name=" + Login.getBenutzername();
        //send(stringData);

        //textBlock1.Text = getContentOfSendOperation();



        button.Foreground = Einstellungen.getBrush();
        button1.Foreground = Einstellungen.getBrush();
        button2.Foreground = Einstellungen.getBrush();


        stringData = "aktion=getMitarbeiterListe";

        //string mitarbeiterListe = getContentOfSendOperation();
        var task = send(stringData);
        string mitarbeiterListe = task.Result;
        textBlock1.Text = mitarbeiterListe;
        //comboBox.Items.Add()



    }



    public Frame globalFrame { get { return _mainFrame; } }



    private void button_Click(object sender, RoutedEventArgs e)
    {
        _mainFrame.Navigate(typeof(Datenbank));
    }

    public async Task<String> send(string stringData)
    {
        System.Net.Http.HttpClient oHttpClient = new System.Net.Http.HttpClient();

        Uri uri = new Uri("*********");
        oHttpClient.DefaultRequestHeaders.UserAgent.ParseAdd("moralsKite/DesktopTestClient");

        var request = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Post, uri);
        request.Content = new StringContent(stringData, Encoding.UTF8, "application/x-www-form-urlencoded");
        var reponse = await oHttpClient.SendAsync(request);

        if (reponse.IsSuccessStatusCode)
        {
            return "??";
            //return await reponse.Content.ReadAsStringAsync();



        }
        return "!!";
    }

    private void button1_Click_1(object sender, RoutedEventArgs e)
    {
        _mainFrame.Navigate(typeof(Einstellungen));
    }

    private void button2_Click_1(object sender, RoutedEventArgs e)
    {
        _mainFrame.Navigate(typeof(Ueber));
    }
}


}

2 个答案:

答案 0 :(得分:1)

您无法在构造函数中运行异步操作并等待它。在你的例子中,任务可以运行更长时间(根据信号等而变化),类的构造函数应该很快。更好地订阅其中一个页面的活动,比如 Loaded ,并将你的工作放在那里:

public MainPage()
{
    this.InitializeComponent();
    // rest of code
    this.Loaded += MainPage_Loaded;
}

private async void MainPage_Loaded(object sender, RoutedEventArgs e)
{
    // Following Peter Torr's comment - Loaded event can be fired multiple times 
    // for example once you navigate back to the page
    this.Loaded -= MainPage_Loaded; // deregister from event if you want to run it once
    stringData = "aktion=getMitarbeiterListe";
    // in your send method uncomment the line:
    // return await reponse.Content.ReadAsStringAsync();
    // then it will return asynchronously the content as string and can be used like this:
    string mitarbeiterListe = await send(stringData);
}

事件可以是异步然后不应该出现问题,您也可以为用户实现在后台加载某些内容的信息。

答案 1 :(得分:0)

而不是:

var task = send(stringData);
        string mitarbeiterListe = task.Result;

使用

string mitarbeiterListe = await send(stringData);

制作调用发送数据异步的方法。 我不建议在构造函数中调用数据收集。