簡體   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