Caliburn.Micro是一个基于WPF和Silverlight的MVVM框架。它提供了一些方便的方法和约定,帮助开发人员更容易地实现分离视图和逻辑,使代码更加模块化、可维护和可测试。以下是一些Caliburn.Micro的入门实例,帮助您快速入门:
1. Hello World
```xml
<Window x:Class="HelloWorld.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cal="http://www.caliburnproject.org"
Title="Hello World" Height="350" Width="525">
<Grid>
<Button x:Name="SayHello" Content="Say Hello" Width="100" Height="50" Margin="10" />
</Grid>
</Window>
```
```csharp
using Caliburn.Micro;
namespace HelloWorld
{
public class MainWindowViewModel : Screen
{
public void SayHello()
{
MessageBox.Show("Hello World!");
}
}
}
```
这个例子演示了如何使用Caliburn.Micro在WPF应用程序中实现一个简单的按钮事件。在这个例子中,我们使用Caliburn.Micro提供的命名约定,将按钮的点击事件绑定到ViewModel中的处理方法。当按钮被点击时,SayHello方法将被调用,并显示一个消息框。
2. 绑定属性
```xml
<Window x:Class="DataBinding.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cal="http://www.caliburnproject.org"
Title="Data Binding" Height="350" Width="525">
<Grid>
<TextBox x:Name="InputText" Width="200" Height="25" Margin="10, 20, 10, 0" />
<Button x:Name="UpperCase" Content="Convert to UpperCase" Width="150" Height="25" Margin="10, 50, 10, 0" />
<TextBlock x:Name="OutputText" Text="{Binding Path=Result}" Width="200" Height="25" Margin="10, 90, 10, 0" />
</Grid>
</Window>
```
```csharp
using Caliburn.Micro;
namespace DataBinding
{
public class MainWindowViewModel : Screen
{
public string InputText { get; set; }
public string OutputText { get; set; }
public void UpperCase()
{
OutputText = InputText.ToUpper();
NotifyOfPropertyChange(nameof(OutputText));
}
}
}
```
这个例子演示了如何使用Caliburn.Micro在WPF应用程序中实现数据绑定。在这个例子中,我们定义了两个属性InputText和OutputText,并在View中将它们绑定到TextBox和TextBlock控件上。当用户输入文本并单击转换按钮时,UpperCasemethod将被调用,并将InputText转换为大写字母,并将结果存储在OutputText属性中。
3. 导航
```xml
<Window x:Class="Navigation.MainShellView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cal="http://www.caliburnproject.org"
Title="Navigation" Height="350" Width="525">
<Grid>
<StackPanel Orientation="Horizontal" Margin="10">
<Button x:Name="NavigateToPage1" Content="Page 1" Width="75" Height="25" Margin="5" />
<Button x:Name="NavigateToPage2" Content="Page 2" Width="75" Height="25" Margin="5" />
</StackPanel>
<ContentControl x:Name="ActiveItem" Margin="10"/>
</Grid>
</Window>
```
```csharp
using Caliburn.Micro;
namespace Navigation
{
public class MainShellViewModel : Conductor<object>.Collection.OneActive
{
public void NavigateToPage1()
{
ActivateItem(new Page1ViewModel());
}
public void NavigateToPage2()
{
ActivateItem(new Page2ViewModel());
}
}
}
```
```csharp
using Caliburn.Micro;
namespace Navigation
{
public class Page1ViewModel : Screen
{
public string PageTitle => "Page 1 Title";
}
}
```
```csharp
using Caliburn.Micro;
namespace Navigation
{
public class Page2ViewModel : Screen
{
public string PageTitle => "Page 2 Title";
}
}
```
这个例子演示了如何使用Caliburn.Micro在WPF应用程序中实现导航。在这个例子中,我们在MainShellView中使用两个按钮来导航到Page1和Page2,当单击按钮时,将ActivateItem方法用于激活一个新视图模型。在这个例子中,我们定义了两个视图模型Page1ViewModel和Page2ViewModel,它们都继承自Screen。使用Caliburn.Micro,我们可以轻松地实现视图之间的导航。
总之,以上是一些Caliburn.Micro的入门实例,其中演示了如何使用Caliburn.Micro在WPF应用程序中实现按钮事件、数据绑定和导航等功能。希望这些例子能够帮助您快速入门Caliburn.Micro框架。
- EOF -
技术群:添加小编微信dotnet999
公众号:Dotnet讲堂