简体   繁体   English

如何在 AvaloniaUI 中将自定义事件从子 UserControl 引发到父控件

[英]How to raise a custom event from child UserControl to parent control in AvaloniaUI

In dotnet's Avalonia-UI framework.在 dotnet 的Avalonia-UI框架中。 I have a UserControl that is an "about" popup view with a "close" button, and should emit a custom event to the parent control to indicate that the popup should be closed by its parent window.我有一个UserControl ,它是一个带有“关闭”按钮的“关于”弹出视图,并且应该向父控件发出一个自定义事件,以指示弹出应该由其父 window 关闭。

So far it seems I almost have it, but I can't find a way to raise the event.到目前为止,我似乎几乎拥有它,但我找不到引发事件的方法。 This is what I have:这就是我所拥有的:

In The AboutView.axaml I have:AboutView.axaml我有:

<UserControl ... >
  <Button  Command="{Binding OnCloseView}" >Close</Button>

in the AboutViewModel.cs I have defined a ReactiveCommand:AboutViewModel.cs我定义了一个 ReactiveCommand:

public ReactiveCommand<Unit, Unit> OnCloseView { get; }

public AboutViewModel()
{
    OnCloseView = ReactiveCommand.Create(() => { });
}

in the code behind of the AboutView.axaml.cs I have:AboutView.axaml.cs后面的代码中,我有:

public AboutView(AboutViewModel viewModel = null)
        {
            this.InitializeComponent();
            var dataContextViewModel = viewModel ?? new AboutViewModel();
            this.InitializeComponent();
            // ...
            dataContextViewModel.OnCloseView.Subscribe(x => {
                var eventArgs = new RoutedEventArgs();

// How do I raise the ExitView event from here?

                //this.RaiseEvent(eventArgs); // This didn't work, throws null reference exception.

                // This didn't work:
                //EventHandler handler = ExitView;
                //handler?.Invoke(this, eventArgs);

                });
            this.DataContext = dataContextViewModel;
        }

        public static readonly RoutedEvent<RoutedEventArgs> ExitViewEvent =
            RoutedEvent.Register<AboutView, RoutedEventArgs>(nameof(ExitView), RoutingStrategies.Bubble);

        public event EventHandler<RoutedEventArgs> ExitView
        {
            add => AddHandler(ExitViewEvent, value);
            remove => RemoveHandler(ExitViewEvent, value);
        }

In the parent MainWindow.axaml :在父MainWindow.axaml中:

<views:AboutView ExitView="SomeCodeBehindEventHandler"  IsVisible="{Binding $parent.DataContext.IsAboutVisible}"/>

In the code-behind MainWindow.axaml.cs :在代码隐藏MainWindow.axaml.cs中:

    public void OnAboutViewExitHandler(object sender, RoutedEventArgs e)
    {
        var viewModel = (MainWindowViewModel)this.DataContext;
        viewModel.OnCloseAboutPopup.Execute();
        e.Handled=true;
    }

The questions I have are:我的问题是:

  • How do I raise the RoutedEvent "ExitView" from code?如何从代码中引发 RoutedEvent“ExitView”?

  • Is there a better way in axaml to bind the event to a ReactiveCommand in the parent's viewmodel?在 axaml 中是否有更好的方法将事件绑定到父视图模型中的ReactiveCommand I couldn't find a way to bind it since this doesn't compile:我找不到绑定它的方法,因为它无法编译:

<views:AboutView ExitView="{Binding OnCloseAboutPopup}"/>

If I don't find a better way then I will have to pass a function to the code-behind and invoke the ReactiveCommand from there.如果我没有找到更好的方法,那么我将不得不将 function 传递给代码隐藏并从那里调用 ReactiveCommand。

  • Is there any other better way to send the event from the child control to the parent?还有其他更好的方法可以将事件从子控件发送到父控件吗?

You need to specify the routed event when constructing your event args.您需要在构造事件参数时指定路由事件。

var eventArgs = new RoutedEventArgs { RoutedEvent = ExitViewEvent };

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

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