未从用户控件和主窗体处理事件

时间:2016-02-19 13:37:43

标签: wpf vb.net events mvvm event-handling

我正在尝试处理应用程序主窗体中的事件,其中所述事件是从主窗体上填充的用户控件引发的。我已经为主表单和用户控件中实现的事件定义了一个接口。我正在使用GalaSoft的MvvmLight来支持MVVM。

后面的主要表单代码表示事件是附加的,但是当我在用户控件中进行检查时,它表示事件没有附加处理程序,所以很明显,它不会到达处理程序

任何帮助都将不胜感激。

我定义的界面非常基础:

Public Interface IEventFiring
   Event EventFiring(sender As Object)
End Interface

My Main Form Xaml看起来像这样:

<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:EventFiringFromContolToMainForm"
        xmlns:efc="clr-namespace:EventFiringControl;assembly=EventFiringControl"
    Title="MainWindow">
   <Window.DataContext>
      <local:MainWindowViewModel/>
   </Window.DataContext>
    <StackPanel>
      <TextBox Text="{Binding StatusText, Mode=OneWay}"
               Height="25"
                 Margin="5"/>
       <Separator Margin="5"/>
       <efc:UserControl1/>
    </StackPanel>
</Window>

守则背后是:

Imports EventFiringControl

Class MainWindow
   Implements IEventFiring

   Public Sub New()

      ' This call is required by the designer.
      InitializeComponent()

      ' Add any initialization after the InitializeComponent() call.
      AddHandler EventFiring, AddressOf EventFiringSub

      If EventFiringEvent Is Nothing Then
         CType(DataContext, MainWindowViewModel).StatusText = "Event did NOT Attach!!"
      Else
         CType(DataContext, MainWindowViewModel).StatusText = "Event Attached"
      End If

   End Sub

   Public Event EventFiring(sender As Object) Implements IEventFiring.EventFiring

   Private Sub EventFiringSub()
      CType(DataContext, MainWindowViewModel).StatusText = "Event Fired"
   End Sub
End Class

主表单的视图模型是:

Imports GalaSoft.MvvmLight

Public Class MainWindowViewModel
   Inherits ViewModelBase

   Private _statusText As String

   Public Property StatusText As String
      Get
         Return _statusText
      End Get
      Set(value As String)
         _statusText = value
         RaisePropertyChanged(Function() StatusText)
      End Set
   End Property

End Class

现在是用户控件。

Xaml文件是:

<UserControl x:Class="UserControl1"
             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:EventFiringControl"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
   <UserControl.DataContext>
      <local:EventFiringViewModel/>
   </UserControl.DataContext>
    <Grid>
            <Button Content="Cancel" Command="{Binding CancelCommand}" Height="50"/>
    </Grid>
</UserControl>

背后的代码是:

Public Class UserControl1 
   Public Sub New()

      ' This call is required by the designer.
      InitializeComponent()

      ' Add any initialization after the InitializeComponent() call.

   End Sub
End Class

最后,用户控件的视图模型是:

Imports GalaSoft.MvvmLight.Command
Imports GalaSoft.MvvmLight

Public Class EventFiringViewModel
   Inherits ViewModelBase
   Implements IEventFiring

   Public ReadOnly Property CancelCommand As RelayCommand
      Get
         Return New RelayCommand(Sub() CancelSub())
      End Get
   End Property

   Private Sub CancelSub()
      If EventFiringEvent IsNot Nothing Then
         RaiseEvent EventFiring(Me)
      End If
   End Sub


   Public Event EventFiring(sender As Object) Implements IEventFiring.EventFiring
End Class

0 个答案:

没有答案