从另一个班级更改表单上的标签

时间:2019-02-22 12:16:22

标签: c# wpf oop

我的问题可能很愚蠢,但是经过数小时的搜索,我不得不向您寻求帮助。 我有一个WPF表单LoginPage,它具有一个Label(称为AlertText),向用户显示消息,我需要从另一个类更改他的内容。 要更改内容,我使用AlertText.Content = "content",它可以正常工作。 但是,当我在另一堂课上并且想要更改它时,它什么也没做。 两个类都在同一个命名空间下。

我已经尝试过了:

LoginPage lg = new LoginPage();
lg.AlertText.Content = "content";

对象已创建,但第二行未执行。 我试图在我的SetAlertText()类中创建一个公共函数LoginPage,但这也不起作用。 我将其设置为静态并由LoginPage.SetAlertText()调用,但是仍然存在相同的问题。

我已将其添加到LoginPage中:

public static LoginPage instance;

        public LoginPage()
        {
            instance = this;
        }

并由

调用
LoginPage.instance.SetAlertText();
    and 
LoginPage.instance.AlertText.Content = "content";

但都不起作用。

我也尝试过:

public LoginPage()
            {
                instance = new LoginPage();
            }

我该怎么办?

编辑: 这是两个类的完整代码:

namespace TMClientWPF
{
    /// <summary>
    /// Logique d'interaction pour LoginPage.xaml
    /// </summary>
    public partial class LoginPage : Page
    {
        ClientTCP client;
        bool isConnected = false;
        public RegisterPage registerPage;
        public static LoginPage instance;

        public LoginPage()
        {
            instance = this;
            InitializeComponent();

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            AlertText.Content = "";
            try
            {
                if (isConnected == false)
                {

                    client = new ClientTCP();
                    client.Connect();

                    isConnected = true;
                }

                client.SEND_LOGINS(UsernameText.Text, PasswordText.Password.ToString());
            }
            catch (NullReferenceException)
            {
                AlertText.Visibility = Visibility.Visible;
                AlertText.Content = "Failed to connect to server, please try again later.";
            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            registerPage = new RegisterPage();
            this.NavigationService.Navigate(registerPage);
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }

        public void SetAlertText(string text)
        {
            AlertText.Visibility = Visibility.Visible;
            AlertText.Content = "Welcome Home";
        }
    }
}

//////////////////////////////

namespace TMClientWPF
{
    public class ClientHandlePackets
    {
        public static ByteBuffer playerBuffer;
        private delegate void Packet_(byte[] data);
        private static Dictionary<long, Packet_> packets;
        private static long pLength;

        public static void HandleData(byte[] data)
        {

            InitializePackets();
            byte[] Buffer;
            Buffer = (byte[])data.Clone();

            if (playerBuffer == null)
            {
                playerBuffer = new ByteBuffer();
            }
            playerBuffer.WriteBytes(Buffer);
            if (playerBuffer.Count() == 0)
            {
                playerBuffer.Clear();
                return;
            }

            if (playerBuffer.Length() >= 8)
            {
                pLength = playerBuffer.ReadLong(false);

                if (pLength <= 0)
                {
                    playerBuffer.Clear();
                    return;
                }
            }

            while (pLength > 0 & pLength <= playerBuffer.Length() - 8)
            {
                if (pLength <= playerBuffer.Length() - 8)
                {
                    playerBuffer.ReadLong(); //read out the packet identifier
                    data = playerBuffer.ReadBytes((int)pLength); //get the full package length
                    HandleDataPackets(data);
                }
                pLength = 0;
                if (playerBuffer.Length() >= 8)
                {
                    pLength = playerBuffer.ReadLong(false);

                    if (pLength < 0)
                    {
                        playerBuffer.Clear();
                        return;
                    }
                }
            }
        }

        private static void HandleDataPackets(byte[] data)
        {
            long packetIdentifier;
            ByteBuffer buffer;
            Packet_ packet;

            buffer = new ByteBuffer();
            buffer.WriteBytes(data);
            packetIdentifier = buffer.ReadLong();
            buffer.Dispose();

            if (packets.TryGetValue(packetIdentifier, out packet))
            {
                packet.Invoke(data);
            }


        }

        private static void InitializePackets()
        {
            packets = new Dictionary<long, Packet_>();
            packets.Add((long)ServerPackets.S_INFORMATION, PACKET_INFORMATION);
            packets.Add((long)ServerPackets.S_EXECUTEMETHODONCLIENT, PACKET_EXECUTEMETHOD);
            packets.Add((long)ServerPackets.S_LOGCALLBACK, Packet_LogCallback);
        }

        #region packets

        private static void PACKET_INFORMATION(byte[] data)
        {
            ByteBuffer buffer = new ByteBuffer();
            buffer.WriteBytes(data);

            long packetIndentifier = buffer.ReadLong();

            string msg1 = buffer.ReadString();
            string msg2 = buffer.ReadString();
            int level = buffer.ReadInteger();
            buffer.Dispose();
            ClientTCP.instance.SEND_THANKYOU();
        }

        private static void PACKET_EXECUTEMETHOD(byte[] data)
        {
            ClientTCP client = new ClientTCP();
        }

        private static void Packet_LogCallback(byte[] data)
        {
            ByteBuffer buffer = new ByteBuffer();
            buffer.WriteBytes(data);
            buffer.ReadLong();
            string message = buffer.ReadString();
            string username = buffer.ReadString();
            buffer.Dispose();
            if(message == "true")
            {
               // LoginPage.SetAlertText("Welcome Home.");
            }
            else
            { 
                LoginPage lg = new LoginPage();
                LoginPage.instance.SetAlertText("Invalid logins.");
            }
        }

        #endregion

    }
}

0 个答案:

没有答案
相关问题