Caliburn.Micro for wp7 学习笔记(4) : 自由添加事件绑定1

打开工具箱,拖动一个Border 到页面上 Border:  Name="border1"  Background="Red"  ,我们的目标是点击这个 Border 弹出一个MessageBox 提示"你点击了Border"

在 MainPageViewModel.cs 添加代码

        public void border1()
        {

            MessageBox.Show("你点击了Border");

        }

运行后点击Border你会发没有任何反应,那是因为根据框架默认的规则,没有Border这个类型的空间

进入文件夹 Caliburn.Micro v1.3.1\samples\Caliburn.Micro.HelloWP71 打开这个 例子

打开AppBootstrapper.cs文件 查看   AddCustomConventions() 方法

            ConventionManager.AddElementConvention<Pivot>(Pivot.ItemsSourceProperty, "SelectedItem", "SelectionChanged").ApplyBinding =
                (viewModelType, path, property, element, convention) => {
                    if(ConventionManager
                        .GetElementConvention(typeof(ItemsControl))
                        .ApplyBinding(viewModelType, path, property, element, convention)) {
                        ConventionManager
                            .ConfigureSelectedItem(element, Pivot.SelectedItemProperty, viewModelType, path);
                        ConventionManager
                            .ApplyHeaderTemplate(element, Pivot.HeaderTemplateProperty, null, viewModelType);
                        return true;
                    }

                    return false;
                };

  

这个方法是向框架注册控件事件

双击   ConventionManager.AddElementConvention<Pivot>(Pivot.ItemsSour...........  中的AddElementConvention 选中它按F12键导航到这个方法的定义,我们可以看到这个方法的注释

        //摘要:
        //添加一个元素公约。
        //
        //参数:
        // bindableProperty:
        //默认属性约束力的公约。
        //
        // parameterProperty:
        //动作参数的默认属性。
        //
        // eventName:
        //默认的事件来触发动作。
        //
        //类型参数:
        // T:
        //元素的类型。
public static ElementConvention AddElementConvention<T>(DependencyProperty bindableProperty, string parameterProperty, string eventName);

使用这个方法来注册控件很简单 

 我们在Bootstrapper.cs 里添加一个AddCustomConventions()方法,内容如下

static void AddCustomConventions() {
      
            ConventionManager.AddElementConvention<Border>(Border.DataContextProperty, "DataContext", "Tap");

        }

  然后运行程序,点击 Border 你可看到如下图

 

 注册了这个规则后,以后解析 Border 时候都会按照这个规则来解析

现在实验一下,我们重新从工具箱中拖出一个 Border 到页面上 Border:  Name="border2"  Background="Blue" 

然后在MainPageViewModel.cs 里添加

       public void border2()
        {

            MessageBox.Show("你点击了border2");

        }

  然后运行,后点击 border2 你会看到如下图

 

 下一节我们说下其他的绑定方式

 

转载于:https://www.cnblogs.com/iiixxxiii/archive/2012/05/13/2495585.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值