【发布时间】:2012-11-30 21:05:56
【问题描述】:
在回调方法中,我试图像这样获取文本框的文本属性:
string postData = tbSendBox.Text;
但是因为它没有在 UI 线程上执行,所以它给了我一个跨线程异常。
我想要这样的东西:
Dispatcher.BeginInvoke(() =>
{
string postData = tbSendBox.Text;
});
但这是异步运行的。同步版本为:
Dispatcher.Invoke(() =>
{
string postData = tbSendBox.Text;
});
但 Windows Phone 不存在 Dispatcher.Invoke()。有没有等价的东西?有不同的方法吗?
这是整个函数:
public void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
// End the operation
Stream postStream = request.EndGetRequestStream(asynchronousResult);
string postData = tbSendBox.Text;
// Convert the string into a byte array.
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
// Write to the request stream.
postStream.Write(byteArray, 0, postData.Length);
postStream.Close();
// Start the asynchronous operation to get the response
request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
}
【问题讨论】:
标签: windows-phone-7 dispatcher