You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
1.9 KiB

using System;
using System.Windows;
using System.Windows.Controls;
namespace JiLinApp.Pages.ShakeServerManage;
/// <summary>
/// Main.xaml 的交互逻辑
/// </summary>
public partial class Main : UserControl
{
#region Fields
private TcpManager Manager { get; } = new();
#endregion Fields
public Main()
{
InitializeComponent();
Init();
}
#region Init
private void Init()
{
if (DataContext != null)
{
MainViewModel vm = DataContext as MainViewModel;
vm.SetView(this);
Manager.OnTcpDeviceMessage += vm.OnTcpDeviceMessage;
Manager.OnTcpSectionMessage += vm.OnTcpSectionMessage;
Manager.OnTcpSectionAlarmOn += vm.OnTcpSectionAlarmOn;
}
}
private void ChangeState(bool openState)
{
if (openState)
{
OpenBtn.IsEnabled = false;
StopBtn.IsEnabled = true;
}
else
{
OpenBtn.IsEnabled = true;
StopBtn.IsEnabled = false;
}
}
#endregion Init
#region ElementEvent
private void OpenBtn_Click(object sender, RoutedEventArgs e)
{
if (!Manager.IsRunning())
{
try
{
TcpManagerConfig config = new()
{
ServerIp = ServerIp.Text,
ServerPort = int.Parse(ServerPort.Text),
DeviceHeartKeep = int.Parse(DeviceHeartKeep.Text)
};
Manager.StartServer(config);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
return;
}
}
ChangeState(true);
}
private void StopBtn_Click(object sender, RoutedEventArgs e)
{
if (Manager.IsRunning())
{
Manager.StopServer();
}
ChangeState(false);
}
#endregion ElementEvent
}