从异步方法分配变量 [英] Assigning variables from async method

查看:195
本文介绍了从异步方法分配变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能叫我异步方法...我想调用该方法,所以我可以在变量的使用方法里面的值。 注意 ViewDetailsAsync 是从Web服务来

I am not able to call my async method... I am wanting to call that method so I can use the values in the variables inside of that method. NOTE: ViewDetailsAsync is coming from a web service.

这是我有:

namespace TilesAndNotifications.Models
{
public class PrimaryTile
{
    public async void GetTileData()
    {
        ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
        string name= string.Empty;
        string description = string.Empty;
        string type= string.Empty;

        var res = await client.ViewDetailsAsync();

        name= res.NameView;
        description = res.DescriptionView;
        type= res.TypeView;
    }

    public string CurrentName { get; set; } = "John Doe";
    public string CurrentDescription { get; set; } = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore.";
    public string CurrentType { get; set; } = "Employee";
}
}

我想什么来完成

public string CurrentName { get; set; } = GetTileData.name;
public string CurrentDescription { get; set; } = GetTileData.description
public string CurrentType { get; set; } = GetTileData.type;

不过,我不确定带来从异步方法的信息......我知道这可能是简陋,但我似乎无法得到它。

But I am unsure as to bring that information from the async method... I know this may be rudimentary, but I can not seem to get it.

推荐答案

您可以设置属性从就好了异步方法:

You can set properties just fine from an async method:

public async Task GetTileDataAsync()
{
  ...
  var res = await client.ViewDetailsAsync();

  CurrentName = res.NameView;
  CurrentDescription = res.DescriptionView;
  CurrentType = res.TypeView;
}

当然,调用code将不得不等待任务从 GetTileDataAsync返回使用这些之前属性。

Of course, the calling code will have to await the task returned from GetTileDataAsync before using those properties.

这篇关于从异步方法分配变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆