当前位置: 首页 > news >正文

MahMetro 框架学习

学习建议:

1.从Demo开始:运行官方Demo,玩遍每一个功能,看看它是如何实现的。

2.动手实践:在自己的一个小项目中应用它,从改造MetroWindow和设置主题开始

3.逐个攻克:依次自学习一个控件(比如先学会用Flyout,再学HamburgerMenu),不要试图一下子掌握所有内容

4.善用搜索引擎:遇到问题时,搜索"MahApps.Metro[你的问题]",通常能在StackOverFlow或 GitHub lssus中找到答案


1.安装框架 install-package MahApps.Metro
2.设置资源字典
  • 说明:Controls.xaml 和Fonts 是必须的核心样式
  • Themes/Light.Blue.xaml 定义了主题和配色,你可以将其改为Dark.Blue.xaml或Light.Red.xaml
 <Application.Resources><ResourceDictionary><ResourceDictionary.MergedDictionaries><!--MahApps.Metro核心样式和主题--><ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" /><ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" /><!--主题(Accent)和基础主题(Base Theme:Light/Dark)--><ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Themes/Light.Blue.xaml" /></ResourceDictionary.MergedDictionaries></ResourceDictionary></Application.Resources>

3.修改窗体类

引用命名空间: xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls"
修改窗体 改为controls:Window

4.修改后台代码

引用命名空间:using MahApps.Metro.Controls
基类由Window 改为MetroWindow

运行: 通过以上步骤可以看出,框架的使用和其他类似框架一样。


Demo1-Metro.Flyout 使用

  • Flyout 是一种飞入式伸缩窗口,类似抽屉。在很多UI设计中为解决界面杂乱臃肿和多种功能集成调用设计中起到简化UI设计又兼备多种功能的集成提供了很好的设计模式和借鉴。

示例:

  • XAML:

<mah:MetroWindow 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/markup-compatibility/2006"xmlns:local="clr-namespace:WpfApp1"xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls"xmlns:pu="clr-namespace:Panuon.UI.Silver;assembly=Panuon.UI.Silver"mc:Ignorable="d"Title="MainWindow" Height="420" Width="600"WindowStartupLocation="CenterScreen"><mah:MetroWindow.Flyouts><mah:FlyoutsControl><mah:Flyout x:Name="FirstFlyout" Header="设置" Position="Right" Width="250"></mah:Flyout><mah:Flyout x:Name="f2" Header="设置"  IsOpen="{Binding IsOpen}" Position="Right" Width="300"><StackPanel Margin="20" Orientation="Vertical"><Button Content="打开" Width="80" Height="30" Margin="5"/><Button Content="修改" Width="80" Height="30" Margin="5"/><Button Content="保存" Width="80" Height="30" Margin="5"/><mah:ToggleSwitch Header="蓄电池闸刀" OffContent="断开" OnContent="闭合" /><mah:ToggleSwitch Header="启机按钮" OffContent="停机" OnContent="启机" /><mah:ProgressRing x:Name="progress" Height="50" Width="50"/></StackPanel></mah:Flyout></mah:FlyoutsControl></mah:MetroWindow.Flyouts><Grid><Grid.ColumnDefinitions><ColumnDefinition Width="Auto"/><ColumnDefinition /></Grid.ColumnDefinitions><StackPanel Orientation="Vertical" Margin="2 10"><Button Width="100"  Command="{Binding OpenSet}" Height="30" Margin="10 5" Content="Flyout" /><Button Width="100" Margin="10 5"Height="30"Style="{StaticResource MahApps.Styles.Button}"Content="消息"Command="{Binding FoolMessageCommand}"/></StackPanel></Grid>
</mah:MetroWindow>
  • Code

using MahApps.Metro.Controls;
using MahApps.Metro.Controls.Dialogs;
using WpfApp1.ViewModels;
namespace WpfApp1
{/// <summary>/// MainWindow.xaml 的交互逻辑/// </summary>public partial class MainWindow : MetroWindow{private MainViewModel main;public MainWindow(){InitializeComponent();main = new MainViewModel(DialogCoordinator.Instance);DataContext = main;}}
}using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using MahApps.Metro.Controls.Dialogs;
using System.Threading.Tasks;
using System.Windows.Input;namespace WpfApp1.ViewModels
{public class MainViewModel : ObservableObject{private IDialogCoordinator dialogCoordinator;private bool isopen;public bool IsOpen{get => isopen;set => SetProperty(ref isopen, value);}private ICommand openSet;public ICommand OpenSet{get{if (openSet == null){openSet = new RelayCommand(() =>{IsOpen = true;});}return openSet;}}private ICommand foolMessage;public ICommand FoolMessageCommand{get{if (foolMessage == null){foolMessage = new RelayCommand(() =>{FoolMessage();});}return foolMessage;}}//该框架内涵依赖注入功能,可以直接在构造函数中注入IDialogCoordinatorpublic MainViewModel(IDialogCoordinator dialogCooldinatorParam){openSet = new RelayCommand(() =>{IsOpen = true;});dialogCoordinator = dialogCooldinatorParam;}public async Task ShowMessageAsync(string title, string message){await dialogCoordinator.ShowMessageAsync(this, title, message);}public async void FoolMessage(){await dialogCoordinator?.ShowMessageAsync(this, "标题", "内容");}}
}
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using MahApps.Metro.Controls.Dialogs;
using System.Threading.Tasks;
using System.Windows.Input;namespace WpfApp1.ViewModels
{public class MainViewModel : ObservableObject{private IDialogCoordinator dialogCoordinator;private bool isopen;public bool IsOpen{get => isopen;set => SetProperty(ref isopen, value);}private ICommand openSet;public ICommand OpenSet{get{if (openSet == null){openSet = new RelayCommand(() =>{IsOpen = true;});}return openSet;}}private ICommand foolMessage;public ICommand FoolMessageCommand{get{if (foolMessage == null){foolMessage = new RelayCommand(() =>{FoolMessage();});}return foolMessage;}}//该框架内涵依赖注入功能,可以直接在构造函数中注入IDialogCoordinatorpublic MainViewModel(IDialogCoordinator dialogCooldinatorParam){openSet = new RelayCommand(() =>{IsOpen = true;});dialogCoordinator = dialogCooldinatorParam;}public async Task ShowMessageAsync(string title, string message){await dialogCoordinator.ShowMessageAsync(this, title, message);}public async void FoolMessage(){await dialogCoordinator?.ShowMessageAsync(this, "标题", "内容");}}
}
  • 效果:

image

image

http://www.wxhsa.cn/company.asp?id=4482

相关文章:

  • 基于MATLAB的标准化降水蒸散指数(SPEI)实现
  • Prometheus Probe 监控配置文档
  • 客户案例|邦普循环x甄知科技,筑牢高效智能的IT运维底座
  • VMware Exporter 指标转换方案
  • 可5V使用引脚兼容STM32F103C8T6的国产32位MCU
  • git clone操作报错diffie-hellman-group1-sha1的解决方案
  • Celery inspect 常用命令手册
  • 都可以!燕千云ITSM一站式接入全球主流AI大模型
  • 删边最短路
  • 问题解决模板
  • 一站式接入全球股票数据:日本、美国、印度、马来西亚等多国API对接实战
  • 基于MATLAB的图像处理程序
  • 跨网文件安全交换系统推荐厂商详解
  • 走迷宫
  • MVC 架构解析
  • 鸿蒙应用开发从入门到实战(五):ArkUI概述
  • 好用的跨网文件安全交换系统:守护企业数据流转的核心屏障!
  • SIM笔记
  • 2025第五届“长城杯”网络安全大赛暨京津冀蒙网络安全技能竞赛 WP Web全
  • FTP替代工具哪个产品好,高效安全之选
  • c++之内存对齐模板类aligned_storage
  • ABC 423先慢慢改吧题解
  • 汇聚层交换机的替换要考虑到的因素
  • git 常见使用
  • python UV 包管理工具安装
  • 什么是网络分区
  • 完整教程:《驾驭云原生复杂性:隐性Bug的全链路防御体系构建》
  • 从机器的角度来说ECS为何性能好
  • 人生最幸福的时刻也就几个瞬间
  • 网络流笔记