在MVVM中加载界面后执行方法或者事件(行为behaviour)

这里主要说的是WPF,并且是prism框架中使用。如果不是prism框架也是可以的,代码中,把关于prism的框架去掉即可。

业务:比如我们加载界面完成后,让界面默认执行一个方法,或者执行一个点击事件。

1.首先安装prism的框架,建立好MVVM模式,这里就不说了。

2.在项目中加入2个重要的dll

Microsoft.Expression.Interactions

System.Windows.Interactivity

最终结构是:

3.前台界面代码

<Window x:Class="WpfApp1.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:prism="http://prismlibrary.com/"
        prism:ViewModelLocator.AutoWireViewModel="True"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" 
        mc:Ignorable="d" Title="MainWindow">
    <i:Interaction.Triggers>
        <!--这里必须是Load,否则报错-->
        <i:EventTrigger EventName="Loaded" >
            <!--View_Loaded关联VM中的View_Loaded事件-->
            <ei:CallMethodAction MethodName="View_Loaded" TargetObject="{Binding}"  />
        </i:EventTrigger>
    </i:Interaction.Triggers>
    <Grid>
        <StackPanel Orientation="Vertical" >
            <Button Content="打开A" Margin="5" Command="{Binding OpenCommand}" CommandParameter="ViewA">
                <i:Interaction.Triggers>
                    <!--这里必须是Load,否则报错-->
                    <i:EventTrigger EventName="Loaded"  >
                        <!--ButtonA_Click关联VM中的ButtonA_Click事件-->
                        <ei:CallMethodAction MethodName="ButtonA_Click" TargetObject="{Binding}"  />
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </Button>
        </StackPanel>
    </Grid>
</Window>

4.MainWindowViewModel层代码

using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace WpfApp1
{
    public class MainWindowViewModel : BindableBase
    {
        public DelegateCommand<string> OpenCommand { get; set; }

        public MainWindowViewModel()
        {
            OpenCommand = new DelegateCommand<string>(Open);
        }

        private void Open(string obj)
        {
            MessageBox.Show("Test  " + obj);
        }

        public void View_Loaded(object sender, EventArgs e)
        {
            MessageBox.Show("123");
        }

        public void ButtonA_Click(object sender, EventArgs e)
        {
            //这里按照事件理解,e就是参数,也可以删除简写,加上引用
            Open(((System.Windows.Controls.Primitives.ButtonBase)((RoutedEventArgs)e).Source).CommandParameter.ToString());
        }
    }
}

5.当我们运行的时候,先弹View_Loaded里面的内容,再弹ButtonA_Click里面的内容 ,效果。

注意:上面的方法可能出现版本的错误,使用下面方法:

1.建立好项目后,安装Microsoft.xaml.Behaviors

2.View界面代码

<Window x:Class="rwerw.MainWindow1"
        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:local="clr-namespace:rwerw" 
        xmlns:behaviour="http://schemas.microsoft.com/xaml/behaviors"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Button Content="Button" HorizontalAlignment="Left" Margin="229,218,0,0" VerticalAlignment="Top" Width="75">
            <behaviour:Interaction.Triggers>
                <behaviour:EventTrigger EventName="Click">
                    <behaviour:CallMethodAction TargetObject="{Binding}" MethodName="Button_Click" />
                </behaviour:EventTrigger>
            </behaviour:Interaction.Triggers>
        </Button>

    </Grid>
</Window>

3.VM中的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace rwerw.ViewModels
{
    public class MainViewModel
    {

        public void Button_Click(object sender, EventArgs e)
        {
            MessageBox.Show("123");
        }
    }
}

 注意点:

1.引用 xmlns:behaviour="http://schemas.microsoft.com/xaml/behaviors"

2.VM中写public, public void Button_Click(object sender, EventArgs e)

3.V和VM之间关联,   this.DataContext = new MainViewModel();

来源:在MVVM中加载界面后执行方法或者事件(行为behaviour)-CSDN博客

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

故里2130

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值