WPF中使用INotifyPropertyChanged绑定系统相关信息
Bind System related information in WPF using INotifyPropertyChanged
我希望使用 MVVM 绑定 和 在 WPF XAML 中列出 临时文件 INotifyPropertyChanged
.
视图模型是 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
中使用 INotifyPropertyChanged
在 Temp 文件夹中更新文件
如何在 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?
使用 FileSystemWatcher
(see the docs for how to use it) 并订阅其中一个事件。在该处理程序中,使用 "CacheFiles"
调用 PropertyChanged
事件以向 UI 发送信号以重新获取 CacheFiles
属性.
您当然可以通过 ObservableCollection
和手动 adding/removing 条目对此进行改进,但我描述的方式应该有效。
您应该使用 FileSystemWatcher 和 Changed event。例如,当 FileSystemWatcher 引发 Changed 事件时,您的事件处理程序应该通过引发 PropertyChanged
来更改 CacheFile 属性
答案是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();
}
}
我希望使用 MVVM 绑定 和 在 WPF XAML 中列出 临时文件 INotifyPropertyChanged
.
视图模型是 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
中使用INotifyPropertyChanged
在 Temp 文件夹中更新文件
如何在 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?
使用 FileSystemWatcher
(see the docs for how to use it) 并订阅其中一个事件。在该处理程序中,使用 "CacheFiles"
调用 PropertyChanged
事件以向 UI 发送信号以重新获取 CacheFiles
属性.
您当然可以通过 ObservableCollection
和手动 adding/removing 条目对此进行改进,但我描述的方式应该有效。
您应该使用 FileSystemWatcher 和 Changed event。例如,当 FileSystemWatcher 引发 Changed 事件时,您的事件处理程序应该通过引发 PropertyChanged
来更改 CacheFile 属性答案是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();
}
}