我正在使用PHP腳本來生成xml文件。我想在我的Windows Phone 8應用程序中將XML文件中的數據寫入Textblock
。 當我調試時,我得到一個錯誤,這是我的catch
沒有抓到。出現錯誤的打印屏幕:http://i811.photobucket.com/albums/zz38/JelleK1996/errorxml1_zps20df0a45.png 出了什麼問題?將在線XML文件寫入文本框
這是我的代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Xml;
using System.IO;
using System.Xml.Linq;
using System.Diagnostics;
namespace xml1
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
// Sample code to localize the ApplicationBar
//BuildLocalizedApplicationBar();
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
try
{
HttpWebRequest request = WebRequest.Create("http://cocktailpws.net23.net/requests/get_cocktail.php?id=10") as HttpWebRequest;
request.BeginGetResponse(r =>
{
var reponse = request.EndGetResponse(r);
//XDocument xmlDoc = XDocument.Load(reponse.GetResponseStream());
XmlReader reader = XmlReader.Create(reponse.GetResponseStream());
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element: // Het knooppunt is een element.
Console.Write("<" + reader.Name);
Console.WriteLine(">");
break;
case XmlNodeType.Text: //De tekst in elk element weergeven.
tb1.Text = tb1.Text + reader.Value + "\r\n";
Console.WriteLine(reader.Value);
break;
case XmlNodeType.EndElement: //Het einde van het element weergeven.
Console.Write("</" + reader.Name);
Console.WriteLine(">");
break;
}
}
}, null);
}
catch (Exception myExc)
{
Console.WriteLine(myExc.Message);
}
}
}
}
可能的答案http://stackoverflow.com/questions/13605166/windows-phone-8-threading-invalid-cross-thread-access – qwr
@qwr呃好的。但我沒有看到我應該在代碼中添加哪些內容,以及如何......是的,我是新來的C# – JelleKerkstra
檢查答案。但對於錯誤有很多答案專門討論它,所以我只回答只考慮你的情況 – qwr