为什么我不能在按钮(或组合框等)事件中将新项目/对象添加到列表中?我的意思是,如果活动不在括号内,活动将看不到我的清单...用红色下划线标出...可以帮我吗?
长话短说:我想通过单击按钮添加一个新对象
private int animValue;
private int strokeWidth = 15;
private int i = 0;
public MyCustomView(Context context) :
base(context)
{
}
public MyCustomView(Context context, IAttributeSet attrs) :
base(context, attrs)
{
animValue = 0;
}
public MyCustomView(Context context, IAttributeSet attrs, int defStyle) :
base(context, attrs, defStyle)
{
}
protected override void OnDraw(Canvas canvas)
{
base.OnDraw(canvas);
Paint paint = new Paint();
paint.SetStyle(Paint.Style.Stroke);
paint.StrokeWidth=(strokeWidth);
RectF rectF = new RectF();
rectF.Set(strokeWidth, strokeWidth, Width - strokeWidth, Width - strokeWidth);
paint.Color = (Color.Gray);
canvas.DrawArc(rectF, 0, 360, false, paint);
paint.Color=(Color.Blue);
canvas.DrawArc(rectF, animValue, i++, false, paint);
}
public void setValue(int animatedValue)
{
animValue = animatedValue;
Invalidate();
}
}
答案 0 :(得分:0)
我认为您应该全局实例化列表,而不是在Form1_Load事件下。 这样,您就可以在整个班级(在这种情况下为窗口)中访问它。
答案 1 :(得分:0)
这似乎很好。启动表单时,可能需要将文本框设置为包含有效值。还请确保您在整个form1类中显示该列表。
namespace Samochody
{
public partial class Form1 : Form
{
// make sure your list looks like this, created outside your functions.
List<Samochod> ListaSamochodow = new List<Samochod>();
public Form1()
{
InitializeComponent();
label1.Text = "the amount in your list is " + ListaSamochodow.Count.ToString();
textBox1.Text = "string here";
textBox2.Text = "string here";
textBox3.Text = "100";
}
private void Form1_Load(object sender, EventArgs e)
{
XmlRootAttribute oRootAttr = new XmlRootAttribute();
XmlSerializer oSerializer = new XmlSerializer(typeof(List<Samochod>), oRootAttr);
StreamWriter oStreamWriter = null;
oStreamWriter = new StreamWriter("samochody.xml");
oSerializer.Serialize(oStreamWriter, ListaSamochodow);
}
private void button1_Click_1(object sender, EventArgs e)
{
Samochod s = new Samochod(textBox1.Text, textBox2.Text, Convert.ToInt32(textBox3.Text));
ListaSamochodow.Add(s);
label1.Text = "the amount in your list is " + ListaSamochodow.Count.ToString();
}
}
}