繁体   English   中英

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

[英]How to Bind Listbox in 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>

您正在更改非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;
});

好吧,您的代码可能不在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);
        }


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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM