加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

windows-phone-7 – Windows Phone 7上的Dispatcher.Invoke()?

发布时间:2020-12-14 02:09:23 所属栏目:Windows 来源:网络整理
导读:在回调方法中,我试图获取textBox的text属性,如下所示: string postData = tbSendBox.Text; 但是因为它没有在UI线程上执行,所以它给了我一个跨线程的异常. 我想要这样的东西: Dispatcher.BeginInvoke(() ={ string postData = tbSendBox.Text;}); 但这是异
在回调方法中,我试图获取textBox的text属性,如下所示:

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,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;
        });

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读