使用 INotifyPropertyChanged 在 WPF 中绑定系统相关信息

本文关键字:系统 信息 绑定 INotifyPropertyChanged WPF 使用 | 更新日期: 2023-09-27 17:56:12

我希望使用 MVVM 绑定与 INotifyPropertyChanged 列出 WPF XAML 中的临时文件

视图模型是 TempViewModel.cs

class TempViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    public List<FileInfo> CacheFiles
    {
        get
        {
            string path = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache);
            System.IO.DirectoryInfo di = new DirectoryInfo(path);
            return di.GetFiles().ToList();
        }
    }
}

临时文件夹中的文件可能会不时变化。每当使用 XAML UI 中的 Temp 文件夹中的文件更新INotifyPropertyChanged时,我需要自动更新它

如何在 XMAL 中绑定它?

MainWindow.xaml

<Window x:Class="Binding.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ListBox Name="LstProduct" ItemsSource="{Binding CacheFiles}" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Path=FullName}"/>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>

MainWindow.xaml.cs

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        Loaded += new RoutedEventHandler(MainWindow_Loaded);
    }
    void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        this.DataContext = new TempViewModel();
    }
}

我无法在 UI 中获取更新的文件名列表。请协助我,每当在临时文件夹中添加或删除文件时,如何更新UI?

使用 INotifyPropertyChanged 在 WPF 中绑定系统相关信息

答案是FileSystemWatcher

您应该修改您的 TempViewModel.cs

class Cleaner : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private List<KeyValuePair<string, ulong>> _memoryPool = new List<KeyValuePair<string, ulong>>();
    public List<KeyValuePair<string, ulong>> MemoryPool
    {
        get { return _memoryPool; }
        set
        {
            _memoryPool = value;
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("MemoryPool"));
        }
    }

    private void watch()
    {
        FileSystemWatcher watcher = new FileSystemWatcher();
        watcher.Path = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache);
        watcher.NotifyFilter = NotifyFilters.LastWrite;
        watcher.Filter = "*.*";
        watcher.Changed += new FileSystemEventHandler(OnChanged);
        watcher.EnableRaisingEvents = true;
    }
    private void OnChanged(object source, FileSystemEventArgs e)
    {
        UpdateCollection();
    }
    private void UpdateCollection()
    {
        string path = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache);;
        System.IO.DirectoryInfo di = new DirectoryInfo(path);
        CacheFiles = di.GetFiles().ToList();
    }
    public Cleaner()
    {
        UpdateCollection();
        watch();
    }
}

使用FileSystemWatcher(有关如何使用它,请参阅文档)并订阅其事件之一。在该处理程序中,使用 "CacheFiles" 调用 PropertyChanged 事件,以向 UI 发出重新获取 CacheFiles 属性的信号。

您当然可以通过ObservableCollection并手动添加/删除条目来改进这一点,但我描述的方式应该有效。

您应该使用 FileSystemWatcher 和 Changed 事件。例如,当文件系统观察器引发 Changed 事件时,事件处理程序应通过引发 PropertyChanged 来更改 CacheFile 属性