当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - XAML binding to an immutable struct

问题描述:

I have some DownloadOperations in my Windows 8 app. The DownloadOperation has a property Progress which contains Status, TotalBytesToReceiveand BytesReceived. I tried to data-bind to those in my XAML markup, but i got some errors. I then found out that it's impossible to bind to a struct, which BackgroundDownlodProgress is.

The only solutions I found online were to make the struct mutable which is impossible for me because it's not my code.

I tried to wrap the DownloadOperation into a Wrapper class that has class properties for DownloadOperation.Progress.Status, DownloadOperation.Progress.BytesReceived and DownloadOperation.Progress.TotalBytesToReceive.

I would have to notify the system of property changes in ord to update the value in the UI though, and normally this is done in the properties' setter method. I have no setter though, because the underlying struct is read-only.

How can I create a XAML binding to an immutable struct defined by the system?

网友答案:

DownloadOperation has a method called "AttachAsync", which returns an object of type "IAsyncOperationWithProgress<DownloadOperation, DownloadOperation>". You can use this return value to add a method that handles progress events. Every time this progress method is called you can notify your UI that the progress properties have changed. It should look like this:

var operationWithProgress = download.AttachAsync();
operationWithProgress.Progress = (asyncInfo, progressInfo) => {
    //send notify property changed events
}

Disclaimer: I didn't try to run a program using this solution, I was just searching the docs. Please comment if you have any problems with this solution.

分享给朋友:
您可能感兴趣的文章:
随机阅读: