基于WPF的MVVM架构实践

MVVM其实网上描述很多了,这里具体原理就不加赘述了,直接贴一下基础架构,同时这个也会在之后的实例中用到这里这个实践框架:

首先ViewBase:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;

namespace HarlyApp.BaseFiles
{
    public class MyViewBase : Window ,IView
    {
        public void ShowWindow()
        {
            this.ShowDialog();
        }

        public void CloseWindow()
        {
            this.Close();
        }
    }
}

然后IView:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HarlyApp.BaseFiles
{
    public interface IView
    {
        void ShowWindow();
        void CloseWindow();
    }

    public interface IView<T> : IView
    {
        object DataContext { get; set; }
    }
}

然后PresenterVBase

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Threading.Tasks;
using System.Windows.Threading;

namespace HarlyApp.BaseFiles
{
    public class PresenterBase<T>
    {
        public IView<T> view { get; set; }

        private ICommand _cancelCommand;
        public ICommand CancelCommand
        {
            get
            {
                if (_cancelCommand == null)
                {
                    _cancelCommand = new RelayCommand(() => this.CloseWindow());
                }
                return _cancelCommand;
            }
        }

        public PresenterBase(IView<T> view)
        {
            this.view = view;
            this.Initialize();
        }

        public void ShowDialog()
        {
            this.view.ShowWindow();
        }

        protected virtual void Initialize()
        { }

        public void CloseWindow()
        {
            this.view.CloseWindow();
        }
    }
}
RelayCommand:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Diagnostics;

namespace HarlyApp.BaseFiles
{
    public class RelayCommand : ICommand
    {
        public RelayCommand(Action execute)
            : this(execute, null)
        {
        }

        public RelayCommand(Action execute, Func<bool> canExecute)
        {
            if (execute == null)
                throw new ArgumentNullException("cannot execute");

            _execute = execute;
            _canExecute = canExecute;
        }

        [DebuggerStepThrough]
        public bool CanExecute(object parameter)
        {
            return _canExecute == null ? true : _canExecute();
        }

        public event EventHandler CanExecuteChanged
        {   
            add
            {
                if (_canExecute != null)
                    CommandManager.RequerySuggested += value;
            }
            remove
            {
                if (_canExecute != null)
                    CommandManager.RequerySuggested -= value;
            }
        }

        public void Execute(object parameter)
        {
            _execute();
        }

        readonly Action _execute;
        readonly Func<bool> _canExecute;
    }
}

MainWindow:

<myView:MyViewBase x:Class="HarlyApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:myView ="clr-namespace:HarlyApp.BaseFiles"
        >
    <Grid>
        <Button Width="200" Height="46" Command="{Binding CancelCommand}" Margin="0,154,0,0"></Button>
    </Grid>
</myView:MyViewBase>

MainPresenter:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HarlyApp.BaseFiles;
using System.Windows.Input;
using System.Windows;
using DataBaseEntity;
using System.Collections.ObjectModel;

namespace HarlyApp
{
    public class MainWindowPresenter : PresenterBase<MainWindow>
    {
        public MainWindowPresenter(IView<MainWindow> view)
            : base(view)
        {
            this.view.DataContext = this;
        }

        protected override void Initialize()
        {

        }
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值