在回调方法中,我尝试获取文本框的文本属性,如下所示:
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);
}
不,你是对的,你只能访问异步的。既然您位于 UI 线程的不同线程上,为什么需要同步呢?
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
string postData = tbSendBox.Text;
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)