MahApps.Metro怎么调用消息窗口

简介: 网上查看了好多资料,没有找到很清楚明了的结果,经过了多天的研究,无意发现了这个方法来进行全局调用 效果展示: 1.主窗口代码 public partial class MainWindow : MetroWindow { public MainWindo...

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

效果展示:

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.")); }

 

相关文章
|
消息中间件 安全 API
C#实现操作Windows窗口句柄:SendMessage/PostMessage发送系统消息、事件和数据【窗口句柄总结之二】
SendMessage/PostMessage API 可以实现发送系统消息,这些消息可以定义为常见的鼠标或键盘事件、数据的发送等各种系统操作......
5499 1
C#实现操作Windows窗口句柄:SendMessage/PostMessage发送系统消息、事件和数据【窗口句柄总结之二】
|
存储 安全 Windows
Win32子窗口创建,子窗口回调函数,消息堆栈,逆向定位子窗口消息处理过程
Win32子窗口创建,子窗口回调函数,消息堆栈,逆向定位子窗口消息处理过程
Qt父窗口与子窗口数据交互(用拾色器举例)
Qt父窗口与子窗口数据交互(用拾色器举例)
Qt父窗口与子窗口数据交互(用拾色器举例)
|
Windows
Windows程序设计——窗口键盘消息滚动事件
Windows程序设计——窗口键盘消息滚动事件
277 0
QT应用编程: 鼠标拖动文件到应用程序窗口
QT应用编程: 鼠标拖动文件到应用程序窗口
319 0
QT应用编程: 鼠标拖动文件到应用程序窗口
|
安全 C#
WPF的消息机制(三)- WPF内部的5个窗口之处理激活和关闭的消息窗口以及系统资源通知窗口
原文:WPF的消息机制(三)- WPF内部的5个窗口之处理激活和关闭的消息窗口以及系统资源通知窗口 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.
1040 0
|
消息中间件 C#
WPF的消息机制(二)- WPF内部的5个窗口之隐藏消息窗口
原文:WPF的消息机制(二)- WPF内部的5个窗口之隐藏消息窗口 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/powertoolsteam/article/details/6109036 ...
1064 0