事件识别 C# WPF
Event recognition C# WPF
我遇到了事件问题。我得到了第一个 Window,看起来像这样:
using System.Windows;
namespace EventsTests
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
/*Binding Event to MainWindow
dont work until you will help*/
MainWindow mw = new MainWindow();
mw.RaiseEvent += raiseEvent_EventHandler;
}
public void raiseEvent_EventHandler()
{
MessageBox.Show("MAINWINDOW Event Fired");
}
private void Button_Click(object sender, RoutedEventArgs e)
{
SecondPage sp = new SecondPage();
sp.Show();
}
}
}
现在第二个页面做的不多:
using System.Windows;
namespace EventsTests
{
/// <summary>
/// Interaction logic for SecondPage.xaml
/// </summary>
public partial class SecondPage : Window
{
SecondPageViewModel spvm = new SecondPageViewModel();
public SecondPage()
{
this.DataContext = spvm;
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
spvm.raiseEventActivate();
}
}
}
最后我有了 SecondPageViewModel:
namespace EventsTests
{
public delegate void raiseEventEventHandler();
class SecondPageViewModel
{
public event raiseEventEventHandler raiseEvent;
public void raiseEventActivate()
{
if(raiseEvent != null)
{
raiseEvent();
}
}
}
}
现在我想,当我单击第二页上的按钮时,事件将被触发,MainWindow 会识别该事件。
使用此代码我得到错误:
错误 1 无法分配给 'RaiseEvent' 因为它是 'method group'
有人可以帮助我吗?或者举个例子?
感谢您的每一个提示 ;)
RaiseEvent
不是你的事件,它是Window
的一个方法。
我想你想这样做:
SecondPage sp = new SecondPage();
sp.raiseEvent += raiseEvent_EventHandler;
sp.Show();
即用第二个页面事件注册一个事件处理器。
虽然我不会为此提倡 event
处理程序。虽然我不知道你想要实现什么,但我宁愿做一些事情,比如将 ViewModel 对象传递给 SecondPage
,而主要的 window 可以响应该 ViewModel 的状态变化。
在 WPF 中,我总是以 零 代码隐藏为目标。
作为对讨论的回应,一个虚拟机如何引用另一个虚拟机。首先传入VM:
SecondPageViewModel spvm;
public SecondPage(SecondPageViewModel model)
{
spvm = model;
this.DataContext = spvm;
InitializeComponent();
}
然后SecondpageVM将一个MainVM作为constuctor中的参数:
SecondPage sp = new SecondPage(new SecondPageViewModel(mainVM));
主要模型的更新在 SecondPageViewModel
内完成。第二页本身没有引用它。
ThirdPage tp = new ThirdPage(new ThirdPageViewModel(spvm))
第三页虚拟机可以通过第二页虚拟机上的属性访问主页虚拟机:spvm.MainVm
在 MainWindow
中,您正在尝试订阅 Window 方法,而不是您的 raiseEvent
。当然你不需要实例化另一个 MainWindow
...
您的 MainWindow
代码应该是这样的:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public void raiseEventFromSecondPage_EventHandler()
{
MessageBox.Show("MAINWINDOW Event Fired");
}
private void Button_Click(object sender, RoutedEventArgs e)
{
SecondPage sp = new SecondPage();
sp.raiseEventFromSecondPage += raiseEventFromSecondPage_EventHandler();
sp.Show();
}
}
然后您需要 SecondPage
公开 raiseEvent
。这将是与其 ViewModel 中的事件不同的事件,但您将两者链接起来。
public partial class SecondPage : Window
{
SecondPageViewModel spvm = new SecondPageViewModel();
public event raiseEventEventHandler raiseEventFromSecondPage;
public SecondPage()
{
this.DataContext = spvm;
spvm.raiseEvent += raiseEvent_EventHandler;
InitializeComponent();
}
public void raiseEvent_EventHandler()
{
if (raiseEventFromSecondPage != null)
raiseEventFromSecondPage();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
spvm.raiseEventActivate();
}
}
我遇到了事件问题。我得到了第一个 Window,看起来像这样:
using System.Windows;
namespace EventsTests
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
/*Binding Event to MainWindow
dont work until you will help*/
MainWindow mw = new MainWindow();
mw.RaiseEvent += raiseEvent_EventHandler;
}
public void raiseEvent_EventHandler()
{
MessageBox.Show("MAINWINDOW Event Fired");
}
private void Button_Click(object sender, RoutedEventArgs e)
{
SecondPage sp = new SecondPage();
sp.Show();
}
}
}
现在第二个页面做的不多:
using System.Windows;
namespace EventsTests
{
/// <summary>
/// Interaction logic for SecondPage.xaml
/// </summary>
public partial class SecondPage : Window
{
SecondPageViewModel spvm = new SecondPageViewModel();
public SecondPage()
{
this.DataContext = spvm;
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
spvm.raiseEventActivate();
}
}
}
最后我有了 SecondPageViewModel:
namespace EventsTests
{
public delegate void raiseEventEventHandler();
class SecondPageViewModel
{
public event raiseEventEventHandler raiseEvent;
public void raiseEventActivate()
{
if(raiseEvent != null)
{
raiseEvent();
}
}
}
}
现在我想,当我单击第二页上的按钮时,事件将被触发,MainWindow 会识别该事件。 使用此代码我得到错误:
错误 1 无法分配给 'RaiseEvent' 因为它是 'method group'
有人可以帮助我吗?或者举个例子?
感谢您的每一个提示 ;)
RaiseEvent
不是你的事件,它是Window
的一个方法。
我想你想这样做:
SecondPage sp = new SecondPage();
sp.raiseEvent += raiseEvent_EventHandler;
sp.Show();
即用第二个页面事件注册一个事件处理器。
虽然我不会为此提倡 event
处理程序。虽然我不知道你想要实现什么,但我宁愿做一些事情,比如将 ViewModel 对象传递给 SecondPage
,而主要的 window 可以响应该 ViewModel 的状态变化。
在 WPF 中,我总是以 零 代码隐藏为目标。
作为对讨论的回应,一个虚拟机如何引用另一个虚拟机。首先传入VM:
SecondPageViewModel spvm;
public SecondPage(SecondPageViewModel model)
{
spvm = model;
this.DataContext = spvm;
InitializeComponent();
}
然后SecondpageVM将一个MainVM作为constuctor中的参数:
SecondPage sp = new SecondPage(new SecondPageViewModel(mainVM));
主要模型的更新在 SecondPageViewModel
内完成。第二页本身没有引用它。
ThirdPage tp = new ThirdPage(new ThirdPageViewModel(spvm))
第三页虚拟机可以通过第二页虚拟机上的属性访问主页虚拟机:spvm.MainVm
在 MainWindow
中,您正在尝试订阅 Window 方法,而不是您的 raiseEvent
。当然你不需要实例化另一个 MainWindow
...
您的 MainWindow
代码应该是这样的:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public void raiseEventFromSecondPage_EventHandler()
{
MessageBox.Show("MAINWINDOW Event Fired");
}
private void Button_Click(object sender, RoutedEventArgs e)
{
SecondPage sp = new SecondPage();
sp.raiseEventFromSecondPage += raiseEventFromSecondPage_EventHandler();
sp.Show();
}
}
然后您需要 SecondPage
公开 raiseEvent
。这将是与其 ViewModel 中的事件不同的事件,但您将两者链接起来。
public partial class SecondPage : Window
{
SecondPageViewModel spvm = new SecondPageViewModel();
public event raiseEventEventHandler raiseEventFromSecondPage;
public SecondPage()
{
this.DataContext = spvm;
spvm.raiseEvent += raiseEvent_EventHandler;
InitializeComponent();
}
public void raiseEvent_EventHandler()
{
if (raiseEventFromSecondPage != null)
raiseEventFromSecondPage();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
spvm.raiseEventActivate();
}
}