小编dig*_*ate的帖子

事件循环

我发现自己经常处于以下情况:

我有一个绑定到一些数据的用户控件。无论何时更新控件,都会更新基础数据。每当更新基础数据时,控件都会更新。所以很容易陷入永无止境的更新循环(控制更新数据、数据更新控制、控制更新数据等)。

通常我通过使用 bool(例如updatedByUser)来解决这个问题,因此我知道控件是否已以编程方式或由用户更新,然后我可以决定是否触发事件以更新基础数据。这看起来不太整洁。

是否有一些处理此类场景的最佳实践?

编辑:我添加了以下代码示例,但我想我已经回答了我自己的问题......?

public partial class View : UserControl
{
    private Model model = new Model();

    public View()
    {
        InitializeComponent();
    }

    public event EventHandler<Model> DataUpdated;

    public Model Model
    {
        get
        {
            return model;
        }
        set
        {
            if (value != null)
            {
                model = value;
                UpdateTextBoxes();
            }
        }
    }

    private void UpdateTextBoxes()
    {
        if (InvokeRequired)
        {
            Invoke(new Action(() => UpdateTextBoxes()));
        }
        else
        {
            textBox1.Text = model.Text1;
            textBox2.Text = model.Text2;
        }
    }

    private void textBox1_TextChanged(object sender, EventArgs …
Run Code Online (Sandbox Code Playgroud)

c# user-interface circular-reference winforms

6
推荐指数
1
解决办法
675
查看次数

从左上到右下的排序坐标

我该如何尝试从左上到右下对不规则数组的点进行排序,如下图所示?

点从左上到右下的顺序

我考虑过的方法是:

  • 计算每个点到图像左上角的距离(毕达哥拉斯定理),但对Y坐标应用某种加权,以尝试对同一' row' 上的点进行优先排序distance = SQRT((x * x) + (weighting * (y * y)))

  • 将点排序为逻辑行,然后对每一行进行排序。

困难的部分原因是我不知道图像中会出现多少行和多少列以及点阵列的不规则性。任何建议将不胜感激。

sorting image-processing coordinates

5
推荐指数
1
解决办法
1453
查看次数

如何判断父类的实例是否是从特定子项的实例创建的

我发现通过代码示例更容易提出这个问题.

class Parent {}
class Child : Parent {}
...
...
Child firstChild = new Child();
Child secondChild = new Child();
Parent firstParent = (Parent)firstChild;
Parent secondParent = (Parent)secondChild;
Run Code Online (Sandbox Code Playgroud)

如果我不知道上述作业,如何firstParentfirstChild不访问/比较其字段或属性的情况下确定是否是从实例创建的?

c#

5
推荐指数
1
解决办法
59
查看次数

C# XmlWriter 命名空间问题

我正在使用XmlWriter并且正在努力创建以下 XML 标记。

<mzML xmlns="http://psi.hupo.org/ms/mzml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://psi.hupo.org/ms/mzml http://psidev.info/files/ms/mzML/xsd/mzML1.1.0_idx.xsd" version="1.1">
Run Code Online (Sandbox Code Playgroud)

我有以下几点:

XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;

XmlWriter xmlWriter = XmlWriter.Create(xmlFilePath, settings);

xmlWriter.WriteStartDocument();

xmlWriter.WriteStartElement("mzML", "xmlns", @"http://psi.hupo.org/ms/mzml");

xmlWriter.WriteAttributeString("xsi", "xmlns", @"http://www.w3.org/2001/XMLSchema-instance");

xmlWriter.WriteAttributeString("schemaLocation", "xsi", @"http://psi.hupo.org/ms/mzml http://psidev.info/files/ms/mzML/xsd/mzML1.1.0.xsd");

xmlWriter.WriteAttributeString("version", "1.1");

xmlWriter.WriteEndElement();

xmlWriter.WriteEndDocument();

xmlWriter.Close();
Run Code Online (Sandbox Code Playgroud)

结果如下:

<mzML:xmlns p1:xsi="http://www.w3.org/2001/XMLSchema-instance" p2:schemaLocation="http://psi.hupo.org/ms/mzml http://psidev.info/files/ms/mzML/xsd/mzML1.1.0.xsd" version="1.1.0" xmlns:p2="xsi" xmlns:p1="xmlns" xmlns:mzML="http://psi.hupo.org/ms/mzml">
Run Code Online (Sandbox Code Playgroud)

文档让我很困惑;我已经尝试了上述代码的许多变体,但似乎无法接近我的目标 XML 标记。

有人可以帮忙吗?

XmlWriter由于我需要创建的 XML 文件的大小,我需要使用 PS 。)

c# xmlwriter

4
推荐指数
1
解决办法
2953
查看次数