C# 使用INotifyPropertyChanged在WPF中绑定系统相关信息
我希望使用MVVM绑定列出WPF XAML中的临时文件,并将C# 使用INotifyPropertyChanged在WPF中绑定系统相关信息,c#,wpf,xaml,mvvm,inotifypropertychanged,C#,Wpf,Xaml,Mvvm,Inotifypropertychanged,我希望使用MVVM绑定列出WPF XAML中的临时文件,并将INotifyPropertyChanged 视图模型为TempViewModel.cs class TempViewModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; public List<FileInfo> CacheFiles { get
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();
}
}
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Loaded += new RoutedEventHandler(MainWindow_Loaded);
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
this.DataContext = new TempViewModel();
}
}
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();
}
}
MainWindow.xaml.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();
}
}
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Loaded += new RoutedEventHandler(MainWindow_Loaded);
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
this.DataContext = new TempViewModel();
}
}
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();
}
}
我无法在UI中获取更新的文件名列表。在临时文件夹中添加或删除文件时,请帮助我如何更新UI?使用
FileSystemWatcher
()并订阅其中一个事件。在该处理程序中,使用“CacheFiles”
调用PropertyChanged
事件,向UI发出重新获取CacheFiles
属性的信号
您当然可以使用
ObservableCollection
并手动添加/删除条目来改进这一点,但我所描述的方法应该是可行的。您应该使用FileSystemWatcher和。例如,当FileSystemWatcher引发已更改事件时,事件处理程序应使用引发属性更改来更改CacheFile属性。答案是FileSystemWatcher
您应该修改您的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();
}
}
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Loaded += new RoutedEventHandler(MainWindow_Loaded);
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
this.DataContext = new TempViewModel();
}
}
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();
}
}
类清理器:INotifyPropertyChanged
{
公共事件属性更改事件处理程序属性更改;
私有列表_memoryPool=新列表();
公共列表内存池
{
获取{return\u memoryPool;}
设置
{
_memoryPool=值;
如果(PropertyChanged!=null)PropertyChanged(这是新的PropertyChangedEventArgs(“MemoryPool”);
}
}
私人虚空观察()
{
FileSystemWatcher watcher=新的FileSystemWatcher();
watcher.Path=Environment.GetFolderPath(Environment.SpecialFolder.InternetCache);
watcher.NotifyFilter=NotifyFilters.LastWrite;
watcher.Filter=“***”;
watcher.Changed+=新文件系统EventHandler(OnChanged);
watcher.EnableRaisingEvents=true;
}
私有void OnChanged(对象源、文件系统目标)
{
UpdateCollection();
}
私有void UpdateCollection()
{
字符串路径=Environment.GetFolderPath(Environment.SpecialFolder.InternetCache);;
System.IO.DirectoryInfo di=新的DirectoryInfo(路径);
CacheFiles=di.GetFiles().ToList();
}
公共清洁工()
{
UpdateCollection();
手表();
}
}
请您详细说明一下代码,两分钟内您得到了两个非常相似的答案。这表明,这不是很难做到。我不会提供全部代码。我建议您阅读INotifyCollectionChanged
界面,然后自己尝试!请你详细说明一下代码好吗?