如何将结果返回到 Windows Phone 中的 UI 线程?

发布于 2025-01-08 16:13:10 字数 850 浏览 0 评论 0原文

有谁知道如何将结果返回到 UI 线程的好主意? 我写了这段代码,但是它会编译错误,因为它不能以异步方式返回“img”。

public byte[] DownloadAsync2(Uri address)
{
    byte[] img;
    byte[] buffer = new byte[4096];

    var wc = new WebClient();

    wc.OpenReadCompleted += ((sender, e) =>
    {
        using (MemoryStream memoryStream = new MemoryStream())
        {
            int count = 0;
            do
            {
                count = e.Result.Read(buffer, 0, buffer.Length);
                memoryStream.Write(buffer, 0, count);
            } while (count != 0);

            Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    if (e.Error == null) img = memoryStream.ToArray();
                });
        }
    }
    );
    wc.OpenReadAsync(address);

    return img; //error : Use of unassigned local variable 'img'
}

Does anyone know great idea How to return result to UI thread ?
I wrote this code, but It will be compile error because it can't return "img" in async.

public byte[] DownloadAsync2(Uri address)
{
    byte[] img;
    byte[] buffer = new byte[4096];

    var wc = new WebClient();

    wc.OpenReadCompleted += ((sender, e) =>
    {
        using (MemoryStream memoryStream = new MemoryStream())
        {
            int count = 0;
            do
            {
                count = e.Result.Read(buffer, 0, buffer.Length);
                memoryStream.Write(buffer, 0, count);
            } while (count != 0);

            Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    if (e.Error == null) img = memoryStream.ToArray();
                });
        }
    }
    );
    wc.OpenReadAsync(address);

    return img; //error : Use of unassigned local variable 'img'
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦在夏天 2025-01-15 16:13:10

将您的方法更改为:

public void DownloadAsync2(Uri address, Action<byte[]> callback, Action<Exception> exception)
{
    var wc = new WebClient();

    wc.OpenReadCompleted += ((sender, e) =>
    {
        using (MemoryStream memoryStream = new MemoryStream())
        {
            int count = 0;
            do
            {
                count = e.Result.Read(buffer, 0, buffer.Length);
                memoryStream.Write(buffer, 0, count);
            } while (count != 0);

            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                if (e.Error == null) callback(memoryStream.ToArray());
                else exception(e.Error);
            });
        }
    }
    );
    wc.OpenReadAsync(address);
}

用法:

DownloadAsync2(SomeUri, (img) =>
{
    // this line will be executed when image is downloaded, 
    // img - returned byte array
},
(exception) =>
{
    // handle exception here
});

Or (没有 lambda 表达式的旧式代码):

DownloadAsync2(SomeUri, LoadCompleted, LoadFailed);

// And define two methods for handling completed and failed events

private void LoadCompleted(byte[] img)
{
    // this line will be executed when image is downloaded, 
    // img - returned byte array
}

private void LoadFailed(Exception exception)
{
    // handle exception here
}

Change your method to:

public void DownloadAsync2(Uri address, Action<byte[]> callback, Action<Exception> exception)
{
    var wc = new WebClient();

    wc.OpenReadCompleted += ((sender, e) =>
    {
        using (MemoryStream memoryStream = new MemoryStream())
        {
            int count = 0;
            do
            {
                count = e.Result.Read(buffer, 0, buffer.Length);
                memoryStream.Write(buffer, 0, count);
            } while (count != 0);

            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                if (e.Error == null) callback(memoryStream.ToArray());
                else exception(e.Error);
            });
        }
    }
    );
    wc.OpenReadAsync(address);
}

Usage:

DownloadAsync2(SomeUri, (img) =>
{
    // this line will be executed when image is downloaded, 
    // img - returned byte array
},
(exception) =>
{
    // handle exception here
});

Or (old-style code without lambda expressions):

DownloadAsync2(SomeUri, LoadCompleted, LoadFailed);

// And define two methods for handling completed and failed events

private void LoadCompleted(byte[] img)
{
    // this line will be executed when image is downloaded, 
    // img - returned byte array
}

private void LoadFailed(Exception exception)
{
    // handle exception here
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文