簡體   English   中英

WPF計時器和UI更新問題

[英]WPF Timer and UI Update Issue

我的應用程序出現問題,更改自定義屬性后,它無法正確更新UI,該自定義屬性使用DisplayMemberBinding="{Binding property}"綁定到GridView Column。

XAML:

<ListView x:Name="downloadList" HorizontalAlignment="Left" Height="293" Margin="0,126,0,0" VerticalAlignment="Top" Width="810" Grid.IsSharedSizeScope="True" MouseDoubleClick="DownloadList_MouseDoubleClick">
    <ListView.View>
        <GridView x:Name="DownloadGridView">
            <GridViewColumn x:Name="c_filename" Header="File name" Width="{Binding Source={x:Static p:Settings.Default}, Path=downloadList_fileName_Width, Mode=TwoWay}" DisplayMemberBinding="{Binding fileName}" />
            <GridViewColumn x:Name="c_size" Header="Size" Width="{Binding Source={x:Static p:Settings.Default}, Path=downloadList_size_Width, Mode=TwoWay}" DisplayMemberBinding="{Binding formattedFileSize}" />
            <GridViewColumn x:Name="c_downloaded" Header="Downloaded" Width="{Binding Source={x:Static p:Settings.Default}, Path=downloadList_downloaded_Width, Mode=TwoWay}" DisplayMemberBinding="{Binding sizeProgress}" />
            <GridViewColumn x:Name="c_status" Header="Status" Width="{Binding Source={x:Static p:Settings.Default}, Path=downloadList_status_Width, Mode=TwoWay}" DisplayMemberBinding="{Binding Status}"/>
        </GridView>
    </ListView.View>
</ListView>

這是我的帶有屬性的自定義類:

using System;
using System.Runtime.CompilerServices;
using System.Text;
using System.ComponentModel;

namespace DownloadManager
{
public class DownloadItem : INotifyPropertyChanged
{
    private string _filepath;
    public string filePath
    {
        get { return _filepath; }
        set
        {
            _filepath = value;
            RaisePropertyChanged();
        }
    }

    private int _sizeprogress;
    public int sizeProgress
    {
        get { return _sizeprogress; }
        set
        {
            _sizeprogress = value;
            RaisePropertyChanged();
        }
    }

// and so on...

    public event PropertyChangedEventHandler PropertyChanged;
    public void RaisePropertyChanged(
        [CallerMemberName] string caller = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(caller));
        }
    }

}
}

計時器:經過編輯以顯示我正在嘗試做的真實示例

System.Windows.Threading.DispatcherTimer updateTimer = new System.Windows.Threading.DispatcherTimer();

updateTimer.Tick += new EventHandler(updateTimer_Tick);
updateTimer.Interval = new TimeSpan(0, 0, 1);


private void updateTimer_Tick(object sender, EventArgs e)
{
    foreach (DownloadItem item in downloadList.Items)
    {
        long BytesReceived = item.filePath.Length;
        item.sizeProgress = BytesReceived;
    }
}

item.filePath包含要下載的文件的路徑,使用FileStream進行寫入。

我的目標是每秒讀取文件大小並顯示它。

問題: UI,在這種情況下,列綁定到sizeProgress ,僅在第一次滴答中更新一次,然后什么也不更新。 該應用仍然可以正常運行。

而且我真的不知道可能是什么問題。

如果您需要更多信息/代碼,請告訴我。 謝謝。

long BytesReceived = item.filePath.Length;

嗯,那是包含文件路徑的string的長度,而不是文件本身的長度。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM