[wpf] handycontrol growl creates an information notification method that is 10 times more elegant than pop-up windows

Preface

Without further ado, here’s the picture:
Insert image description here
This kind of pop-up box will not affect the script of the main process and is divided into four levels:

  • Ordinary message: Info (disappears automatically when the time is up, unless the mouse stays on it)
  • Warning: Warning (disappears automatically when the time is up, unless the mouse stays on it)
  • Error: Error (will not disappear automatically, but can be clicked to close)
  • Fatal error: Fatal (will not disappear automatically and cannot be closed)

Do you want such an elegant pop-up frame?

Preparation

handycontrol This needs to be installed. The installation process will not be described here. If you don’t know how, you can go to the official website to take a look.
handycontrol official website

Interface part

The growl pop-up box is not provided as a control. It is provided as an attached attribute. You only need to provide a place for it.
Here, I use ScrollViewer + StackPanel to "accommodate" her.

<!--弹出区域-->
<ScrollViewer Grid.Row="1" VerticalScrollBarVisibility="Hidden" HorizontalAlignment="Right">
     <StackPanel hc:Growl.GrowlParent="True" VerticalAlignment="Top" Margin="0,10,10,10"/>
</ScrollViewer>

In this way, there will be a lot of real-time messages arranged continuously.
The hc: here is the statement of handycontrol:

xmlns:hc="https://handyorg.github.io/handycontrol" 

We put this part of the code into the main interface

Backend part

Next, you only need to call HandyControl.Controls.Growl.Info("XXXX");
or HandyControl.Controls.Growl.Error("XXXX") to realize the pop-up box.

But here we use the event tool in prism, so that no matter where we are, we can send messages and let the main interface display the messages. It is very convenient to generate some alarm messages in this way! The packaged source code is no longer available below.

Subscription package

public MainWindow(IEventAggregator eventAggregator)
{
    
    
    //事件订阅
    eventAggregator.GetEvent<GrowlEvent>().Subscribe((GrowMsg msg) =>{
    
    
        if (msg.enumAlarmType == EnumAlarmType.Info)
        {
    
    
            HandyControl.Controls.Growl.Info(msg.message);
        }
        else if (msg.enumAlarmType == EnumAlarmType.Info)
        {
    
    
            HandyControl.Controls.Growl.Warning(msg.message);
        }
        else if (msg.enumAlarmType == EnumAlarmType.Err)
        {
    
    
            HandyControl.Controls.Growl.Error(msg.message);
        }
        else if (msg.enumAlarmType == EnumAlarmType.Fatal)
        {
    
    
            HandyControl.Controls.Growl.Fatal(msg.message);
        }
        else
        {
    
    
            HandyControl.Controls.Growl.Info(msg.message);
        }
    });
}

Send encapsulation

void PostGrowlEvent(string strMessage, EnumAlarmType enumat)
{
    
    
    eventAggregator.GetEvent<GrowlEvent>().Publish(new GrowMsg()
    {
    
    
        enumAlarmType = enumat,
        message = strMessage,
    });
}

The following are the enumeration types, event types, and message types used above.

public enum EnumAlarmType
{
    
    
    Debug = 0,
    Info,
    Err,
    Fatal,
    Warning,
}

/// <summary>
/// 一个弹窗事件
/// </summary>
public class GrowlEvent : PubSubEvent<GrowMsg>
{
    
    
}

public class GrowMsg
{
    
    
    public EnumAlarmType enumAlarmType;
    public string message;
    public string token;
}

In this way, if you call PostGrowlEvent anywhere, the main interface will have an elegant pop-up prompt box! !

For the prism part, you can refer to my column. Thank you for your support!
"[Prism Series] Prism Event Aggregator"

Guess you like

Origin blog.csdn.net/songhuangong123/article/details/134894103