0

Need help.. This is my first application ever. I'm trying to populate GridView with JSON data. code below works, but now I'm trying to move the async private void haePostimerkitPilvesta() and public static string ReadStreamAsString(Stream input) method code blocks from MainPage.xaml.cs to other .cs file with no luck.. How should I write the code so that I can call it correctly? without async method I am able to do the same, but then the code does not work.

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());
        }
    }
Loofer
  • 6,841
  • 9
  • 61
  • 102
  • 1
    Possible duplicate of [Calling async method synchronously](http://stackoverflow.com/questions/22628087/calling-async-method-synchronously) – BlueTrin Jan 11 '16 at 16:44
  • Try http://stackoverflow.com/questions/22628087/calling-async-method-synchronously – BlueTrin Jan 11 '16 at 16:45
  • 3
    Are you changing the private method to public when you move it? – David Watts Jan 11 '16 at 16:47
  • 2
    You say "no luck". Is that a runtime error? Is it throwing a `NoLuckException` at runtime? Or is it a compiler error, `No Luck in line 115`? Do you mean you were unable to paste the text of the methods into the new file, due to some indefinable probabilistic misadventure? Or do you just have a pervasive sense of looming misfortune? Please be specific about where your luck is running out, and what the exact symptoms are. – 15ee8f99-57ff-4f92-890c-b56153 Jan 11 '16 at 17:38
  • if i just copy that code block to xx.cs and change the private method to public, then GridViewPostimerkit.ItemsSource says error: the name does not exist in the current context. – user5445811 Jan 11 '16 at 18:10
  • @user5445811 Try prefixing the method name with the name of the class it is now a member of. If the new class is `xx`, try `xx.haePostimerkitPilvesta()`. Also, if you do not have an instance of `xx` handy, BOTH methods must now be static. – 15ee8f99-57ff-4f92-890c-b56153 Jan 11 '16 at 18:15

2 Answers2

0

It looks like you moved the methods to a different class, but you're still calling them as if they were still a method of the same class.

By the way you'll also need to make haePostimerkitPilvesta() static now, if you don't have an instance of the new class handy to call it on.

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
    }
}
0

Convert haePostimerkitPilvesta() to haePostimerkitPilvestaAsync() that returns a Task and set GridViewPostimerkit.ItemsSource in MainPage_Loaded() event. Now you should be able to move methods to other file as well.

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());
    }
}
Gaurav
  • 195
  • 9