Metro 是一个用于创建 Windows 应用商店应用的开源框架,它提供了一套丰富的 UI 控件和样式,以及一些实用的功能,如导航、数据绑定等,Metro 的源码可以在 GitHub 上找到:https://github.com/MahApps/MahApps.Metro
Metro 的主要组成部分包括:
1、MahApps.Metro:核心库,包含了 Metro 的样式和主题。
2、Metro.Behaviours:行为库,提供了一些附加的功能,如拖放、右键菜单等。
3、Metro.Dialogs:对话框库,提供了一些常用的对话框,如消息框、颜色选择器等。
4、Metro.Fonts:字体库,包含了 Metro 使用的字体文件。
5、Metro.Samples:示例项目,展示了如何使用 Metro 框架创建应用。
要使用 Metro 框架,首先需要在项目中引入相应的 NuGet 包,对于 MahApps.Metro,可以在项目的 NuGet 包管理器控制台中输入以下命令:
InstallPackage MahApps.Metro
在 App.xaml.cs 文件中添加对 MahApps.Metro 的引用:
using MahApps.Metro.Controls;
在 App.xaml 文件中添加 Metro 的主题资源:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<! MahApps.Metro resource dictionary >
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
<! Accent and AppTheme setting >
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Themes/Light.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
你可以在 XAML 文件中使用 Metro 提供的控件和样式了。
<Window x:Class="WpfApp1.MainWindow"
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/markupcompatibility/2006"
xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<controls:MetroWindow.LeftWindowCommands>
<controls:WindowCommand Icon="?" Content="Open website" Click="OnWindowCommand" />
</controls:MetroWindow.LeftWindowCommands>
<controls:MetroWindow.RightWindowCommands>
<controls:WindowCommand Icon="?" Content="Search" />
</controls:MetroWindow.RightWindowCommands>
<TextBox controls:TextboxHelper.Watermark="Enter text here" HorizontalAlignment="Center" VerticalAlignment="Center" Width="200" />
</Grid>
</Window>
在这个例子中,我们使用了 Metro 提供的 WindowCommand 控件来实现窗口左侧和右侧的命令按钮,我们还使用了 Metro 的 TextboxHelper 类为 TextBox 控件添加了一个占位符文本。