当前位置:网站首页>在MVVM中加载界面后执行方法或者事件
在MVVM中加载界面后执行方法或者事件
2022-07-13 18:09:00 【故里2130】
这里主要说的是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();
边栏推荐
- Is it true that you can't do software testing until you're 35? So what should I do I'm 36
- Introduction to C language compiler
- Reptile Youdao translation
- ABAP BAPI 复制标准项目模板实现项目立项
- About pycharm localization 2020-09-20
- ABAP Bapi copy the standard project template to achieve project initiation
- WordPress Chinese website code download
- Rust Thrift Demo
- Jsonp principle
- IDEA 注释模板,这样配置才够逼格!
猜你喜欢

Are you still using the strategy mode to solve if else? Map+ functional interface method is yyds

2022/7/10RHCSA

26 years old, has worked in automation for three years, and the monthly salary is only 12K. Can you change jobs and find a higher salary job?

I2C协议

LVM and disk quota

解决nodejs中mysql查询数据中bigint类型数据精度缺失问题

Why is it said that the testing post is a giant pit? The 10-year-old tester told you not to be fooled~

2022/7/10RHCSA

Day 9 of leetcode question brushing

CCF 201909-1 称检测点查询
随机推荐
2022/7/10RHCSA
ABAP Bapi copy the standard project template to achieve project initiation
Still using enumeration in MySQL? Pay attention to these traps!
V891-z3735f redo system, drive repair summary
GoFrame Step by Step Demo - P1
Day 4 of leetcode question brushing
Day 6 of leetcode question brushing
Redis master-slave cluster construction and sentinel mode configuration
It's so delicious. I finally understand why so many people want to switch to software testing~
64位整除乘法
V891-Z3735F重做系统,驱动修复汇总
Data storage and disaster recovery (2nd Edition) editor in chief Lu Xianzhi Wu Chunling comprehensive training answer
Day 7 of leetcode question brushing
LVM and disk quota
使用Code First
项目上线之gzip和pm2管理工具的基本使用
The difference between set, map, weakset and weakmap
如何将 @Transactional 事务注解运用到炉火纯青?
CentOS 7X 安装Mysql 数据库
C notes - basics, Q & A, WPF