I want to bind listbox in windows phone app 8.1. I am using following code but It raise an error :
Additional information: The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD)).
I am using the following code
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);
}
}
}
and my xaml page :
<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>
You're mutating something bound to the UI on a non-UI thread. You'll need to marshall this on to the UI thread.
You could use a helper method on yuor view model to get stuff back on to the UI thread, so something like:
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);
}
}
}
This could then be called something like:
OnUIThread(() =>
{
lmylistbox.ItemsSource = list;
});
Well, your code is probably not in UI Thread. Try to change your code to that:
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);
}
}
}
await Windows.ApplicationModel.Core.CoreApplication
.MainView
.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
mylistbox.ItemsSource = list;
});
User contributions licensed under CC BY-SA 3.0