Windows Phone 7上的Dispatcher.Invoke()? [英] Dispatcher.Invoke() on Windows Phone 7?

查看:63
本文介绍了Windows Phone 7上的Dispatcher.Invoke()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在回调方法中,我尝试获取文本框的文本属性,如下所示:

In a callback method I am attempting to get the text property of a textBox like this:

string postData = tbSendBox.Text;

但是因为它没有在UI线程上执行,所以给了我一个跨线程异常。

But because its not executed on the UI thread it gives me a cross-thread exception.

我想要这样的东西:

Dispatcher.BeginInvoke(() =>
{
    string postData = tbSendBox.Text;
});

但这是异步运行的。同步版本为:

But this runs asynchronously. The synchronous version is:

Dispatcher.Invoke(() =>
{
    string postData = tbSendBox.Text;
});

但是Windows Phone不存在Dispatcher.Invoke()。有等同的东西吗?有其他方法吗?

But Dispatcher.Invoke() does not exist for the Windows Phone. Is there something equivalent? Is there a different approach?

这里是整个函数:

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


推荐答案

否,您可以仅访问异步之一。为什么要同步,因为您位于用户界面的另一个线程上?

No you are right you can access only to the async one. Why do you want sync since you are on a different thread of the UI one?

Deployment.Current.Dispatcher.BeginInvoke(() =>
       {
            string postData = tbSendBox.Text;
        });

这篇关于Windows Phone 7上的Dispatcher.Invoke()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆