主窗口页面中的火灾事件 [英] Fire event in page from mainwindow
本文介绍了主窗口页面中的火灾事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个托管页面的窗口。当用户在关闭MainWindow时单击取消时,我想在页面上触发事件。目前,我正在努力研究如何在Page From Window上触发事件。这是页面上的事件;
I have a Window that hosts a Page. I want to fire an event on the Page when the user clicks cancel when closing the MainWindow. At the moment I am struggling to work out how to fire the event on the Page from Window. Here is the event on the Page;
public void OnChangeUserEvent()
{
var _employeeSelectionWindow = new EmployeeSelectionWindow();
_employeeSelectionWindow.ReturnEmployeeDetails += LoadSelectedEmployeeAsLoggedInUser;
_employeeSelectionWindow.ShowDialog();
}
这是我想要解雇的时候(这个方法是在一个窗口,托管页面) ;
And here is when I would like to fire it (this method is in a Window, hosting the page);
private void OnWindowClosed(object sender, System.ComponentModel.CancelEventArgs e)
{
var result = MessageBox.Show(@"Are you sure you want to exit the database?" + Environment.NewLine + "Click cancel to change user.",
"", MessageBoxButton.YesNoCancel);
if (result == MessageBoxResult.Yes)
{
Environment.Exit(0);
}
else if (result == MessageBoxResult.Cancel)
{
//Fire the event on the page here!
e.Cancel = true;
}
else
{
e.Cancel = true;
}
}
我的尝试:
我尝试过使用类似的东西;
What I have tried:
I've tried using something like;
((LoginPage)e.Content).UserChangeEvent+= new EventHandler(MainWindow_NewRecordSaved);
什么都没有用对我来说。
nothing has worked for me so far.
推荐答案
尝试以下示例:
主窗口标记
Try this following sample:
Main window markup
<Window x:Class="WpfApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button x:Name="ShowPageButton" Content="Show Page" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="75" Click="ShowPageButton_Click"/>
<Frame x:Name="frame" Content="Frame" HorizontalAlignment="Left" Height="247" Margin="10,37,0,0" VerticalAlignment="Top" Width="507"/>
<Button x:Name="CancelButton" Content="Cancel" HorizontalAlignment="Left" Margin="414,300,0,0" VerticalAlignment="Top" Width="76" Click="CancelButton_Click"/>
</Grid>
</Window>
主窗口代码
Main window Code
public partial class MainWindow : Window
{
private Page1.OnMainWindowEvent CancelEvent;
private Page1 page1;
public MainWindow()
{
InitializeComponent();
}
private void ShowPageButton_Click(object sender, RoutedEventArgs e)
{
CancelEvent = new Page1.OnMainWindowEvent(OnCancelClicked);
page1 = new Page1();
page1.RegisterMainWindowEvent(CancelEvent);
frame.Content = page1;
}
private void CancelButton_Click(object sender, RoutedEventArgs e)
{
OnCancelClicked("Cancel clicked on main window.");
}
private void OnCancelClicked(string message)
{
page1.DoSomething(message);
}
}
Page1标记
Page1 markup
<Page x:Class="WpfApp.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfApp"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Title="Page1">
<Grid>
<Label x:Name="label" Content="Hi I am Page1" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"/>
</Grid>
</Page>
Page1代码
Page1 code
public partial class Page1 : Page
{
public delegate void OnMainWindowEvent(string eventName);
private event OnMainWindowEvent MainWindowEvent;
public Page1()
{
InitializeComponent();
}
public void RegisterMainWindowEvent(OnMainWindowEvent eventHandler)
{
MainWindowEvent = eventHandler;
}
public void DoSomething(string someMessage)
{
MessageBox.Show(someMessage);
label.Content = someMessage;
}
}
这篇关于主窗口页面中的火灾事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文