以下のスクリーンショットを参照すると、進行状況バーのダウンロードを表す次のビューとビューモデルのセットアップがあります。このダウンロードのコレクションに複数のダウンロードを追加すると、ダウンロード 1 がダウンロード 2 の Progress() パブリック プロパティを上書きするようです。
以下の例は、ダウンロード 1 が 1MB をダウンロードしており、最初に終了し、5MB をダウンロードしているダウンロード 2 と比較した場合です。ダウンロード 1 が終了すると、ダウンロード 2 (以下の hello2 の例) の進行状況バーはそこで停止しますが、バイトは 5MB に達するまでバックグラウンドでダウンロードされ続けます。
ダウンロード 1 がダウンロード 2 のプログレス バー UI に干渉しないようにコードを変更するにはどうすればよいですか。Progress() を非公開に変更しようとしましたが、両方の進行状況バー UI が表示されません
意見
<ItemsControl Name="MyItemsControl" ItemsSource="{Binding GameDownloads}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid DockPanel.Dock="Right">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding GameName}" />
<ProgressBar Grid.Row="0" Grid.Column="1" Minimum="0" Maximum="100" Value="{Binding Progress, Mode=OneWay}" />
<TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding StatusText}" />
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
DownloadAppViewModel
Public Class DownloadAppViewModel
Inherits ViewModelBase
Private ReadOnly WC As WebClient
Public Sub New(ByVal Name As String, ByVal URL As String, ByVal FileName As String)
_gameName = Name
WC = New WebClient
AddHandler WC.DownloadFileCompleted, AddressOf DownloadCompleted
AddHandler WC.DownloadProgressChanged, AddressOf DownloadProgressChanged
WC.DownloadFileAsync(New Uri(URL), FileName)
End Sub
Private Sub DownloadCompleted(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs)
End Sub
Private Sub DownloadProgressChanged(ByVal sender As [Object], ByVal e As DownloadProgressChangedEventArgs)
If _totalSize = 0 Then
_totalSize = e.TotalBytesToReceive
End If
DownloadedSize = e.BytesReceived
End Sub
Private _gameName As String
Public Property GameName() As String
Get
Return _gameName
End Get
Set(ByVal value As String)
_gameName = value
Me.OnPropertyChanged("GameName")
End Set
End Property
Public ReadOnly Property StatusText() As String
Get
If _downloadedSize < _totalSize Then
Return String.Format("Downloading {0} MB of {1} MB", _downloadedSize, _totalSize)
End If
Return "Download completed"
End Get
End Property
Private _totalSize As Long
Private Property TotalSize() As Long
Get
Return _totalSize
End Get
Set(ByVal value As Long)
_totalSize = value
OnPropertyChanged("TotalSize")
OnPropertyChanged("Progress")
OnPropertyChanged("StatusText")
End Set
End Property
Private _downloadedSize As Long
Private Property DownloadedSize() As Long
Get
Return _downloadedSize
End Get
Set(ByVal value As Long)
_downloadedSize = value
OnPropertyChanged("DownloadedSize")
OnPropertyChanged("Progress")
OnPropertyChanged("StatusText")
End Set
End Property
Public ReadOnly Property Progress() As Double
Get
If _totalSize <> 0 Then
Return 100.0 * _downloadedSize / _totalSize
Else
Return 0.0
End If
End Get
End Property
End Class