1. Caliburn是什么?
Caliburn是Rob Eisenberg在2009年1月26日(Rob's MIX10 talk "Build Your Own MVVM Framework")提出的一個MVVM類的開源框架。它是一套用於協助開發WPF,Silverlight,WP7和Win RT等的應用程序的庫。
Caliburn.Micro由Rob Eisenberg於2010年6月7日正式發布。
Caliburn.Micro是一個小而強大的框架,專為在所有XAML平台上構建應用程序而設計。憑借對MVVM和其他經證明的UI模式的強大支持,Caliburn.Micro將使你能夠快速構建Solution,而無需犧牲代碼質量和可測試性
2. 項目創建:
step1:創建工程,使用NuGet包管理工具為當前項目安裝Caliburn.Micro
step2:項目創建:
新建StartView.xaml
刪除項目根目錄下的MainWindow.xaml
修改 App.xaml 刪除StartupUri="MainWindow.xmal"。
<Window x:Class="WpfApp8.StartView" 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:WpfApp8" mc:Ignorable="d" Title="StartView" Height="450" Width="800"> <Grid Background="Gray"> <Button x:Name="testBtn" Content="testBtn" HorizontalAlignment="Center" VerticalAlignment="Center" Width="100" Height=" 50" Background="LightCyan"/> </Grid> </Window>
新建StartViewModel.cs
using Caliburn.Micro; using System.Windows; namespace WpfApp8 { class StartViewModel : Screen { public StartViewModel() { } public void testBtn() { MessageBox.Show("hello world!"); } } }
新建一個類繼承BootstrapperBase,這里我命名為MyBootstrapper
using Caliburn.Micro; using System.Windows; namespace WpfApp8 { class MyBootstrapper : BootstrapperBase { public MyBootstrapper() { Initialize();//初始化框架 } protected override void OnStartup(object sender, StartupEventArgs e) { DisplayRootViewFor<StartViewModel>();//顯示界面 } } }
運行結果:
代碼下載鏈接:
鏈接:https://pan.baidu.com/s/1tZlvSWOxxSZDIA1gMuITsQ
提取碼:添加小編微信zls20210502獲取!