自定义类添加到列表,发送到新方法的第一个问题
本文关键字:新方法 第一个 问题 添加 列表 自定义 | 更新日期: 2023-09-27 18:21:58
我有一个全局类,它将另一个类的所有实例保存在内存中,但每次添加新实例时,它也应该发送到另一个显示它的方法。这是下面的代码:
/// <summary>
/// Notifications held
/// Array((int)type,(string)"message")
/// </summary>
private List<Classes.notificationObject> _notifications;
public List<Classes.notificationObject> notifications
{
get { return _notifications; }
set
{
if (_notifications != value)
{
_notifications = value;
OnPropertyChanged("notifications");
//Show new notification
Views.Notification x = new Views.Notification(value);
x.Show();
}
}
}
根据我的理解,我认为value
只是Classes.notificationObject
对象,但我得到了以下错误:参数1:无法从转换
'System.Collections.Generic.List'到"PhotoManagment.Classes.notificationObject"
这是"notificationObject":
public class notificationObject
{
private string _icon;
private string _message;
private string _detail;
private Color _color1;
private Color _color2;
public string Icon
{ get { return _icon; } set { _icon = value; }}
public string Message
{ get { return _message; } set { _message = value; }}
public string Detail
{ get { return _detail; } set { _detail = value; }}
public Color Color1
{ get { return _color1; } set { _color1 = value; }}
public Color Color2
{ get { return _color2; } set { _color2 = value; }}
public notificationObject newNotification(int type, string detail, string message)
{
//Create new instance of object
notificationObject x = new notificationObject();
switch (type)
{
case 1:
//Fatal
x.Icon = "";
x.Message = message;
x.Detail = detail;
x.Color1 = Color.FromArgb(0, 170, 60, 18);
x.Color2 = Color.FromArgb(0, 238, 78, 16);
return x;
case 2:
//Fatal
x.Icon = "";
x.Message = message;
x.Detail = detail;
x.Color1 = Color.FromArgb(0, 170, 60, 18);
x.Color2 = Color.FromArgb(0, 238, 78, 16);
return x;
case 3:
//Unauthorized
x.Icon = "";
x.Message = message;
x.Detail = detail;
x.Color1 = Color.FromArgb(0, 170, 60, 18);
x.Color2 = Color.FromArgb(0, 238, 78, 16);
return x;
case 4:
//Warning
x.Icon = "";
x.Message = message;
x.Detail = detail;
x.Color1 = Color.FromArgb(0, 241, 176, 24);
x.Color2 = Color.FromArgb(0, 205, 152, 28);
return x;
case 5:
//Warning
x.Icon = "";
x.Message = message;
x.Detail = detail;
x.Color1 = Color.FromArgb(0, 41, 161, 213);
x.Color2 = Color.FromArgb(0, 36, 142, 184);
return x;
case 6:
//Warning
x.Icon = "";
x.Message = message;
x.Detail = detail;
x.Color1 = Color.FromArgb(0, 94, 129, 7);
x.Color2 = Color.FromArgb(0, 138, 183, 28);
return x;
}
//Can't find error code
x.Icon = "";
x.Message = message;
x.Detail = detail;
x.Color1 = Color.FromArgb(0, 170, 60, 18);
x.Color2 = Color.FromArgb(0, 238, 78, 16);
return x;
}
编辑1
嗯,看看我现在是否要使用我的"runtimeObject"对象,并想将notificationObject
添加到我的列表中,我会调用"runtimeObjectInstance.add()"。
然而,runtimeObject
将具有许多不同的属性,所以这不是一个糟糕的方法吗?另外,我不能在不自己编码的情况下进行runtimeObjectInstance.notifications.Add()
吗?
此错误表示您的属性需要一个notificationObject列表。但是您正在分配一个"notificationObject"。您可以从IList派生Collection类,然后使用"Add"方法来填充它,而不是用这种方式。如果这太难了,您可以创建一个具有List属性的类,并实现"Add"方式来填充上面的集合类型属性。
请确保在类构造函数中初始化集合。
public class MyList
{
public MyList()
{
_notifications = new List<notificationObject>();
}
private List<notificationObject> _notifications;
public List<notificationObject> notifications
{
get { return _notifications; }
set
{
if (_notifications != value)
{
_notifications = value;
OnPropertyChanged("notifications");
//I am not sure about this bit of code. You may have to test it again after doing this change.
//Show new notification
//Views.Notification x = new Views.Notification(value);
//x.Show();
}
}
}
public void Add(notificationObject newnotificationObject)
{
notifications.Add(newnotificationObject);
OnPropertyChanged("notifications");
}
public void Remove(notificationObject existingnotificationObject)
{
if (notifications.Contains(existingnotificationObject))
{
notifications.Remove(existingnotificationObject);
OnPropertyChanged("notifications");
}
}
}
这是第一种方法的示例。如果你是这样做的,你就必须实现所有的方法。
public class MyList2 : IList<notificationObject>
{
public int IndexOf(notificationObject item)
{
throw new NotImplementedException();
}
public void Insert(int index, notificationObject item)
{
throw new NotImplementedException();
}
public void RemoveAt(int index)
{
throw new NotImplementedException();
}
public notificationObject this[int index]
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
public void Add(notificationObject item)
{
throw new NotImplementedException();
}
public void Clear()
{
throw new NotImplementedException();
}
public bool Contains(notificationObject item)
{
throw new NotImplementedException();
}
public void CopyTo(notificationObject[] array, int arrayIndex)
{
throw new NotImplementedException();
}
public int Count
{
get { throw new NotImplementedException(); }
}
public bool IsReadOnly
{
get { throw new NotImplementedException(); }
}
public bool Remove(notificationObject item)
{
throw new NotImplementedException();
}
public IEnumerator<notificationObject> GetEnumerator()
{
throw new NotImplementedException();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
}