[英]How do I fire a custom event from a User Control?
I have a very interesting task that I need help with. 我有一个非常有趣的任务,我需要帮助。 The description is as follows:
描述如下:
I have a user control (SomeUserControl), that I am using in a Main Window. 我有一个用户控件(SomeUserControl),我在主窗口中使用。 I write my application entirely on SomeUserControl, and Main Window is hosting SomeUserControl and nothing else.
我完全在SomeUserControl上编写我的应用程序,主窗口正在托管SomeUserControl,没有别的。
Right now I have a shutdown button (in SomeUserControl), that is supposed to close the Main Window. 现在我有一个关闭按钮(在SomeUserControl中),它应该关闭主窗口。 The reason for this is that I do not want anything from SomeUserControl to close the application itself, but to fire an event from SomeUserControl, and Main Window receives it, and Main Window will close the application instead of SomeUserControl.
原因是我不希望SomeUserControl中的任何东西关闭应用程序本身,而是从SomeUserControl触发事件,主窗口接收它,主窗口将关闭应用程序而不是SomeUserControl。
How do I do it? 我该怎么做? I am not familiar with the concept of creating and handling custom events, so if someone could explain it in words and in code as an example, I will be very grateful to you!
我不熟悉创建和处理自定义事件的概念,所以如果有人能用文字和代码解释它,我将非常感谢你!
Edit: Here's my code so far. 编辑:到目前为止,这是我的代码。
(in Window 2) (在窗口2中)
Public Event CloseApp As EventHandler
Private Sub CancelButton_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles CancelButton.Click
DialogResult = False
RaiseEvent CloseApp(Me, New EventArgs)
End Sub
(In Main Window) Public loginPage As New LoginPage (在主窗口中)公共loginPage作为新的LoginPage
Public Sub New()
InitializeComponent()
AddHandler loginPage.CloseApp, AddressOf Me.ShutDownJobChangeWindow
End Sub
Private Sub ShutDownJobChangeWindow(ByVal sender As Object, ByVal e As EventArgs)
Application.Current.Shutdown()
End Sub
Goal: I want to close the application when I click cancel in Window 2, but I don't want to do it in such a way that Window 2 closes itself, but by sending some notification to Main Window, and Main Window closes the application. 目标:我想在窗口2中单击取消时关闭应用程序,但我不希望以窗口2自行关闭的方式执行此操作,而是通过向主窗口发送一些通知,并且主窗口关闭应用程序。
If the logic of the user control is implemented in the "code behind" of the user control class, do the following. 如果用户控件的逻辑在用户控件类的“代码隐藏”中实现,请执行以下操作。
I'm assuming the XAML file has somewhere a button with a click event: 我假设XAML文件有一个带有click事件的按钮:
<Button Click="Button_Click">Close App</Button>
Then, in your code behind for SomeUserControl class, do the following: 然后,在SomeUserControl类的代码后面,执行以下操作:
public partial class SomeUserControl : UserControl
{
public event EventHandler CloseApp;
...
private void Button_Click( object sender, RoutedEventArgs e )
{
if( CloseApp != null ) {
CloseApp( this, new EventArgs( ) );
}
}
}
In your Main window, listen to the event: 在主窗口中,收听事件:
...
someUserCtrl.CloseApp += new EventHandler( MyFn );
private void MyFn( object sender, object EventArgs e ) {
...
}
The simplest way to do this is to declare a custom event: 最简单的方法是声明一个自定义事件:
public delegate void ShutdownEventHandler (object sender, EventArgs data);
public event ShutdownEventHandler Shutdown;
protected void OnShutdown(EventArgs args)
{
if(Shutdown != null)
Shutdown(this, args);
}
Then you subscribe to the Shutdown event in MainWindow, and handle shutdown logic there. 然后,您在MainWindow中订阅Shutdown事件,并在那里处理关闭逻辑。 And in SomeUserControl, you simply run OnShutdown when you want to shutdown your application.
在SomeUserControl中,只需在关闭应用程序时运行OnShutdown即可。
I had the same problem, and I've solved in this way: 我有同样的问题,我已经用这种方式解决了:
if you are using a soft version of MVVM (with soft I mean that you use codebehind for events handling) and your event is within the ModelView class do this: 如果您使用的是MVVM的软版本( 软件我的意思是您使用代码隐藏进行事件处理)并且您的事件在ModelView类中执行此操作:
In your MainWindow: 在您的MainWindow中:
public MainWindow(ViewModels.MyViewModel vm)
{
InitializeComponent();
//pass the istance of your ViewModel to the MainWindow (for MVVM patter)
this.vm = vm;
//Now pass it to your User Control
myUserControl.vm = vm;
}
In your UserControl 在您的UserControl中
public partial class MyUserControl: UserControl
{
public ViewModels.MyViewModel vm;
public MyUserControl()
{
InitializeComponent();
}
private void button_Click(object sender, RoutedEventArgs e)
{
vm.MyMethod();
}
}
Last but not least, in your MainWindow's xaml insert your user control and give it a name: 最后但并非最不重要的是,在您的MainWindow的xaml中插入您的用户控件并为其命名:
<local:MyUserControl x:Name="myUserControl" />
If you don't want to use a ViewModel, you can simply pass to your UserControl an instance of your MainWindow and then you can run within the codebehind of your usercontrol all of your MainWindow's public methods. 如果您不想使用ViewModel,只需将您的MainWindow实例传递给UserControl,然后您就可以在您的用户控件的代码隐藏中运行所有MainWindow的公共方法。
Hope it will help! 希望它会有所帮助!
Sorry, the question is VB so here is the VB version of the code above: 对不起,问题是VB所以这里是上面代码的VB版本:
MainWindow: 主窗口:
Public Sub New(ByVal vm As ViewModels.MyViewModel)
InitializeComponent()
Me.vm = vm
myUserControl.vm = vm
End Sub
UserControl: 用户控件:
Public Partial Class MyUserControl
Inherits UserControl
Public vm As ViewModels.MyViewModel
Public Sub New()
InitializeComponent()
End Sub
Private Sub button_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
vm.MyMethod()
End Sub
End Class
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.