图片展示网站织梦源码,网页美工设计岗前培训,免费的网络推广平台,2016响应式网站模板前言
前面的文章里面我们有介绍在Wpf中如何在View层将事件映射到ViewModel层的文章#xff0c;传送门#xff0c;既然WPF和Avalonia是两套不同的前端框架#xff0c;那么WPF里面实现模式肯定在这边就用不了#xff0c;本篇我们将分享一下如何在Avalonia前端框架下面将事件…前言
前面的文章里面我们有介绍在Wpf中如何在View层将事件映射到ViewModel层的文章传送门既然WPF和Avalonia是两套不同的前端框架那么WPF里面实现模式肯定在这边就用不了本篇我们将分享一下如何在Avalonia前端框架下面将事件映射到ViewModel层。本章内容还是在上一节的基础上做扩展讲解。Avalonia中使用Prism实现区域导航功能
安装行为扩展
在Avalonia框架下面有它自己的行为扩展我们需要借助这些扩展库里面的行为扩展来实现我们今天要讲解的功能。
dotnet add package Avalonia.Xaml.Behaviors --version 11.0.5编写View层的代码
我们先来一个简单的页面加载事件的触发View层代码如下
UserControl xmlnshttps://github.com/avaloniauixmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:dhttp://schemas.microsoft.com/expression/blend/2008xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006xmlns:prismhttp://prismlibrary.com/xmlns:iclr-namespace:Avalonia.Xaml.Interactivity;assemblyAvalonia.Xaml.Interactivityxmlns:iaclr-namespace:Avalonia.Xaml.Interactions.Core;assemblyAvalonia.Xaml.Interactionsprism:ViewModelLocator.AutoWireViewModelTruemc:Ignorabled d:DesignWidth800 d:DesignHeight450x:ClassAvaloniaTest.Views.ViewB BackgroundGreeni:Interaction.Behaviorsia:EventTriggerBehavior EventNameLoadedia:InvokeCommandAction Command{Binding OnLoad}/ia:InvokeCommandAction/ia:EventTriggerBehavior/i:Interaction.BehaviorsStackPanelTextBlock Text{Binding Title}/TextBlock/StackPanel
/UserControlxmlns:iclr-namespace:Avalonia.Xaml.Interactivity;assemblyAvalonia.Xaml.Interactivityxmlns:iaclr-namespace:Avalonia.Xaml.Interactions.Core;assemblyAvalonia.Xaml.Interactions这段代码的意思是在头部引入了事件行为用到的两个命名空间Avalonia.Xaml.Interactivity和Avalonia.Xaml.Interactions。 i:Interaction.Behaviorsia:EventTriggerBehavior EventNameLoadedia:InvokeCommandAction Command{Binding OnLoad}/ia:InvokeCommandAction/ia:EventTriggerBehavior/i:Interaction.Behaviors这段代码的意思是我们给Interactivity的Interaction.Behaviors属性赋值这里面我们用到了Avalonia.Xaml.Interactions里面的EventTriggerBehavior字面意思是“事件触发行为”我们设置EventName属性为Loaded这个是axalm的前端加载事件名称。
编写ViewModel层的代码
using Prism.Commands;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace AvaloniaTest.ViewModels
{public class ViewBViewModel : ViewModelBase, INavigationAware{private string _title ViewB;public string Title{get _title;set{SetProperty(ref _title, value);}}public bool IsNavigationTarget(NavigationContext navigationContext){return true;}public void OnNavigatedFrom(NavigationContext navigationContext){}public void OnNavigatedTo(NavigationContext navigationContext){}private DelegateCommand _onLoad;public DelegateCommand OnLoad _onLoad ?? (_onLoadnew DelegateCommand(() {Debug.WriteLine(OnLoad is run!);}));}
}
相比较上一篇的内容其实就多了一个命令属性OnLoad
运行程序查看效果果然在导航页面加载的时候会进入该方法完美。