当前位置: 首页 > 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.rkmt.cn/news/4997.html

相关文章:

  • git clone操作报错diffie-hellman-group1-sha1的解决方案
  • 都可以!燕千云ITSM一站式接入全球主流AI大模型
  • 问题解决模板
  • 鸿蒙应用开发从入门到实战(五):ArkUI概述
  • ABC 423先慢慢改吧题解
  • 汇聚层交换机的替换要考虑到的因素
  • git 常见使用
  • python UV 包管理工具安装
  • latex 打印生僻字
  • 电机ADC采集
  • 道德经
  • Go by Example(3.Variables)
  • 9.3-9.10周报七
  • 学习心得
  • 基于Python+Vue开发的农产品商城管理系统源码+运行
  • 多人多次并发
  • B. Alternating Current
  • 深入解析:【JavaEE】网络原理初识
  • 爬虫逆向--Day22Day23--核心实战案例【荔枝网】【WASM学习】
  • Ubuntu上进行Zookeeper集群部署
  • A Survey of Reinforcement Learning for Large Reasoning Models - jack
  • 财务系统里面,怎么合并使用两个经费本号
  • 【火电机组、风能、储能】高比例风电电力系统储能运行及配置分析(Matlab代码实现) - 详解
  • Redis是如何进行内存管理的?缓存中有哪些常见问题?如何实现分布式锁?
  • 移远OPENCPU笔记
  • 2025.9.16——1绿
  • LGP5688 [CSP-S-JX 2019] 散步 学习笔记
  • 2025 PHP 开发者必看得 25 个容易犯的常见错误 90% 的开发者都踩过
  • 蔚小理的辅助驾驶,谁最拉跨?
  • 【GitHub每日速递 250915】3 个宝藏开源项目:超长语音合成、算法学习库、自托管软件导航,开发者速收