zoukankan      html  css  js  c++  java
  • 引用mvvmlight dll ,操作command

    前言

    因为vs2010没有集成mvvmlight 所以想要使用mvvmlight的relaycomman需要引用dll
    需要测试某个功能的时候,不能进行快带的集成

    引用mvvmlight dll

    如果以前有其它版本的vs能过nuget安装过,则目录 是:

    c:usersadministrator.xdmwiwrtm7kc37ydocumentsvisual studio 2013ProjectsWpfApplication2packagesMvvmLightLibs.4.2.30.0lib
    et40
    

    引用

    GalaSoft.MvvmLight.WPF4.dll
    
    

    如何实现

    xaml中的代码如下:

    放一个button ,指定Button的命令是TestN, 这里是随便写的,全符合命名规范

    注意:Datacontext的指定 ,如果不指定 ,命令不会触发。这里不加新类,直接写在CS里面。

    就是为了测试而测试

    <Window x:Class="WpfApplication2.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:wpfApplication2="clr-namespace:WpfApplication2"
            Title="MainWindow" Height="350" Width="525"
            DataContext="{Binding RelativeSource={RelativeSource Self}}"
            >
        <Grid >
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Button Command="{Binding TestN}" Width="100" Height="100" Content="wefwfwef"/>
        </Grid>
    
    </Window>
    

    注意:
    如果没有以下这句,会报错

     DataContext="{Binding RelativeSource={RelativeSource Self}}"
    

    cs后端的代码如下

      public partial class MainWindow : Window
        {
            private RelayCommand _testN;
    
            public MainWindow()
            {
                InitializeComponent();
                //DataContext = this;
            }
    
            public RelayCommand TestN
            {
                get { return _testN??(_testN=new RelayCommand(() =>
                {
                    TestNMethod();
                })); }
                set { _testN = value; }
            }
    
            private void TestNMethod()
            {
                throw new NotImplementedException();
            }
        }
    
    

    注意:
    如果xaml中没有指定 datacontxt,则要在构造 函数中指定datacontext是本类。

    调用以后TestNMethod()方法会被触发。

  • 相关阅读:
    (组件、路由)懒加载
    vue.js实现用户评论、登录、注册、及修改信息功能
    vue 路由传参 params 与 query两种方式的区别
    WebSocket入门教程(五)-- WebSocket实例:简单多人聊天室
    js系列教程11-json、ajax(XMLHttpRequest)、comet、SSE、WebSocket全解
    回忆一下跨域
    Http,Socket,TCP/IP 协议简述
    Vue+WebSocket 实现页面实时刷新长连接
    微信小程序JS导出和导入
    Vue学习之路之登录注册实例代码
  • 原文地址:https://www.cnblogs.com/hsapphire/p/8886424.html
Copyright © 2011-2022 走看看