WPF MVVM编程实践--使用Caliburn.Micro搭建MVVM框架

目录

1. NuGet安装依赖Caliburn.Micro 

2. 创建文件夹Views/ViewModels/Models

3. 删除MainModelView.xmal及MainModelView.xmal.cs

4. 删除App.xaml的startupUri

5. 创建ViewModels/ShellViewModel类

6. 创建ShellView.xaml

7. 创建Bootstrapper类

8. 编辑App.xaml

9. 源码


1. NuGet安装依赖Caliburn.Micro 

2. 创建文件夹Views/ViewModels/Models

3. 删除MainModelView.xmal及MainModelView.xmal.cs

4. 删除App.xaml的startupUri

<Application x:Class="CMDemo.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:CMDemo">
    <Application.Resources>
        
    </Application.Resources>
</Application>

5. 创建ViewModels/ShellViewModel类

using System.ComponentModel;
using System.Threading;
using System.Windows;
using Caliburn.Micro;

namespace CMDemo.ViewModels
{
    public class ShellViewModel : Screen, INotifyPropertyChanged
    {
        private double _left;
        private double _right;
        private double _result;

        public double Left
        {
            get { return _left; }
            set
            {
                _left = value;
                NotifyOfPropertyChange();
            }
        }

        public double Right
        {
            get { return _right; }
            set
            {
                _right = value;
                NotifyOfPropertyChange();
            }
        }

        public double Result
        {
            get { return _result; }
            set
            {
                _result = value;
                NotifyOfPropertyChange();
            }
        }
        public bool CanDivide(double left, double right)
        {
            return right != 0;
        }

        public async void Divide(double left, double right)
        {
            Thread.Sleep(600);
            if (CanDivide(left, right) == true)
                Result = left / right;
            else MessageBox.Show("Divider cannot be zero.", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
        }

        public async void Plus(double left, double right)
        {
            Result = left + right;
        }

        public async void Minus(double left, double right)
        {
            Result = left - right;
        }

        public async void Multipy(double left, double right)
        {
            Result = left * right;
        }
    }
}

6. 创建ShellView.xaml

<Window x:Class="CMDemo.Views.ShellView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:CMDemo.Views"
        xmlns:cal="http://www.caliburnproject.org"
        mc:Ignorable="d"
        Title="Calculator" SizeToContent="Height" Width="240">

    <StackPanel Background="Beige">
        <StackPanel Orientation="Horizontal">
            <Label Margin="10"
                   Target="{Binding ElementName=left}">
                Operand _1:
            </Label>
            <TextBox Margin="10"
                     Width="72"
                     x:Name="left"/>
        </StackPanel>
        <StackPanel Orientation="Horizontal">
            <Label Margin="10"
                   Target="{Binding ElementName=right}">
                Operand _2:
            </Label>
            <TextBox Margin="10"
                     Width="72"
                     x:Name="right"/>
        </StackPanel>
        <StackPanel Orientation="Horizontal">
            <Button Margin="10"
                    Content="相加"
                    Background="{x:Null}"
                    x:Name="btnPlus" 
                    cal:Message.Attach="[Event Click]=[Action Plus(left.Text, right.Text):result.Text]">
            </Button>

            <Button Margin="10"
                    Content="相减"
                    Background="{x:Null}"
                    cal:Message.Attach="[Event Click]=[Action Minus(left.Text, right.Text):result.Text]">
            </Button>

            <Button Margin="10"
                    x:Name="btnMultiply" 
                    Content="相乘"
                    Background="{x:Null}"
                    cal:Message.Attach="[Event Click]=[Action Multipy(left.Text, right.Text):result.Text]">
            </Button>

            <Button Margin="10"
                    Content="相除"
                    Background="{x:Null}"
                    x:Name="btnDivide" IsEnabled="{Binding Path=CanDivide}"
                    cal:Message.Attach="[Event Click]=[Action Divide(left.Text, right.Text):result.Text]">
            </Button>

        </StackPanel>
        <StackPanel Orientation="Horizontal">
            <Label Margin="10">
                Answer:
            </Label>
            <TextBox Margin="10"
                     Width="72"
                     Text ="{Binding Path=Result, StringFormat={}{0:F4}}" IsReadOnly="True" />
        </StackPanel>
    </StackPanel>
</Window>

7. 创建Bootstrapper类

using Caliburn.Micro;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using WpfApp4.ViewModels;

namespace WpfApp4
{
    public class Bootstrapper : BootstrapperBase
    {
        public Bootstrapper()
        {
            Initialize();
        }

        protected override void OnStartup(object obj, StartupEventArgs e)
        {
            DisplayRootViewForAsync<ShellViewModel>();
        }
    }
}

8. 编辑App.xaml

<Application x:Class="CMDemo.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:CMDemo">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary>
                    <local:Bootstrapper x:Key="bootstrapper"/>
                </ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

9. 源码

源码:https://download.csdn.net/download/liugang590/87441267

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Caliburn.Micro是一套基于XAML的MVVM模式的开发框架,它是一个小巧而强大的框架。在Caliburn.Micro中,只需要按照约定将View的名字加上后缀ViewModel,就是它的ViewModel的名字,例如MainPage和MainPageViewModel。Caliburn.Micro会自动将ViewModel绑定到View的DataContext,并且如果ViewModel的属性名和控件的名称相同,它会自动进行绑定。 MVVM模式是一种用于开发WPF应用程序的架构模式,它将应用程序分为三个部分:Model、View和ViewModel。ViewModel负责处理View与Model之间的交互,以及将Model的数据呈现给View。MVVM模式的目标是编写优雅、可测试、可维护和可扩展的表示层代码,而Caliburn.Micro努力超越简单的MVVM实现,使开发变得更加容易。 在MVVM框架中,ViewModel通常需要实现INotifyPropertyChanged接口,以便在ViewModel的属性更改时能够自动发出事件通知View。在Caliburn.Micro中,ViewModel通常可以继承自PropertyChangedBase或Screen,这两个类都实现了INotifyPropertyChanged接口。如果继承自Screen,ViewModel可以像窗口一样进行激活、关闭等操作。 综上所述,Caliburn.Micro是一个基于XAML的MVVM框架,它使开发WPF应用程序变得更加简单和高效。它提供了自动绑定ViewModel到View的功能,同时也支持INotifyPropertyChanged接口,使ViewModel能够与View进行双向通信。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [C# WPF MVVM开发框架Caliburn.Micro入门介绍①](https://blog.csdn.net/zls365365/article/details/121711023)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [WPF MVVM框架Caliburn.Micro(一)简介](https://blog.csdn.net/Accidentabird/article/details/130192466)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值