博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WPF Caliburn 学习笔记(四) Message Triggers
阅读量:4563 次
发布时间:2019-06-08

本文共 3243 字,大约阅读时间需要 10 分钟。

前面我们触发事件的行为用的是普通的EventMessageTrigger 。其实在Caliburn中有三种事件触发模式:

  1. EventMessageTrigger
  2. GestureMessageTrigger
  3. AttachedEventMessageTrigger

1.EventMessageTrigger

简写:cal:Message.Attach="Divide(left.Text, right.Text) : DivideResult.Text"

详细:

2.GestureMessageTrigger

GestureMessageTrigger,比如:当我们需要按”CTRL+左击 ”时,执行事件。我们就要用到这个。

简写:

 
[Action Divide(left.Text, right.Text) : DivideResult.Text]" />

详细:

3.AttachedEventMessageTrigger

AttachedEventMessageTrigger :把事件放到父容器中。相当于共享事件。

简写:

详细:

AvailabilityEffect

AvailabilityEffect属性用来显示控件的外观。比如:显示,隐藏,修改透明度等。它有如下属性:

None,Hide,Collapse,Opacity。

这里讲下Opacity属性的用法。

首先新建一个OpacityEffect类,他必须继承IAvailabilityEffect接口。

public class OpacityEffect : IAvailabilityEffect    {        //Note: Update the UI based on the availability.        public void ApplyTo(DependencyObject target, bool isAvailable)        {            var uiElement = target as UIElement;            if(uiElement == null) return;            uiElement.Opacity = isAvailable ? 1 : .5;        }    }

然后在app.xaml.cs中,我们要在创建一个容器,然后在这个容器中注册这个属性。

 

代码 public partial class App : Application    {        public App()        {            //Note: Explicitly creating the container.            var container = new SimpleContainer();            CaliburnFramework                .ConfigureCore(container) //Note: Setting the container.                .WithPresentationFramework()                .Start();            //Note: Register the custom IAvailabilityEffect by key.            container.RegisterSingleton
("Opacity"); } }

xaml上:

效果图 下面的那个Button:

其他属性不要在容器中注册,直接设置AvailabilityEffect就行了。

UserInRoleAttribute

在前面我们执行一个函数,上面加个了  [Preview("CanXXX")]特性。用来判断该函数能否执行。

返回True,则执行该函数,反之,不执行。

还有一个特性叫:UserInRoleAttribute,该特性用来判断权限的。

我们在某个方法上加个[UserInRole("Admin", Priority = 1)]

我们要用到判别这个特性的类。

代码//Note: A custom filter that will be executed before the action.    //Note: Custom filters can also be IPostProcessor or IRescue    public class UserInRoleAttribute : Attribute, IPreProcessor    {        private readonly string _role;        public UserInRoleAttribute(string role)        {            _role = role;        }        public int Priority { get; set; }        public bool AffectsTriggers        {            get { return true; }        }        //Note:  The code that will execute before the action.        public bool Execute(IRoutedMessage message, IInteractionNode handlingNode, object[] parameters)        {            return Thread.CurrentPrincipal.IsInRole(_role);        }    }
这里的用户角色是:Admin,权限为1。通过返回Thread.CurrentPrincipal.IsInRole(_role); 来判断是否执行。

转载于:https://www.cnblogs.com/dingli/archive/2011/03/18/1987653.html

你可能感兴趣的文章
python全栈脱产第19天------常用模块---shelve模块、xml模块、configparser模块、hashlib模块...
查看>>
[LeetCode] House Robber
查看>>
virtualbox中kali虚拟机安装增强功能
查看>>
java生成六位验证码
查看>>
iOS的MVP设计模式
查看>>
stringstream
查看>>
【转】HDU 6194 string string string (2017沈阳网赛-后缀数组)
查看>>
前后端分离
查看>>
渗透测试学习 一、环境搭建
查看>>
处理图片透明操作
查看>>
Postman-OAuth 2.0授权
查看>>
mac pycharm打不开问题
查看>>
iOS项目之苹果审核被拒
查看>>
java多态及tostring方法与equals方法的重写
查看>>
python 获取远程设备ip地址
查看>>
JDBC 第五课 —— 小项目的界面升级
查看>>
团队作业3——需求改进&系统设计
查看>>
返回json格式时间,解析时间
查看>>
线程并发-栈限制&ThreadLocal
查看>>
[转]Understand QoS at OpenSwitch
查看>>