博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MahApps.Metro怎么调用消息窗口
阅读量:5149 次
发布时间:2019-06-13

本文共 4631 字,大约阅读时间需要 15 分钟。

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

效果展示:

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;            }        }         ///         /// 调用消息窗口的代理事件        ///         public Action
ShowDialog { get; set; } ///
/// 调用消息确认窗口的代理事件 /// public 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."));        }

 

转载于:https://www.cnblogs.com/xcsn/p/4531365.html

你可能感兴趣的文章
OO学习总结与体会
查看>>
虚拟机长时间不关造成的问题
查看>>
toString和valueOf的区别
查看>>
C#操作Excel(创建、打开、读写、保存)几种方法的总结
查看>>
校门外的树2 contest 树状数组练习 T4
查看>>
JS及JQ使用JSONP实现跨域调用必应搜索
查看>>
面试整理:Python基础
查看>>
Python核心编程——多线程threading和队列
查看>>
三次数模总结一下
查看>>
Py之np.concatenate函数【转载】
查看>>
【NOIP模拟】matrix(简化矩阵)
查看>>
e.preventDefault()和e.stopPropagation()以及return false的作用和区别
查看>>
洛谷 1571 眼红的Medusa
查看>>
[HEOI2016/TJOI2016]树
查看>>
(转载)PHP中设置时区方法小结
查看>>
spring--百度百科
查看>>
关于Invoke和InvokeRequired
查看>>
Program exited with code **** 相关解释
查看>>
装服务器,测试数据库,简单的maven命令
查看>>
升级Firefox8后watir-webdriver出现错误“unable to obtain stable firefox connection in 60 seconds”...
查看>>