从另一个类C#更新标签不起作用

时间:2017-05-23 15:21:59

标签: c# wpf

所以我正在开发一款软件,让我可以连接到一些Arduino并控制它。现在,我有一个方法来处理Arduino的所有代码(串口,波特率,端口名等),一切正常。

现在,我面临的挑战是:我的窗口中有一个标签,我想更改该标签的内容,以便在软件连接到我的Arduino以及何时断开连接时显示该标签。我有代码,它似乎是正确的,它运行,但它不会更新我的标签。我尝试了不同的方式,它仍然没有工作,所以我在其中一个应该更新我的标签的方法中制作了一个MessageBox,并且当按下按钮(连接按钮)时显示该消息框,因此该方法有效,但不是应该更新我的标签的那一行。任何想法为什么以及如何解决这个问题?

ArduinoControl代码:

let items = [{
  name: 'Z'
}, {
  name: 'A'
}, {
  name: 'Y'
}];

let sorted = items.sort((a, b) => {
  if (a.name < b.name) {
    return -1;
  }
  if (a.name > b.name) {
    return 1;
  }
  return 0;
});

console.log(sorted)

我窗口的课程在这里:

using System;
using System.IO.Ports;

namespace MyApp
{
    static class ArduinoControl
    {

        static SerialPort sp = new SerialPort();

        //Pentru conectarea la MainWindow;
        static Arduino ard = new Arduino();

        public static bool isConnected = false;    


        public static void ConnectToArduino()
        {
            try
            {

                string portName = ard.tboxPortName.Text;
                sp.PortName = portName;
                sp.BaudRate = 9600;
                sp.Open();
                ard.DisplayConnected();
                isConnected = true;
            }
            catch (Exception)
            {
                System.Windows.MessageBox.Show("Please give a valid port number or check your connection. " +
                    "If the port number is correct but the error persist, please check if your Arduino device is correctly connected.");
            }
        }


        public static void DisconnectFromArduino()
        {
            try
            {
                sp.Close();
                isConnected = false;
                ard.DisplayDisconnected();
            }
            catch (Exception)
            {
                System.Windows.MessageBox.Show("In order to disconnect, you have to connect first to an Arduino device.");
            }
        }


        //in work
        public static bool IsConnected()
        {
            return isConnected;
        }
    }
}

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您应该调用屏幕上显示的现有窗口实例的方法。这意味着您需要以某种方式获取对此特定实例的引用。

最简单的方法是使用Application.Current.Windows属性:

public static void ConnectToArduino()
{
    try
    {

        Arduino window = Application.Current.Windows.OfType<Arduino>().FirstOrDefault();
        string portName = window.tboxPortName.Text;
        sp.PortName = portName;
        sp.BaudRate = 9600;
        sp.Open();
        window.DisplayConnected();
        isConnected = true;
    }
    catch (Exception)
    {
        System.Windows.MessageBox.Show("Please give a valid port number or check your connection. " +
            "If the port number is correct but the error persist, please check if your Arduino device is correctly connected.");
    }
}

public static void DisconnectFromArduino()
{
    try
    {
        sp.Close();
        isConnected = false;
        Arduino window = Application.Current.Windows.OfType<Arduino>().FirstOrDefault();
        window.DisplayDisconnected();
    }
    catch (Exception)
    {
        System.Windows.MessageBox.Show("In order to disconnect, you have to connect first to an Arduino device.");
    }
}