zoukankan      html  css  js  c++  java
  • 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 你会看到如下图

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

  • 相关阅读:
    JQuery学习之语法
    sysbench
    fio——IO基准测试
    Python待分析的模块
    Taglist
    tcprstat
    SQL注入
    Nytro MegaRaid
    dstat
    Python之hashlib模块
  • 原文地址:https://www.cnblogs.com/iiixxxiii/p/2495585.html
Copyright © 2011-2022 走看看