当我试图在相同的解决方案中将ToggleButton
分配(订阅)给不同文件中的事件处理程序(例如:XAML命名为PrimaryPage.xaml
,C#文件命名为不同的EventHandler.cs
,而不是PrimaryPage.xaml
)时,问题就出现了,因为我想要分离事物.
我的意思是:
- 在XAML中创建一个
ToggleButton
:
<ToggleButton
x:Name="PumpBTN"
IsThreeState="False"
Checked="PumpBTN_C"
Unchecked="PumpBTN_U"
Content="Pump" />
- 在
PrimaryPage.xaml.cs
中创建活动订阅
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml;
using WinUI.XXXX.EventHandler; // Include EventHandler
namespace WinUI.XXXX.Views;
public partial class PrimaryPage : Page // Unsealed
{
public PrimaryViewModel ViewModel
{
get;
}
public PrimaryPage()
{
ViewModel = App.GetService<PrimaryViewModel>();
InitializeComponent();
Event event = new Event();
PumpBTN.Checked += event.PumpBTN_C; // Pump-checked subscription
PumpBTN.Unchecked += event.PumpBTN_U; // Pump-unchecked subscription
}
}
- 在
EventHandler.cs
中创建事件处理程序:
using System.IO.Ports;
using WinUI.XXXX.Views;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml;
namespace WinUI.XXXX.EventHandler;
public partial class Event
{
public readonly SerialPort serial_port = new();
public Serial()
{
Main();
}
public string Main()
{
// Serial port stuffs
return 'strings' // Just for an example
}
public void PumpBTN_C(object sender, RoutedEventArgs e) // Event handler
{
// Code
}
public void PumpBTN_U(object sender, RoutedEventArgs e) // Event handler
{
// Code
}
我想我已经做了我需要做的所有事情,我已经输入using
,后面跟着相应的命名空间,我已经订阅并根据需要创建了一个事件处理程序.
我几周前才开始在WinUI3中用C#和XAML开发图形用户界面,之前我没有用OOP种语言编写的经验,所以我不知道下一步该做什么.有什么主意吗?