在回调方法中,我正在尝试获取文本框的文本属性,代码如下:
但这是异步运行的。同步版本如下:
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);
}
myReq.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback **参数??**), myReq);
- Lemontongs