c# - c#对象参数在分配参数后神秘地清除
问题描述
我是 C# 的新手。我想为MouseUp
事件中的 Paint 动作收集所有分数。从aPaintAction2
into传入参数后Actions2
,我清除aPaintAction2
. 不知何故,aPaintAction2
内容被清除后,Actions2
(aPaintAction2
传入的)参数值也被清除了。
有人可以向我解释这是什么问题,为什么会这样?我只想将点数aPaintAction2
传入Actions2
,Actions2
保持点数参数,并清除aPaintAction2
,以便aPaintAction2
可以保持新点。谢谢你。
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (moving && x != -1 && y != -1)
{
aPaintAction2.Add(e.Location);
x = e.X;
y = e.Y;
}
}
private List<AnnotationAction> Actions2 = new List<AnnotationAction>();
private List<Point> aPaintAction2 = new List<Point>();
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
// save a Paint action
Actions2.Add(new AnnotationAction(newActionId, pen.Color, pen.Width, aPaintAction2));
aPaintAction2.Clear();
moving = false;
x = -1;
y = -1;
newActionId++;
}
解决方案
代替
aPaintAction2.Clear();
从列表中删除所有项目,请尝试:
aPaintAction2 = new List<Point>();
这将创建一个新的空列表,但将旧列表保留在 Actions 中。
推荐阅读
- c++ - 如何使用 MySQL Server 8.0 为 Qt 构建 MySQL 插件?
- google-sheets - 将多行数组合并为单行
- rust - Rust 紫水晶的画布?
- javascript - 如何将输入的值设置为与所选收音机的值相同?
- font-awesome - 减少 Font Awsome Hamburger 内 3 个条的高度和宽度
- javascript - Django 简单验证码刷新音频源
- java - 有没有更好的方法来优化使用“ParameterizedPreparedStatementSetter”设置参数的代码?
- javascript - 如何在javascript中绕过或编辑计时器
- python-3.x - 如何以简单明了的方式正确地将数据集拆分为训练验证测试集?
- tensorflow - 如何从 TF 模型中加载 .ckpt、.meta 输出 ffile,输入预测数据并进行预测?