当我试图在相同的解决方案中将ToggleButton分配(订阅)给不同文件中的事件处理程序(例如:XAML命名为PrimaryPage.xaml,C#文件命名为不同的EventHandler.cs,而不是PrimaryPage.xaml)时,问题就出现了,因为我想要分离事物.

我的意思是:

  1. 在XAML中创建一个ToggleButton:
<ToggleButton
    x:Name="PumpBTN"
    IsThreeState="False"
    Checked="PumpBTN_C"
    Unchecked="PumpBTN_U"
    Content="Pump" />
  1. 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
    }
}
  1. 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种语言编写的经验,所以我不知道下一步该做什么.有什么主意吗?

推荐答案

典型的方法是使用MVVM将View的IsChecked属性绑定到视图模型中的属性:

<CheckBox IsChecked="{Binding MyIsCheckedProperty}">
Pump
</CheckBox>

视图模型

public class My视图模型 : INotifyPropertyChanged
{
    private bool myIsCheckedValue;
    public bool MyIsCheckedProperty
    {
        get => myIsCheckedValue;
        set
        {
            if (value != myIsCheckedValue)
            {
                myIsCheckedValue = value;                
                OnPropertyChanged();
                // Do other stuff
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

You can add an event to your My视图模型, and raise it at the // Do other stuff point to inform other components. Or just make a call to some method to inform it that the checkbox has changed. The idea with the pattern is that the "视图模型" classes should interact with and update a "Model" that represents the overall state of your application.

Note that you will need to ensure the DataContext object of your 观 is set to the 观Model object. For example by using a little bit of code behind:

public partial class MyMainWindow : Window
{
        private MainWindow视图模型 mwmv;
        public MyMainWindow ()
        {
            InitializeComponent();
            DataContext = new My视图模型;
        }
}

This is typically only done for the root window, and use bindings to associate any other 观s with 观Models. This needs to be done since all bindings are relative to the DataContextObject.

Csharp相关问答推荐

我们应该如何在IHostedService中使用按请求的GbContent实例?

如何使用FastEndpoints和.NET 8 WebAppliationBuilder进行集成测试?

错误NU 1301:无法加载源的服务索引

为什么这个Reflection. Emit代码会导致一个DDL ViolationException?

读取配置文件(mytest. exe. config)

从应用程序图API调用访问所有者字段

C#-VS2022:全局使用和保存时的代码清理

更改执行目录

使用带有参数和曲面的注入失败(&Q;)

如何将DotNet Watch与发布配置和传递给应用程序的参数一起使用?

在两个已具有一对多关系的表之间添加另一个一对多关系

如果是,我怎么才能让这个加75,如果不是,我怎么才能减go 100?

如何在不复制或使用输出的情况下定义项目依赖

如何避免在.NET中将日志(log)写入相对路径

为什么我的用户界面对象移动到略低于实际目标?

在C#中,当输入一个方法/局部函数时,我的IEnumerator被重置/重新创建.为什么?

C#命名管道-编码错误?

在同一个捕获中可以有多种类型的异常吗?

如何读取TagHelper属性的文本值?

实例化列表时的集合表达式是什么?