ObservableCollection 更新列表
ObservableCollection updating List
我也是 WPF 和 MVVM 的新手。我正在我的程序中搜索 USB 设备。但是如果我连接了一个新的设备,需要重启程序才能显示。
怎么做,即刻刷新。
目前我在 class 中搜索设备:
public List<Devices> devices = new List<Devices>();
public void FindDevices() // spremeni v public bool da dobis feedback
{
_deviceList = HidDevices.Enumerate(VendorID, ProductID).ToArray();
... devices.Add(new Devices()
{
DeviceId = nod + 1,
ManufacturerId = deviceManufacturerstring[nod],
ProductId = deviceProductstring[nod],
SerialNumberId = deviceSNstring[nod],
HardwareVersionId = "test4",
FirmwareVersionId = "test5",
DateOfManufaturedId = "test6"
});
在循环中,我将设备添加到列表中。我需要循环,因为我从每个设备读取了一些数据。
我稍后将此设备添加到 ViewModel 的列表中:
public class Windows1ViewModel : ViewModelBase
{
public ObservableCollection<Devices> devfuck { get; protected set; }
List<Devices> _devicelist;
public List<Devices> Devices
{
get { return _devicelist; }
set { _devicelist = value; }
}
public Windows1ViewModel()
{
USBmiddleware cs = new USBmiddleware();
cs.FindDevices();
devfuck = new ObservableCollection<Devices>();
foreach (var item in cs.devices)
{
devfuck.Add(item);
}
List<Devices> keks = cs.devices;
NotifyPropertyChanged("devfuck");
}
public List<Devices> lvdevices
{
get { return _devicelist; }
set { _devicelist = value; }
}
要改变什么?在哪里添加 INotifyPropertyChanged?或者如何解决我的问题?
请帮忙。谢谢!
我的 ViewModelBase
public class ViewModelBase : INotifyPropertyChanged, IDisposable
{
protected ViewModelBase()
{
}
#region DisplayName
public virtual string DisplayName { get; protected set; }
#endregion // DisplayName
#region Debugging Aides
[Conditional("DEBUG")]
[DebuggerStepThrough]
public void VerifyPropertyName(string propertyName)
{
// Verify that the property name matches a real,
// public, instance property on this object.
if (TypeDescriptor.GetProperties(this)[propertyName] == null)
{
string msg = "Invalid property name: " + propertyName;
if (this.ThrowOnInvalidPropertyName)
throw new Exception(msg);
else
Debug.Fail(msg);
}
}
protected virtual bool ThrowOnInvalidPropertyName { get; private set; }
public event PropertyChangedEventHandler PropertyChanged;
/// <param name="propertyName">The property that has a new value.</param>
protected virtual void NotifyPropertyChanged(string propertyName)
{
this.VerifyPropertyName(propertyName);
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
var e = new PropertyChangedEventArgs(propertyName);
handler(this, e);
}
}
protected virtual void NotifyPropertyChangedAll(object inOjbect)
{
foreach (PropertyInfo pi in inOjbect.GetType().GetProperties())
{
NotifyPropertyChanged(pi.Name);
}
}
public virtual void Refresh()
{
NotifyPropertyChangedAll(this);
}
public void Dispose()
{
this.OnDispose();
}
/// <summary>
/// Child classes can override this method to perform
/// clean-up logic, such as removing event handlers.
/// </summary>
protected virtual void OnDispose()
{
}
~ViewModelBase()
{
string msg = string.Format("{0} ({1}) ({2}) Finalized", this.GetType().Name, this.DisplayName, this.GetHashCode());
System.Diagnostics.Debug.WriteLine(msg);
}
}
您需要在 Windows1ViewModel
class 中实施 INotifyPropertyChanged
interface。然后你需要从你的 属性 setter 调用它,或者在你设置 属性 之后调用它。
接下来,您应该设置一个 DispatcherTimer
来每隔一段时间调用您的 FindDevices
方法,然后从那里更新 ObservableCollection
属性。
您可以使用经常调用(根据需要)的计时器来完成此操作。当然,如果您可以使用事件管理来做到这一点,那就更性感了...
谢天谢地,有办法做到这一点:
var watcher = new ManagementEventWatcher();
var query = new WqlEventQuery("SELECT * FROM Win32_DeviceChangeEvent WHERE EventType = 2");
watcher.EventArrived += new EventArrivedEventHandler(watcher_EventArrived);
watcher.Query = query;
watcher.Start();
如此处所述:Detecting USB drive insertion and removal using windows service and c#
然后您可以订阅该事件并调用您的
FindDevices()
从那里开始。
我也是 WPF 和 MVVM 的新手。我正在我的程序中搜索 USB 设备。但是如果我连接了一个新的设备,需要重启程序才能显示。
怎么做,即刻刷新。
目前我在 class 中搜索设备:
public List<Devices> devices = new List<Devices>();
public void FindDevices() // spremeni v public bool da dobis feedback
{
_deviceList = HidDevices.Enumerate(VendorID, ProductID).ToArray();
... devices.Add(new Devices()
{
DeviceId = nod + 1,
ManufacturerId = deviceManufacturerstring[nod],
ProductId = deviceProductstring[nod],
SerialNumberId = deviceSNstring[nod],
HardwareVersionId = "test4",
FirmwareVersionId = "test5",
DateOfManufaturedId = "test6"
});
在循环中,我将设备添加到列表中。我需要循环,因为我从每个设备读取了一些数据。
我稍后将此设备添加到 ViewModel 的列表中:
public class Windows1ViewModel : ViewModelBase
{
public ObservableCollection<Devices> devfuck { get; protected set; }
List<Devices> _devicelist;
public List<Devices> Devices
{
get { return _devicelist; }
set { _devicelist = value; }
}
public Windows1ViewModel()
{
USBmiddleware cs = new USBmiddleware();
cs.FindDevices();
devfuck = new ObservableCollection<Devices>();
foreach (var item in cs.devices)
{
devfuck.Add(item);
}
List<Devices> keks = cs.devices;
NotifyPropertyChanged("devfuck");
}
public List<Devices> lvdevices
{
get { return _devicelist; }
set { _devicelist = value; }
}
要改变什么?在哪里添加 INotifyPropertyChanged?或者如何解决我的问题? 请帮忙。谢谢!
我的 ViewModelBase
public class ViewModelBase : INotifyPropertyChanged, IDisposable
{
protected ViewModelBase()
{
}
#region DisplayName
public virtual string DisplayName { get; protected set; }
#endregion // DisplayName
#region Debugging Aides
[Conditional("DEBUG")]
[DebuggerStepThrough]
public void VerifyPropertyName(string propertyName)
{
// Verify that the property name matches a real,
// public, instance property on this object.
if (TypeDescriptor.GetProperties(this)[propertyName] == null)
{
string msg = "Invalid property name: " + propertyName;
if (this.ThrowOnInvalidPropertyName)
throw new Exception(msg);
else
Debug.Fail(msg);
}
}
protected virtual bool ThrowOnInvalidPropertyName { get; private set; }
public event PropertyChangedEventHandler PropertyChanged;
/// <param name="propertyName">The property that has a new value.</param>
protected virtual void NotifyPropertyChanged(string propertyName)
{
this.VerifyPropertyName(propertyName);
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
var e = new PropertyChangedEventArgs(propertyName);
handler(this, e);
}
}
protected virtual void NotifyPropertyChangedAll(object inOjbect)
{
foreach (PropertyInfo pi in inOjbect.GetType().GetProperties())
{
NotifyPropertyChanged(pi.Name);
}
}
public virtual void Refresh()
{
NotifyPropertyChangedAll(this);
}
public void Dispose()
{
this.OnDispose();
}
/// <summary>
/// Child classes can override this method to perform
/// clean-up logic, such as removing event handlers.
/// </summary>
protected virtual void OnDispose()
{
}
~ViewModelBase()
{
string msg = string.Format("{0} ({1}) ({2}) Finalized", this.GetType().Name, this.DisplayName, this.GetHashCode());
System.Diagnostics.Debug.WriteLine(msg);
}
}
您需要在 Windows1ViewModel
class 中实施 INotifyPropertyChanged
interface。然后你需要从你的 属性 setter 调用它,或者在你设置 属性 之后调用它。
接下来,您应该设置一个 DispatcherTimer
来每隔一段时间调用您的 FindDevices
方法,然后从那里更新 ObservableCollection
属性。
您可以使用经常调用(根据需要)的计时器来完成此操作。当然,如果您可以使用事件管理来做到这一点,那就更性感了...
谢天谢地,有办法做到这一点:
var watcher = new ManagementEventWatcher();
var query = new WqlEventQuery("SELECT * FROM Win32_DeviceChangeEvent WHERE EventType = 2");
watcher.EventArrived += new EventArrivedEventHandler(watcher_EventArrived);
watcher.Query = query;
watcher.Start();
如此处所述:Detecting USB drive insertion and removal using windows service and c#
然后您可以订阅该事件并调用您的
FindDevices()
从那里开始。