简体   繁体   English

如何实现IAsyncOperationWithProgress

[英]How to implement IAsyncOperationWithProgress

I'm porting some custom .NET streams to WINRT. 我正在将一些自定义.NET流移植到WINRT。 Language is C#. 语言是C#。

Is there some example implementation of IAsyncOperationWithProgress? 是否有一些IAsyncOperationWithProgress的示例实现? Since Methods ReadAsync, WriteAsync from Windows.Storage.Streams require them. 由于方法ReadAsync,来自Windows.Storage.Streams的WriteAsync需要它们。 Custom WinRT streams implementations are welcomed also. 自定义WinRT流实现也受到欢迎。

I found some C examples using create_async, but i'm looking to do this in C#, and i cannot find create_async in the Metro framework. 我发现了一些使用create_async的C示例,但我希望在C#中执行此操作,并且我在Metro框架中找不到create_async。

Thanks in advance 提前致谢

Here is an example of using IAsyncOperationWithProgress to display the progress of installing an XAP file programatically. 下面是使用IAsyncOperationWithProgress以编程方式显示安装XAP文件的进度的示例。 I'm pretty new to Win8 development so not sure if it's entirely idiomatic. 我对Win8开发很新,所以不确定它是否完全不是惯用语。

Note the Dispatcher.BeginInvoke to marshall the progress back to the UI thread. 请注意Dispatcher.BeginInvoke将进度编组回UI线程。 Hope it helps: 希望能帮助到你:

private async void InstallApp(string name, Uri uri)
{
    try
    {
        StatusTextBlock.Text = "Installing app";
        var installTask = InstallationManager.AddPackageAsync(name, uri);

        installTask.Progress = (installResult, progress) => Dispatcher.BeginInvoke(() =>
        {
            StatusTextBlock.Text = "Progress: " + progress;
        });

        var result = await installTask;
        StatusTextBlock.Text = "Done: " + result.InstallState.ToString();
    }
    catch (Exception ex)
    {
        StatusTextBlock.Text = "Failed to install: " + ex.Message;
    }
}

For C#, you should check out this //build talk on async programming . 对于C#,您应该查看这个//关于异步编程的构建讨论

If you're using high level C++, you should look at this article about how to do asynchronous operations in the PPL. 如果您使用的高级别C ++,你应该看看这个文章,了解如何在PPL做异步操作。

If you need to implement your own async operation from low level C++, you should look at the WRL::AsyncBase class. 如果需要从低级C ++实现自己的异步操作,则应该查看WRL :: AsyncBase类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM