简体   繁体   English

C# 在 WinUI 3 中向 CustomControl 添加自定义事件

[英]C# Add custom event to a CustomControl in WinUI 3

I've created a CustomControl in WinUI 3 with some custom properties ( DependencyProperty ).我在WinUI 3中创建了一个带有一些自定义属性( DependencyProperty )的CustomControl

Now I also want to add a custom Event (not sure if it has to be a RoutedEvent ?) that has to trigger whenever the CurrentStatus property of my control changes.现在我还想添加一个自定义Event (不确定它是否必须是RoutedEvent ?),只要我的控件的CurrentStatus属性发生更改,它就必须触发。 I'd like to be able to manage it just like a simple Button's Click , so something like:我希望能够像简单的 Button's Click一样管理它,例如:

public sealed class MyCustomTextBlock : Control
{
    public enum Status
    { 
        Waiting,
        Busy,
    }
    
    private Status currentStatus;
    public Status CurrentStatus
    {
        get { return currentStatus; }
    }

    public MyCustomTextBlock()
    {
        this.DefaultStyleKey = typeof(MyCustomTextBlock);
        currentStatus = Status.Waiting;
    }

    // The currentStatus variable is set on some custom methods inside the implementation of the control.
}
<local:MyCustomTextBlock x:Name="MessageBlock"
                    Message="{TemplateBinding Message}"
                    DisplayMode="{TemplateBinding DisplayMode}"
                    StatusChanged="MessageBlock_StatusChanged">
</local:MyCustomTextBlock>
private void MessageBlock_StatusChanged(object sender, RoutedEventArgs e)
{
    // Check the CurrentStatus property
    if (MyCustomTextBlock.CurrentStatus == MyCustomTextBlock.Status.Waiting)
        // Do something...
}

How should I declare the Event and the EventHandler on the code behind of the MyCustomTextBlock control?我应该如何在MyCustomTextBlock控件背后的代码上声明 Event 和 EventHandler?

I'm new to WinUI 3 and XAML and looking for " winui 3 custom event " I found this but it's related to WPF and so I cannot find the EventManager.RegisterRoutedEvent() which is cited in the WinUI Microsoft.UI.Xaml library. I'm new to WinUI 3 and XAML and looking for " winui 3 custom event " I found this but it's related to WPF and so I cannot find the EventManager.RegisterRoutedEvent() which is cited in the WinUI Microsoft.UI.Xaml library.

You can implement a custom EventHandler like this.您可以像这样实现自定义EventHandler

public class StatusChangedEventArgs : EventArgs
{
    public Status OldValue { get; }
    public Status NewValue { get; }

    public StatusChangedEventArgs(Status oldValue, Status newValue)
    {
        OldValue = oldValue;
        NewValue = newValue;
    }
}

public sealed class MyCustomTextBlock : Control
{
    public enum Status
    { 
        Waiting,
        Busy,
    }
    
    private Status currentStatus;
    public Status CurrentStatus
    {
        get { return currentStatus; }
    }

    public MyCustomTextBlock()
    {
        this.DefaultStyleKey = typeof(MyCustomTextBlock);
        currentStatus = Status.Waiting;
    }

    public event EventHandler<StatusChangedEventArgs>? StatusChanged;

    protected void OnStatusChanged(Status oldValue, Status newValue)
    {
        StatusChanged?.Invoke(this, new StatusChangedEventArgs(oldValue, newValue));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM