MahApps.Metro怎么调用消息窗口

本文介绍了一个使用 WPF 中的 MetroDialog 控件进行全局调用的自定义示例,通过创建单例类实现了消息框的统一管理和调用,并提供了具体的代码实现。

网上查看了好多资料,没有找到很清楚明了的结果,经过了多天的研究,无意发现了这个方法来进行全局调用

效果展示:

1.主窗口代码

  public partial class MainWindow : MetroWindow
    {
        public MainWindow()
        {
            InitializeComponent();
 
            MessageExt.Instance.ShowDialog = ShowDialog;            MessageExt.Instance.ShowYesNo = ShowYesNo;
        }
        public async void ShowDialog(string message, string title)
        {
            var mySettings = new MetroDialogSettings()
            {
                AffirmativeButtonText = "关闭",
                ColorScheme = MetroDialogColorScheme.Theme
            };
            MessageDialogResult result = await this.ShowMessageAsync(title, message, MessageDialogStyle.Affirmative, mySettings);
        }
        public async void ShowYesNo(string message, string title,Action action)        
{
       var mySettings = new MetroDialogSettings()
{
AffirmativeButtonText = "确定",
NegativeButtonText = "取消",
ColorScheme = MetroDialogColorScheme.Theme
};
MessageDialogResult result = await this.ShowMessageAsync(title, message, MessageDialogStyle.AffirmativeAndNegative, mySettings); if (result == MessageDialogResult.Affirmative)
await Task.Factory.StartNew(action);
} }

 

2.单例类代码

    public sealed class MessageExt
    {
        private static readonly MessageExt instance = new MessageExt();
        private MessageExt()
        {
        }

        public static MessageExt Instance
        {
            get
            {
                return instance;
            }
        } 
        /// <summary>
        /// 调用消息窗口的代理事件
        /// </summary>
        public Action<string, string> ShowDialog { get; set; }
        /// <summary>
        /// 调用消息确认窗口的代理事件
        /// </summary>
        public Action<string, string, Action> ShowYesNo { get; set; }
    }

 

3.调用方法:

     MessageExt.Instance.ShowDialog("查询", "提示");

     MessageExt.Instance.ShowYesNo("查询", "提示", new Action(() => {
            MessageBox.Show("我来了");
     }));

 

其他请自行实现。

 

官方调用消息窗口的demo代码

 private async void ShowMessageDialog(object sender, RoutedEventArgs e)
        {
            // This demo runs on .Net 4.0, but we're using the Microsoft.Bcl.Async package so we have async/await support
            // The package is only used by the demo and not a dependency of the library!
            MetroDialogOptions.ColorScheme = UseAccentForDialogsMenuItem.IsChecked ? MetroDialogColorScheme.Accented : MetroDialogColorScheme.Theme;

            var mySettings = new MetroDialogSettings()
            {
                AffirmativeButtonText = "Hi",  //确定
                NegativeButtonText = "Go away!", //否
                FirstAuxiliaryButtonText = "Cancel",
//第一个自定义按钮, SecondAuxiliaryButtonText为第二个自定义按钮 ColorScheme
= UseAccentForDialogsMenuItem.IsChecked ? MetroDialogColorScheme.Accented : MetroDialogColorScheme.Theme }; MessageDialogResult result = await this.ShowMessageAsync("Hello!", "Welcome to the world of metro!", MessageDialogStyle.AffirmativeAndNegativeAndSingleAuxiliary, mySettings); if (result != MessageDialogResult.FirstAuxiliary) //这里编写点击后的代码 await this.ShowMessageAsync("Result", "You said: " + (result == MessageDialogResult.Affirmative ? mySettings.AffirmativeButtonText : mySettings.NegativeButtonText + Environment.NewLine + Environment.NewLine + "This dialog will follow the Use Accent setting.")); } private async void ShowLimitedMessageDialog(object sender, RoutedEventArgs e) { var mySettings = new MetroDialogSettings() { AffirmativeButtonText = "Hi", NegativeButtonText = "Go away!", FirstAuxiliaryButtonText = "Cancel", MaximumBodyHeight = 100, ColorScheme = UseAccentForDialogsMenuItem.IsChecked ? MetroDialogColorScheme.Accented : MetroDialogColorScheme.Theme }; MessageDialogResult result = await this.ShowMessageAsync("Hello!", "Welcome to the world of metro!" + string.Join(Environment.NewLine, "abc","def","ghi", "jkl","mno","pqr","stu","vwx","yz"), MessageDialogStyle.AffirmativeAndNegativeAndSingleAuxiliary, mySettings); if (result != MessageDialogResult.FirstAuxiliary) await this.ShowMessageAsync("Result", "You said: " + (result == MessageDialogResult.Affirmative ? mySettings.AffirmativeButtonText : mySettings.NegativeButtonText + Environment.NewLine + Environment.NewLine + "This dialog will follow the Use Accent setting.")); }

 

代码介绍 MetroForWinForm(win8风格模版) using System; using System.Drawing; using System.Globalization; using System.Windows.Forms; using MetroFramework.Forms; namespace MetroFramework.Demo { public partial class MainForm : MetroForm { public MainForm() { InitializeComponent(); metroStyleManager.Theme = MetroThemeStyle.Default; metroStyleManager.Style = MetroColorStyle.Teal; } private void metroTileSwitch_Click(object sender, EventArgs e) { var m = new Random(); int next = m.Next(0, 13); metroStyleManager.Style = (MetroColorStyle)next; } private void metroTile1_Click(object sender, EventArgs e) { metroStyleManager.Theme = metroStyleManager.Theme == MetroThemeStyle.Light ? MetroThemeStyle.Dark : MetroThemeStyle.Light; } private void metroButton1_Click(object sender, EventArgs e) { MetroTaskWindow.ShowTaskWindow(this, "SubControl in TaskWindow", new TaskWindowControl(), 10); } private void metroButton2_Click(object sender, EventArgs e) { MetroMessageBox.Show(this, "Do you like this metro message box?", "Metro Title", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Asterisk); } private void metroButton5_Click(object sender, EventArgs e) { metroContextMenu1.Show(metroButton5, new Point(0, metroButton5.Height)); } private void metroButton6_Click(object sender, EventArgs e) { MetroMessageBox.Show(this, "This is a sample MetroMessagebox `OK` only button", "MetroMessagebox", MessageBoxButtons.OK, MessageBoxIcon.Information); } private void metroButton10_Click(object sender, EventArgs e) { MetroMessageBox.Show(this, "This is a sample MetroMessagebox `OK` and `Cancel` button", "MetroMessagebox", MessageBoxButtons.OKCancel, MessageBoxIcon.Information); } private void metroButton7_Click(object sender, EventArgs e) { MetroMessageBox.Show(this, "This is a sample MetroMessagebox `Yes` and `No` button", "MetroMessagebox", MessageBoxButtons.YesNo, MessageBoxIcon.Question); } private void metroButton8_Click(object sender, EventArgs e) { MetroMessageBox.Show(this, "This is a sample MetroMessagebox `Yes`, `No` and `Cancel` button", "MetroMessagebox", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); } private void metroButton11_Click(object sender, EventArgs e) { MetroMessageBox.Show(this, "This is a sample MetroMessagebox `Retry` and `Cancel` button. With warning style.", "MetroMessagebox", MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning); } private void metroButton9_Click(object sender, EventArgs e) { MetroMessageBox.Show(this, "This is a sample MetroMessagebox `Abort`, `Retry` and `Ignore` button. With Error style.", "MetroMessagebox", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Error); } private void metroButton12_Click(object sender, EventArgs e) { MetroMessageBox.Show(this, "This is a sample `default` MetroMessagebox ", "MetroMessagebox"); } private void metroButton4_Click(object sender, EventArgs e) { var testform = new TestForm1(); testform.ShowDialog(); } private void metroButton4_Click_1(object sender, EventArgs e) { metroTextBox2.Focus(); } } }
03-18
### MahApps.Metro Framework for WPF MahApps.Metro 是一个专为 Windows Presentation Foundation (WPF) 设计的框架,旨在帮助开发人员以最少的努力构建具有更好用户界面的应用程序[^1]。此框架提供了 Metro 风格的设计支持,使应用程序能够拥有现代化、简洁且一致的外观。 #### 主要特点 MahApps.Metro 提供了一系列丰富的功能和特性来增强 WPF 应用程序的用户体验: - **全面的控件集合**:该框架包含大量预定义的 UI 控件,这些控件经过精心设计,可以轻松集成到任何 WPF 项目中[^3]。 - **主题与样式自定义**:开发者可以通过简单的配置文件或代码逻辑调整整个应用的主题颜色、字体以及其他视觉属性。 - **动画效果支持**:内置多种过渡动画,提升用户的交互体验。 - **详细的文档资源**:官方维护了详尽的技术文档,配合活跃的社区讨论区,极大地方便了初学者的学习过程以及高级使用者解决问题的能力。 - **跨平台兼容性良好**:尽管主要针对桌面端Windows环境下的软件开发,但在.NET Core/.NET 5+时代之后,其适应范围进一步扩大[^4]。 对于希望采用统一而美观的设计模式的人来说,这是一个非常理想的选择;尤其是当目标群体倾向于扁平化设计理念时更是如此[^2]。 以下是创建基于 `MahApps.Metro` 的简单窗口实例演示如何设置基本布局结构并加载默认主题样式的 C# 示例代码片段: ```csharp using System.Windows; using MahApps.Metro; namespace MyFirstMetroApp { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); // 设置当前主题为 Light, Accent Color Blue ThemeManager.ChangeTheme(this, "Light.Blue"); Title = "My First MahApps.Metro App"; } } } ``` 上述例子展示了怎样初始化一个新的 WPF 窗口并将它关联至特定的主题配色方案之上。通过调用 `ThemeManager.ChangeTheme()` 方法即可实现动态切换不同的显示偏好设定。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值