MVVM - 使用ICommand在视图模型之间传递参数 [英] MVVM - Pass parameter between viewmodels using ICommand

查看:128
本文介绍了MVVM - 使用ICommand在视图模型之间传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,大家好,



我已经实现了ICommand,但只能在ViewModel中使用它。



例如我在视图中的
我有一个带命令定义的按钮:

Hello, everyone,

I have already implemented ICommand, but only use it within a ViewModel.

E.g.
in my view I have a button with command definition:

            <Button x:Name="BT_Save" 
                    HorizontalAlignment="Left" 
                    VerticalAlignment="Top" 
                    Width="50" Height="50" 
                    Background="{x:Null}"
                    Command="{Binding SaveCommand}"

在我的ViewModel中我有:

in my ViewModel I have:

Public Property SaveCommand As TC2.Command.FormCommands
.
.
SaveCommand = New TC2.Command.FormCommands(AddressOf OnSave, AddressOf CanSave)
.
.
        Private Sub OnSave()
.
.
        End Sub

        Private Function CanSave() As Boolean
.
.
        End Function




当然一切正常!

现在我必须在主表单和用户控件之间共享信息。在我的< g class =" gr_ gr_63 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace"数据-GR-ID =" 63" ID = QUOT; 63"> mainFrm< / g取代;
我的主窗口上有一个包含不同用户名的列表视图。如果从主窗体上的列表视图中选择用户,则用户控件应该知道这一点并自动显示该用户的数据。



我认为最好的方法是通过事件和代表来解决这个问题。例如,如果我从列表中选择了某些内容,则会触发在用户控件中接收的事件。这是一个很好的解决方案,因为我有更多的用户控件需要响应
到事件。



好​​吧,我以为我已经可以使用我的ICommand实现了为了这?那可能吗?或者我是否必须在主窗体中自行添加事件并在用户控件中监听它们?

Now I have to share information between my main form and User-control. In my <g class="gr_ gr_63 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="63" id="63">mainFrm</g> I have a list view with different user-names on my main window. If a user is selected from the list view on the main form, User-control should know this and display data from this user automatically.

I thought the best way would be to solve this via events and delegates. For example, if I selected something from the list, an event is triggered that is received in the user control. This is a good solution because I have more user controls that need to respond to the event.

Well, I thought I could already use my ICommand implementation for this? Is that possible? Or do I have to add the events myself in the main form and listen for them in the user control?

我的ICommand实现是:

My ICommand Implementation is:

    Public Class FormCommands : Implements ICommand

        Private ReadOnly _methodToExecute As Action
        Private ReadOnly _canExecuteEvaluator As Func(Of Boolean)

        Public Custom Event CanExecuteChanged As EventHandler Implements ICommand.CanExecuteChanged
            AddHandler(value As EventHandler)
                AddHandler CommandManager.RequerySuggested, value
            End AddHandler
            RemoveHandler(value As EventHandler)
                RemoveHandler CommandManager.RequerySuggested, value
            End RemoveHandler
            RaiseEvent(sender As Object, e As EventArgs)
            End RaiseEvent
        End Event

        Public Sub New(methodToExecute As Action, canExecuteEvaluator As Func(Of Boolean))
            _methodToExecute = methodToExecute
            _canExecuteEvaluator = canExecuteEvaluator
        End Sub
        Public Sub New(methodToExecute As Action)
            Me.New(methodToExecute, Function() True)
        End Sub
        Public Function CanExecute(parameter As Object) As Boolean Implements ICommand.CanExecute
            Return _canExecuteEvaluator.Invoke()
        End Function
        Public Sub Execute(parameter As Object) Implements ICommand.Execute
            _methodToExecute.Invoke()
        End Sub

        Public Sub RaiseCanExecuteChanged()
            RaiseEvent CanExecuteChanged(Me, EventArgs.Empty)
        End Sub

    End Class

提前谢谢

Dario




推荐答案

你用什么工具包来实现mvvm模式?

what toolkit do you use for implements the mvvm pattern?

如果你使用 MVVM Light ,你可以看到这个
示例

if you use MVVM Light, you can see this example.


这篇关于MVVM - 使用ICommand在视图模型之间传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆