用于转换为 MVVM 模式的事件名称 [英] Event Names for converting into MVVM pattern
问题描述
我正在将 Silverlight 代码转换为 MVVM 模式.
I am converting silverlight code into MVVM pattern.
我想知道诸如关闭事件"之类的事件名称,我正在使用代码.
I want to know event name like for "closing event" I am using code.
<i:Interaction.Triggers>
<i:EventTrigger EventName="Closing">
<i:InvokeCommandAction Command="{Binding OnClose}" />
</i:EventTrigger>
</i:Interaction.Triggers>
我想对以下事件执行类似的任务.
I want to perform similar task on following events.
IsVisibleChanged
IsVisibleChanged
Windows_SizeChanged
Windows_SizeChanged
webBrowser1_Unloaded
webBrowser1_Unloaded
DataContextChanged
DataContextChanged
LoadingProgress_Loaded
LoadingProgress_Loaded
请帮忙.
推荐答案
好吧,我不自称是 mvvm 的专家,但我发现有效的一种方法如下.
Well I don't profess to be an expert in mvvm, but one way that I have found works is the following.
假设我的表单上有一个文本框,它的 gotFocus 和 Text 更改了我想在视图模型中处理的事件.
Let's say that I have a textbox on my form whose gotFocus and Text changed events I want to handle in my view model.
在 xaml 中,我将有以下内容:
In the xaml I would have the following:
<ribbon:TextBox x:Name="txtSubmissionSearch" Width="150" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<i:InvokeCommandAction Command="{Binding Path=SubmissionSearchTextBoxLoadedCommand}" CommandParameter="{Binding ElementName=txtSubmissionSearch}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</ribbon:TextBox>
在我的视图模型中,我为文本框定义了一个私有变量
In my viewmodel I define a private variable for the textbox
Private ersSeachSubmissionTextBox As TextBox
然后是您真正感兴趣的部分:
And then to the bit that you're really interested in:
Private _submissionSearchTextBoxLoadedCommand As ICommand
Public ReadOnly Property SubmissionSearchTextBoxLoadedCommand As ICommand
Get
If _submissionSearchTextBoxLoadedCommand Is Nothing Then
Dim mySubmissionSearchTextBoxLoaded As New Action(Of Object)(AddressOf SubmissionSearchTextBoxLoaded)
_submissionSearchTextBoxLoadedCommand = New RelayCommand(mySubmissionSearchTextBoxLoaded)
End If
Return _submissionSearchTextBoxLoadedCommand
End Get
End Property
Private Sub SubmissionSearchTextBoxLoaded(ByVal obj As Object)
ersSeachSubmissionTextBox = DirectCast(obj, TextBox)
AddHandler ersSeachSubmissionTextBox.GotFocus, AddressOf ErsSeachSubGotFocus
AddHandler ersSeachSubmissionTextBox.TextChanged, AddressOf ErsSearchSubTextChanged
End Sub
Private Sub ErsSeachSubGotFocus(ByVal sender As Object, ByVal e As RoutedEventArgs)
InvokeSubmissionEditorSearch()
End Sub
Private Sub ErsSearchSubTextChanged(ByVal sender As Object, ByVal e As TextChangedEventArgs)
InvokeSubmissionEditorSearch()
End Sub
注意:我正在使用 Josh Smith 和其他人描述的中继命令模式来处理很多这样的问题,但是在其他地方有很多关于它的信息,你很可能正在实施它反正你自己.
NB: I am using the relay command pattern as described by Josh Smith and others to handle a lot of this, but there is plenty of information available about that elsewhere and you may well be implementing it yourself anyway.
Double NB 我的 viewModels 通常是我的 Forms/UserControls 的 DataContext.如果您开始动态更改 DataContext,那么您会发现这种方法存在问题.这不是不可逾越的,您只需要仔细考虑您的 xaml 布局即可.
Double NB My viewModels are generally the DataContext for my Forms/UserControls. If you start to change your DataContext on the fly then you can find issues with this approach. It's not insurmountable, you just need to consider your xaml layout carefully.
希望这在某种程度上有所帮助
Hope this helps in some way
这篇关于用于转换为 MVVM 模式的事件名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!