码迷,mamicode.com
首页 > 移动开发 > 详细

windows universal app中使用mvvm light

时间:2014-08-23 22:52:11      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   使用   io   for   ar   

新建空的universal app project,在windows 8.1 和 windows phone 8.1 的project中分别添加“MVVM Light libraries only (PCL) NuGet” 包

在shared project 中新建view model 的class

bubuko.com,布布扣
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Text;

namespace Universal_mvvmlight.ViewModel
{
    public class MainVM : ViewModelBase
    {
        private string mTitle = "helloworld";

        public string Title
        {
            get { return mTitle; }
            set
            {

                mTitle = value;

                RaisePropertyChanged("Title");
            }
        }

        private RelayCommand mButtonClick;

        public RelayCommand ButtonClick
        {
            get
            {
                return mButtonClick ?? (mButtonClick = new RelayCommand(() =>
                {


                    this.Title = "Fuck The World";
                }));
            }
        }


    }
}
MainVM

不用view model locator ,直接在code behind中绑定view model

 public MainPage()
        {
            this.InitializeComponent();
            this.DataContext = new MainVM();
        }

接下来在xaml中绑定

<StackPanel Orientation="Vertical" VerticalAlignment="Center" HorizontalAlignment="Center">
            <TextBlock FontSize="64" Foreground="White" Text="{Binding Title}" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>
            <Button Command="{Binding ButtonClick}" Content="click me" FontSize="64"></Button>
        </StackPanel>

 

bubuko.com,布布扣

英文原文链接:http://dontcodetired.com/blog/post/Using-MVVM-Light-in-Universal-Windows-Apps.aspx

windows universal app中使用mvvm light

标签:style   blog   http   color   os   使用   io   for   ar   

原文地址:http://www.cnblogs.com/Zhaowh/p/3931867.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!