2016-01-11 82 views
0

需要幫助..這是我的第一個應用程序。我想用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()); 
     } 
    } 
+1

[同步調用異步方法]的可能的複製(http://stackoverflow.com/questions/22628087/calling-async-method-synchronously) – BlueTrin

+0

嘗試http://stackoverflow.com/questions/22628087/同步調用異步方法 – BlueTrin

+3

您是否在將私有方法更改爲公開時​​將其更改爲公共方法? –

回答

0

轉換haePostimerkitPilvesta()來haePostimerkitPilvestaAsync()返回一個任務和在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()); 
    } 
} 
+0

非常感謝,這工作! – user5445811

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 
    } 
} 
相關問題