`我需要有关 IPropertyChanged 的帮助,如何操作才能正常工作。
`C# 如何使用 IPropertyChanged 创建对象类?
我尝试在课堂上执行此操作,但它不起作用,我需要以某种方式更改它或添加一些方法。我正在尝试在 Employee 类中实现 INotifyPropertyChanged 接口,但无法使其正常工作。我需要确保正确通知财产变更。以下是我的实施细节:
员工类别: 此类代表具有姓名、职位、电话等属性的员工。 我已经实现了 INotifyPropertyChanged 来通知这些属性的更改。
public string? Name { get; set; }
public string? Position { get; set; }
public string? Phone { get; set; }
public string? Email { get; set; }
public string? Room { get; set; }
public string? MainWorkplace { get; set; }
public string? Workplace { get; set; }
public event PropertyChangedEventHandler? PropertyChanged;
Here is how it works, maybe i forgot to put some brackets. There are 3 classes, each represents some object, i need to apply inotifychanged to it EmployeeList Class: This class represents a collection of employees and provides methods to load from and save to a JSON file. It also includes methods to search employees and get distinct positions and workplaces.
public class EmployeeList : ObservableCollection<Employee>
{
public static EmployeeList? LoadFromJson(FileInfo jsonFile)
{
if (!jsonFile.Exists)
throw new FileNotFoundException("Súbor typu JSON neexistuje !", jsonFile.FullName);
string jsonText = File.ReadAllText(jsonFile.FullName);
EmployeeList? employeeList = JsonConvert.DeserializeObject<EmployeeList>(jsonText);
return employeeList;
}
`public void SaveToJson(FileInfo jsonFile)
{
string jsonText = JsonConvert.SerializeObject(this, Formatting.Indented);
File.WriteAllText(jsonFile.FullName, jsonText);
}
public IEnumerable<string?> GetPositions() => this.Select(employee => employee.Position).Distinct().OrderBy(p => p);
public IEnumerable<string?> GetMainWorkplaces() => this.Select(employee => employee.MainWorkplace).Distinct().OrderBy(wp => wp);
public SearchResult Search(string? name, string? position, string? mainWorkplace)
{
IEnumerable<Employee> emps = this.AsEnumerable();
List<Employee> filteredEmps = new();
foreach (var e in emps)
{
bool matchOfMW = true;
bool matchOfP = true;
bool matchOfN = true;
if (!string.IsNullOrEmpty(mainWorkplace))
matchOfMW = e.MainWorkplace?.IndexOf(mainWorkplace, StringComparison.OrdinalIgnoreCase) >= 0;
if (!string.IsNullOrEmpty(position))
matchOfP = e.Position?.IndexOf(position, StringComparison.OrdinalIgnoreCase) >= 0;
if (!string.IsNullOrEmpty(name))
matchOfN = e.Name?.IndexOf(name, StringComparison.OrdinalIgnoreCase) >= 0;
if (matchOfMW && matchOfP && matchOfN)
filteredEmps.Add(e);
}
Console.WriteLine($"Počet ľudí: {emps.Count()}");
Console.WriteLine($"Z toho spĺňajúci: {filteredEmps.Count} \n");
return new SearchResult(filteredEmps.ToArray());
}
}`
`public class SearchResult
{
public Employee[] Employees { get; }
public SearchResult(Employee[] employees)
{
Employees = employees;
}
public void SaveToCsvFile(string filePath, string delimiter = "\t")
{
if (Employees == null)
return;
using (StreamWriter writer = new StreamWriter(filePath, false, Encoding.UTF8))
{
writer.Write("\uFEFF");
writer.WriteLine($"Name{delimiter}Position{delimiter}Phone{delimiter}Email{delimiter}Room{delimiter}MainWorkplace{delimiter}Workplace");
foreach (Employee e in Employees)
{
writer.WriteLine($"{e.Name}{delimiter}{e.Position}{delimiter}{e.Phone}{delimiter}{e.Email}" + $"{delimiter}{e.Room}{delimiter}{e.MainWorkplace}{delimiter}{e.Workplace}");
}
}
}
public int GetEmployeeCount()
{
return Employees.Length;
}`
Here is how it works, maybe i forgot to put some brackets. There are 3 classes, each represents some object, i need to apply inotifychanged to it.
我会这样做
public class Employee : INotifyPropertyChanged
{
private string _name = string.Empty;
private string _position = string.Empty;
private string? _mainWorkplace;
private string _email = string.Empty;
private string? _room;
private string? _phone;
private string? _workplace;
public string Name { get => _name;
set
{
if (_name != value)
{
_name = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(Name));
}
}
}
public string Position { get => _position;
set
{
if (_position != value)
{
_position = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(Position));
}
}
}
public string? Phone { get => _phone;
set
{
if (_phone != value)
{
_phone = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(Phone));
}
}
}
public string Email { get => _email;
set
{
if (_email != value)
{
_email = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(Email));
}
}
}
public string? Room { get => _room;
set
{
if (_room != value)
{
_room = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(Room));
}
}
}
public string? MainWorkplace { get => _mainWorkplace;
set
{
if (_mainWorkplace != value)
{
_mainWorkplace = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(MainWorkplace));
}
}
}
public string? Workplace { get => _workplace;
set {
if (_workplace != value) {
_workplace = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(Workplace));
}
}
}
public event PropertyChangedEventHandler? PropertyChanged;
}
} )