如何从其他类文件中调用异步方法

时间:2016-01-11 16:43:20

标签: c# uwp

需要帮助..这是我第一次申请。我试图用JSON数据填充GridView。下面的代码有效,但现在我试图将async private void haePostimerkitPilvesta()public static string ReadStreamAsString(Stream input)方法代码块从MainPage.xaml.cs移动到其他.cs文件而没有运气..我应该如何编写代码,以便我可以正确调用它?没有异步方法我也可以这样做,但代码不起作用。

void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        haePostimerkitPilvesta();
    }

    async private void haePostimerkitPilvesta()
    {

        Uri address = new Uri("xxx.json"); //public link of our file
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address);
        WebResponse response = await request.GetResponseAsync();
        Stream stream = response.GetResponseStream();
        string content = ReadStreamAsString(stream);
        GridViewPostimerkit.ItemsSource = JsonConvert.DeserializeObject(content, typeof(List<Postimerkit>));

    }

    public static string ReadStreamAsString(Stream input)
    {
        byte[] buffer = new byte[16 * 1024];
        using (MemoryStream ms = new MemoryStream())
        {
            int read;
            while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
            {
                ms.Write(buffer, 0, read);
            }
            return Encoding.UTF8.GetString(ms.ToArray(), 0, ms.ToArray().Count());
        }
    }

2 个答案:

答案 0 :(得分:0)

看起来您将方法移动到了另一个类,但您仍然在调用它们,就好像它们仍然是同一个类的方法一样。

顺便说一下,如果您没有新类的实例可以方便地调用它,那么您现在还需要使haePostimerkitPilvesta()静态。

public class A {
    public void F() {
        //  Compiler says "The name 'M' does not exist in current context."
        //  Current context is A. There is no M in A. M who? M in B or M in C?
        //  You must specify what M you mean! The compiler plays no guessing games.
        M();

        //  This will compile.
        //  Now the compiler knows where M lives and can find him.
        B.M();
    }
}

public class B {
    public static void M() {
        //  Do stuff
    }
}

public class C {
    public static void M() {
        //  Do TOTALLY DIFFERENT stuff
    }
}

答案 1 :(得分:0)

将haePostimerkitPilvesta()转换为haePostimerkitPilvestaAsync(),它返回一个Task并在MainPage_Loaded()事件中设置GridViewPostimerkit.ItemsSource。现在你应该能够将方法移动到其他文件。

async void MainPage_Loaded(object sender, RoutedEventArgs e)
{
    GridViewPostimerkit.ItemsSource = await haePostimerkitPilvestaAsync();
}

public static async Task<List<Postimerkit>> haePostimerkitPilvestaAsync()
{

    Uri address = new Uri("xxx.json"); //public link of our file
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address);
    WebResponse response = await request.GetResponseAsync();
    Stream stream = response.GetResponseStream();
    string content = ReadStreamAsString(stream);
    return JsonConvert.DeserializeObject(content, typeof(List<Postimerkit>));
}

public static string ReadStreamAsString(Stream input)
{
    byte[] buffer = new byte[16 * 1024];
    using (MemoryStream ms = new MemoryStream())
    {
        int read;
        while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
        {
            ms.Write(buffer, 0, read);
        }
        return Encoding.UTF8.GetString(ms.ToArray(), 0, ms.ToArray().Count());
    }
}