2015-10-05 51 views
0

我想在Windows Phone應用程序8.1中綁定列表框。我正在使用下面的代碼,但它引發了一個錯誤:如何在Windows Phone 8.1中綁定列表框

附加信息:應用程序調用了針對不同線程進行編組的接口。 (來自HRESULT的異常:0x8001010E(RPC_E_WRONG_THREAD))。

我使用下面的代碼

private void getResponse(IAsyncResult result) 
    { 
     HttpWebRequest request = result.AsyncState as HttpWebRequest; 
     if (request != null) 
     { 
      try 
      { 
       WebResponse response = request.EndGetResponse(result); 
       Stream streamResponse = response.GetResponseStream(); 
       StreamReader streamRead = new StreamReader(streamResponse); 
       string read = streamRead.ReadToEnd(); 

       deserializeJsonString(read); 
       List<lstData> list = new List<lstData>(); 
       lstData lstObj = new lstData(); 
       foreach (var itm in childList.AppData) 
       { 
        lstObj.app_name = Convert.ToString(itm.app_name); 
        lstObj.app_url = Convert.ToString(itm.app_url); 
        list.Add(lstObj); 

       } 

       mylistbox.ItemsSource = list; 

      } 
      catch (WebException e) 
      { 
       // Debug.WriteLine("Exception in getResponse" + e); 
      } 


     } 
    } 

和我的XAML頁面:

<ListBox x:Name="mylistbox" Margin="0,234,0,0"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <StackPanel Orientation="Horizontal"> 
       <TextBlock Text="{Binding app_name}" FontSize="45" Margin="0,10" Width="204"></TextBlock> 
       <TextBlock Text="{Binding app_url}" FontSize="35" Width="246" Margin="0,10"/> 
      </StackPanel> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

回答

2

你突變綁定到UI在非UI線程的東西。你需要將這個放到UI線程中。

你可以使用在上你的視圖模型一個輔助方法得到的東西回來到UI線程,所以像:

protected delegate void OnUIThreadDelegate(); 
    /// <summary> 
    /// Allows the specified delegate to be performed on the UI thread. 
    /// </summary> 
    /// <param name="onUIThreadDelegate">The delegate to be executed on the UI thread.</param> 
    protected static void OnUIThread(OnUIThreadDelegate onUIThreadDelegate) 
    { 
     if (onUIThreadDelegate != null) 
     { 
      if (Deployment.Current.Dispatcher.CheckAccess()) 
      { 
       onUIThreadDelegate(); 
      } 
      else 
      { 
       Deployment.Current.Dispatcher.BeginInvoke(onUIThreadDelegate); 
      } 
     } 
    } 

此類型可稱爲是這樣的:

OnUIThread(() => 
{ 
    lmylistbox.ItemsSource = list; 
}); 
0

那麼,你的代碼可能不在UI線程中。 嘗試你的代碼更改爲:

private void getResponse(IAsyncResult result) 
{ 
    HttpWebRequest request = result.AsyncState as HttpWebRequest; 
    if (request != null) 
    { 
     try 
     { 
      WebResponse response = request.EndGetResponse(result); 
      Stream streamResponse = response.GetResponseStream(); 
      StreamReader streamRead = new StreamReader(streamResponse); 
      string read = streamRead.ReadToEnd(); 
      deserializeJsonString(read); 
      List<lstData> list = new List<lstData>(); 
      lstData lstObj = new lstData(); 
      CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,() => { 
       foreach (var itm in childList.AppData) 
       { 
       lstObj.app_name = Convert.ToString(itm.app_name); 
       lstObj.app_url = Convert.ToString(itm.app_url); 
       list.Add(lstObj); 

       } 
       mylistbox.ItemsSource = list; 
      }); 
     } 
     catch (WebException e) 
     { 
      // Debug.WriteLine("Exception in getResponse" + e); 
     } 


    } 
} 
0
await Windows.ApplicationModel.Core.CoreApplication 
            .MainView 
            .CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,() => 
            { 
             mylistbox.ItemsSource = list; 
            }); 
+0

謝謝Clerissa serrao .Resolved我的問題。 –

相關問題