如何在Windows Phone 8.1中绑定列表框

时间:2015-10-05 14:03:18

标签: c# windows-phone-8.1

我想在windows phone app 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>

3 个答案:

答案 0 :(得分:2)

您在非UI线程上改变绑定到UI的内容。您需要将其编组到UI线程上。

您可以在yuor视图模型上使用辅助方法将内容返回到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;
});

答案 1 :(得分: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);
        }


    }
}

答案 2 :(得分:0)

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