繁体   English   中英

从异步方法获取价值?

[英]Get Value from async Method?

我想从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));
    }
}


}

您不能运行异步操作并在构造函数中等待它。 在您的示例中,任务可以运行更长的时间(根据信号等而变化),类的构造函数应该很快。 最好订阅该页面的事件之一,例如已加载,然后将您的工作放在那里:

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);
}

事件可以是异步的,那么应该不存在问题,您还可以为用户实现一个信息,即某些内容正在后台加载。

代替 :

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

采用

string mitarbeiterListe = await send(stringData);

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

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM