用Prism架構重新實現Hello MVVMMy First MVVM DemoPrepare for Prism

來源:互聯網
上載者:User

前面的博文My First MVVM Demo,DebugLZQ用MVVM實現了一個簡單的Demo。下面來用Prism架構重新實現,來感受Prism帶來的方便!關於Prism可以參考DebugLZQ前面的博文Prepare for Prism。

建立一個項目,並在項目中添加相應的檔案夾。因為這個項目只有MainWindow一個View所以沒有添加Views檔案夾,也沒有Model,因此也沒有Modes檔案夾。我們只添加一個ViewModels檔案夾,並在其中添加一個MainWindowViewModel類。

為了使用Prism架構提供的DelegateCommand、NotificationObject需要添加Prism的引用,參考前面的博文。

實現MainWindowViewModel類如下:

using System;using Microsoft.Practices.Prism.ViewModel;//using Microsoft.Practices.Prism.Commands;//namespace MVVMPro1Prism.ViewModels{    class MainWindowViewModel : NotificationObject    {        //MainWindow有2個輸入,1個輸出,對應3個“資料屬性”        private string input1;        public string Input1        {            get { return input1; }            set            {                input1 = value;                RaisePropertyChanged("Input1");            }        }        private string input2;        public string Input2        {            get { return input2; }            set            {                input2 = value;                RaisePropertyChanged("Input2");            }        }        private string output1;        public string Output1        {            get { return output1; }            set            {                output1 = value;                RaisePropertyChanged("Output1");            }        }        //一個Button,一共一個“命令屬性”        public DelegateCommand AddCommand { get; set; }        private void Add()        {            Output1 = Input1 + Input2;        }        //Ctor中進行關聯        public MainWindowViewModel()        {                       AddCommand = new DelegateCommand(new Action(Add));        }    }}

注意Prism架構提供的方法的的使用方法!!(key point!)

在View中添加Binding(和前面的一樣):

<Window x:Class="MVVMPro1Prism.MainWindow"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        Title="MainWindow" Height="350" Width="525">    <Grid>        <Grid.RowDefinitions>            <RowDefinition Height="40"/>            <RowDefinition Height="40"/>            <RowDefinition Height="40"/>            <RowDefinition Height="*"/>        </Grid.RowDefinitions>        <TextBox Grid.Row="0" FontSize="26" Text="{Binding Input1}"/>        <TextBox Grid.Row="1" FontSize="26" Text="{Binding Input2}"/>        <TextBox Grid.Row="2" FontSize="26" Text="{Binding Output1}"/>        <Button  Grid.Row="3" Content="Add" Command="{Binding AddCommand}"  />    </Grid></Window>
using System.Windows;using MVVMPro1Prism.ViewModels;namespace MVVMPro1Prism{    /// <summary>    /// MainWindow.xaml 的互動邏輯    /// </summary>    public partial class MainWindow : Window    {        public MainWindow()        {            InitializeComponent();            //            this.DataContext = new MainWindowViewModel();        }    }}

OK,完成了Run run看:

程式正常運行,Prism架構確實為MVVM模式提供了便利!我們需要做的是,遵循Prism架構提供的方法規範,正確快捷的開發WPF MVVM程式。

-------------------

本文利用Prism提供的DelegateCommand、NotificationObject,簡化MVVM編寫。

Prism提供的Hello World Demo(黃色矩形框)如下:

其目錄結構如下:

Download

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.