回复:WPF中的MVVM测试

已锁定

宝冬

  • 帖子

    197
  • 精华

    26
  • 被关注

    248

论坛等级:奇侠

注册时间:2016-07-06

黄金 黄金 如何晋级?

发布于 2020-02-01 22:25:08

7楼

3.3 通过自定义对象,处理View中的事件。这里用了另一个界面来测试,只有一个按钮来触发事件。


界面的Xaml代码如下:


<Window x:Class="WpfApp02.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:i="http://schemas.microsoft.com/expression/2010/interactivity"        

        xmlns:local="clr-namespace:WpfApp02"

        mc:Ignorable="d"

        Title="MainWindow" Height="145" Width="220">

    <Window.DataContext>

        <local:MainWindowViewModel/>

    </Window.DataContext>

    <Grid>

        <Button Content="Button" Name="button1" VerticalAlignment="Center" Width="100">

            <i:Interaction.Triggers>

                <i:EventTrigger EventName="Click">                    

                    <local:ExInvokeCommandAction 

                        Command="{Binding ClickCommand}"  

                        CommandParameter="{Binding ElementName=button1}" />

                </i:EventTrigger>

            </i:Interaction.Triggers>

        </Button>

    </Grid>

</Window>


与MainWindow对应的MainWindowViewModel的C#代码如下:


using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Windows.Input;

using Prism.Commands;

using System.Windows;


namespace WpfApp02

{

    public class MainWindowViewModel

    {

        public ICommand ClickCommand

        {

            get

            {

                return new DelegateCommand<ExCommandParameter>((p) =>

                {

                    RoutedEventArgs args = p.EventArgs as RoutedEventArgs;

                    MessageBox.Show(args.ToString());

                },

                (p) =>

                {

                    return true;

                }

                );

            }

        }

    }

}


这里只是简单展示ViewModel得到的来自UI的消息性质。实用中把消息拆包,会根据内容做出不同的响应。




评论
编辑推荐: 关闭

请填写推广理由:

本版热门话题

SIMATIC WinCC / Panel

共有32566条技术帖

相关推荐

热门标签

相关帖子推荐

guzhang

恭喜,你发布的帖子

评为精华帖!

快扫描右侧二维码晒一晒吧!

再发帖或跟帖交流2条,就能晋升VIP啦!开启更多专属权限!

  • 分享

  • 只看
    楼主

top
您收到0封站内信:
×
×
信息提示
很抱歉!您所访问的页面不存在,或网址发生了变化,请稍后再试。