Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从类访问字符串生成和窗体组件_C# - Fatal编程技术网

C# 从类访问字符串生成和窗体组件

C# 从类访问字符串生成和窗体组件,c#,C#,背景 我正在编写一个程序,它收集不同的事件(按键、鼠标位置等),并将单个字符串发送到串行端口。 程序有一小部分,按下一个键来控制移动(前进-W键;左-a键,前进-左-WA键)。 当事件发生时,将调用一个方法,该方法获取一个数字字符串并在FinalStringClass中处理。在主窗体上,有一个方法,将收集的值分配给标签。 但是,由于缺乏编程经验,我在代码方面遇到了一个问题:( 主要形式: Serial port; FinalStringClass fstr; public

背景

我正在编写一个程序,它收集不同的事件(按键、鼠标位置等),并将单个字符串发送到串行端口。 程序有一小部分,按下一个键来控制移动(前进-W键;左-a键,前进-左-WA键)。 当事件发生时,将调用一个方法,该方法获取一个数字字符串并在FinalStringClass中处理。在主窗体上,有一个方法,将收集的值分配给标签。 但是,由于缺乏编程经验,我在代码方面遇到了一个问题:(

主要形式:

    Serial port;
    FinalStringClass fstr;
    public Form1()
    {
        InitializeComponent();
        //... Serial port settings
        timer.Start();
        fstr = new FinalStringClass(UpdateLabel2);
    }

    public void UpdateLabel2(string x, string y, string speed, string mvm) //
    {
        label2.Text = "x: " + x + "y: " + y + "speed: " + speed + "mvm: " + mvm;
    }
    private void timer_Tick(object sender, EventArgs e)
    {
        var up = KeyboardInfo.GetKeyState(Keys.W);
        var down = KeyboardInfo.GetKeyState(Keys.S);
        var left = KeyboardInfo.GetKeyState(Keys.A);
        var right = KeyboardInfo.GetKeyState(Keys.D);

        if (left.IsPressed)
        {
            if (up.IsPressed == true) // if W and A keys are pressed, ->
            {
                fstr.mvmMethod("7"); // this method is triggered
                return;
            }
            if (down.IsPressed)
            {
                fstr.mvmMethod("9");
                return;
            }
            if (right.IsPressed)
            {

                fstr.mvmMethod("1");
                return;
            }

            fstr.mvmMethod("4"); //if W key is pressed, then this method is triggered
        }
        //... (other if condition statements)

        label2.Text = fstr.FinalStringBuilder();
        port.Write(fstr.FinalStringBuilder());
    }
class FinalStringClass
{
    private Action<string, string, string, string> updateLabel2;
    Action<String> _x, _y, _speed, _mvm;
    string xF, yF, speedF, mvmF;

    public FinalStringClass(Action<string, string, string, string> updateLabel2)
    {
        this.updateLabel2 = updateLabel2;
    }

    public FinalStringClass(Action<String> x, Action<String> y, Action<String> speed, Action<String> mvm)
    {
        _x = x;
        _y = y;
        _speed = speed;
        _mvm = mvm;
    }

    public void xMethod(string x)
    {
        _x(x);
        xF = x;
    }
    public void yMethod(string y)
    {
        _y(y);
        yF = y;
    }
    public void speedMethod(string speed)
    {
        _speed(speed);
        speedF = speed;
    }
    public void mvmMethod(string mvm)
    {
        _mvm(mvm);
        mvmF = mvm;
    }
    public string FinalStringBuilder()
    {
        return xF + yF + speedF + mvmF;
    }
}
外部类:

    Serial port;
    FinalStringClass fstr;
    public Form1()
    {
        InitializeComponent();
        //... Serial port settings
        timer.Start();
        fstr = new FinalStringClass(UpdateLabel2);
    }

    public void UpdateLabel2(string x, string y, string speed, string mvm) //
    {
        label2.Text = "x: " + x + "y: " + y + "speed: " + speed + "mvm: " + mvm;
    }
    private void timer_Tick(object sender, EventArgs e)
    {
        var up = KeyboardInfo.GetKeyState(Keys.W);
        var down = KeyboardInfo.GetKeyState(Keys.S);
        var left = KeyboardInfo.GetKeyState(Keys.A);
        var right = KeyboardInfo.GetKeyState(Keys.D);

        if (left.IsPressed)
        {
            if (up.IsPressed == true) // if W and A keys are pressed, ->
            {
                fstr.mvmMethod("7"); // this method is triggered
                return;
            }
            if (down.IsPressed)
            {
                fstr.mvmMethod("9");
                return;
            }
            if (right.IsPressed)
            {

                fstr.mvmMethod("1");
                return;
            }

            fstr.mvmMethod("4"); //if W key is pressed, then this method is triggered
        }
        //... (other if condition statements)

        label2.Text = fstr.FinalStringBuilder();
        port.Write(fstr.FinalStringBuilder());
    }
class FinalStringClass
{
    private Action<string, string, string, string> updateLabel2;
    Action<String> _x, _y, _speed, _mvm;
    string xF, yF, speedF, mvmF;

    public FinalStringClass(Action<string, string, string, string> updateLabel2)
    {
        this.updateLabel2 = updateLabel2;
    }

    public FinalStringClass(Action<String> x, Action<String> y, Action<String> speed, Action<String> mvm)
    {
        _x = x;
        _y = y;
        _speed = speed;
        _mvm = mvm;
    }

    public void xMethod(string x)
    {
        _x(x);
        xF = x;
    }
    public void yMethod(string y)
    {
        _y(y);
        yF = y;
    }
    public void speedMethod(string speed)
    {
        _speed(speed);
        speedF = speed;
    }
    public void mvmMethod(string mvm)
    {
        _mvm(mvm);
        mvmF = mvm;
    }
    public string FinalStringBuilder()
    {
        return xF + yF + speedF + mvmF;
    }
}
阶级

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
名称空间FFApp
{
类MyClass
{
行动标签机;
公共MyClass(行动标签机)
{
this.labelSetter=labelSetter;
}
公共字符串MyProperty{get;set;}
公共void MyMethod(字符串文本)
{
标签机(文本);
MyProperty=文本;
}
}
}

或者,在这段代码中,是否可以获得不断变化的值(存储在变量中),当按下W/A/D/S/W+A/W+D等键以允许生成最终字符串时?例如:
string finalstring=x+y+speed+pressed_键;

您是否可以添加有关您看到的问题的更多信息?乍一看,您的计时器将位于不同的线程上,因此从该线程更新GUI将导致问题。您应该调用更新以在GUI线程上运行。此处的更多详细信息:@JohnCornell我添加了有关该问题的更多信息请帮助我!:)此处存在多个问题。首先,您应该在调用FinalStringClass中的操作之前设置局部变量,以避免出现争用情况—如果第一个操作在调用第二个操作之前可能没有完成,那么您可能看到了第二个操作。其次,对于表单,请确保将上面提到的invokererequired代码放置在对表单控件的任何编辑周围。最后,任何可能被不同线程访问的变量都需要一个围绕它们的锁。请您添加更多关于您看到的问题的信息好吗?乍一看,计时器将位于不同的线程上,因此从该线程更新GUI将导致问题。您应该调用更新以在GUI线程上运行。更多详细信息:@JohnCornell我添加了有关该问题的更多信息,请帮助我!:)这里有很多问题。首先,您应该在调用FinalStringClass中的操作之前设置局部变量,以避免出现争用情况—如果第一个操作在调用第二个操作之前可能没有完成,那么您可能看到了第二个操作。其次,对于表单,请确保将上面提到的invokererequired代码放置在对表单控件的任何编辑周围。最后,任何可能被不同线程访问的变量都需要一个锁。