欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于WPF簡單實現(xiàn)Meesage消息提醒

 更新時間:2023年07月05日 11:23:10   作者:WPF開發(fā)者  
這篇文章主要介紹了如何利用WPF簡單實現(xiàn)Meesage消息提醒,文中的示例代碼講解詳細,對我們學(xué)習(xí)或工作有一定幫助,需要的可以參考一下

WPF 簡單實現(xiàn) Meesage 消息提醒

框架使用.NET4 至 .NET6;

Visual Studio 2022;

實現(xiàn)方式通過獲取當前焦點的 Window 然后對它添加裝飾器,將 Meesage 內(nèi)部添加 ListBox 用作記錄顯示消息。

新建裝飾器 MessageAdorner.cs 提供一個公共方法 PushMessage 提供可傳參數(shù)

  • message 消息文本,居中顯示從 Y 軸 0 方向屏幕下添加。
  • MessageBoxImage 消息類型枚舉
  • 消息添加到 ListBox 集合中后,使用 DispatcherTimer 用來記時關(guān)閉 MessageItem

新建 MessageItem.cs 基礎(chǔ) ListBoxItem 用來展示每條 Meesage 消息

添加一個依賴屬性 MessageType 枚舉,用來區(qū)分顯示的 Icon 與文本字體顏色

新建 MessageItem.xaml 的模板

對 EventTrigger Loaded 添加動畫 ScaleY 從 0 到 1

新建 Message.cs 幫助類實現(xiàn)全局方便使用

添加靜態(tài)方法 PushMessage 判斷裝飾器是否為空,如果為空則創(chuàng)建裝飾,如果不為空則調(diào)用裝飾器內(nèi)部 PushMessage 方法

下期會寫一個增加關(guān)閉消息,消息談出,文本居中等。

此項目使用了 WPFDevelopers 的引用

實現(xiàn)代碼

1)新增 MessageItem.cs 代碼如下:

using?System.Windows;
using?System.Windows.Controls;
namespace?MessageSample
{
????public?class?MessageItem?:?ListBoxItem
????{
????????public?MessageBoxImage?MessageType
????????{
????????????get?{?return?(MessageBoxImage)GetValue(MessageTypeProperty);?}
????????????set?{?SetValue(MessageTypeProperty,?value);?}
????????}
????????public?static?readonly?DependencyProperty?MessageTypeProperty?=
????????????DependencyProperty.Register("MessageType",?typeof(MessageBoxImage),?typeof(MessageItem),?new?PropertyMetadata(MessageBoxImage.Information));
????}
}

2)新增 MessageAdorner.cs 代碼如下:

public?class?MessageAdorner?:?Adorner
????{
????????private?ListBox?listBox;
????????private?UIElement?_child;
????????private?FrameworkElement?adornedElement;
????????public?MessageAdorner(UIElement?adornedElement)?:?base(adornedElement)
????????{
????????????this.adornedElement?=?adornedElement?as?FrameworkElement;
????????}
????????public?void?PushMessage(string?message,?MessageBoxImage?type?=?MessageBoxImage.Information)
????????{
????????????if?(listBox?==?null)
????????????{
????????????????listBox?=?new?ListBox()?{?Style?=?null?,BorderThickness?=?new?Thickness(0)?,Background?=?Brushes.Transparent};
????????????????Child?=?listBox;
????????????}
????????????var?item?=?new?MessageItem{?Content?=?message?,?MessageType?=?type};
????????????var?timer?=?new?DispatcherTimer();
????????????timer.Interval?=?TimeSpan.FromSeconds(10);
????????????timer.Tick?+=?(sender,?e)?=>
????????????{
????????????????listBox.Items.Remove(item);
????????????????timer.Stop();
????????????};
????????????listBox.Items.Insert(0,item);
????????????timer.Start();
????????}
????????public?UIElement?Child
????????{
????????????get?=>?_child;
????????????set
????????????{
????????????????if?(value?==?null)
????????????????{
????????????????????RemoveVisualChild(_child);
????????????????????_child?=?value;
????????????????????return;
????????????????}
????????????????AddVisualChild(value);
????????????????_child?=?value;
????????????}
????????}
????????protected?override?int?VisualChildrenCount
????????{
????????????get
????????????{
????????????????return?_child?!=?null???1?:?0;
????????????}
????????}
????????protected?override?Size?ArrangeOverride(Size?finalSize)
????????{
????????????var?x?=?(adornedElement.ActualWidth?-?_child.DesiredSize.Width)?/?2;
????????????_child.Arrange(new?Rect(new?Point(x,?0),?_child.DesiredSize));
????????????return?finalSize;
????????}
????????protected?override?Visual?GetVisualChild(int?index)
????????{
????????????if?(index?==?0?&&?_child?!=?null)?return?_child;
????????????return?base.GetVisualChild(index);
????????}
????}

3)新增 Message.cs 代碼如下:

public?static?class?Message
????{
????????private?static?MessageAdorner?messageAdorner;
????????public?static?void?PushMessage(?string?message,?MessageBoxImage?type?=?MessageBoxImage.Information)
????????{
????????????if?(messageAdorner?!=?null)
????????????{
????????????????messageAdorner.PushMessage(message,?type);
????????????????return;
????????????}
????????????Window?win?=?null;
????????????if?(Application.Current.Windows.Count?>?0)
????????????{
????????????????win?=?Application.Current.Windows.OfType<Window>().FirstOrDefault(o?=>?o.IsActive);
????????????????if?(win?==?null)
????????????????????win?=?Application.Current.Windows.OfType<Window>().First(o?=>?o.IsActive);
????????????}
????????????var?layer?=?GetAdornerLayer(win);
????????????if?(layer?==?null)
????????????????throw?new?Exception("not?AdornerLayer?is?null");
????????????messageAdorner?=?new?MessageAdorner(layer);
????????????layer.Add(messageAdorner);
????????????messageAdorner.PushMessage(message,?type);
????????}
????????static?AdornerLayer?GetAdornerLayer(Visual?visual)
????????{
????????????var?decorator?=?visual?as?AdornerDecorator;
????????????if?(decorator?!=?null)
????????????????return?decorator.AdornerLayer;
????????????var?presenter?=?visual?as?ScrollContentPresenter;
????????????if?(presenter?!=?null)
????????????????return?presenter.AdornerLayer;
????????????var?visualContent?=?(visual?as?Window)?.Content?as?Visual;
????????????return?AdornerLayer.GetAdornerLayer(visualContent????visual);
????????}
????}

4)新增 MessageItem.xaml 代碼如下:

?<Style?BasedOn="{StaticResource?WD.ControlBasicStyle}"?TargetType="{x:Type?local:MessageItem}">
????????????????<Setter?Property="Background"?Value="{DynamicResource?WD.BackgroundSolidColorBrush}"?/>
????????????????<Setter?Property="Width"?Value="300"?/>
????????????????<Setter?Property="Template">
????????????????????<Setter.Value>
????????????????????????<ControlTemplate?TargetType="{x:Type?local:MessageItem}">
????????????????????????????<wd:SmallPanel
????????????????????????????????Name="PART_SmallPanel"
????????????????????????????????Margin="4"
????????????????????????????????RenderTransformOrigin=".5,0">
????????????????????????????????<wd:SmallPanel.RenderTransform>
????????????????????????????????????<ScaleTransform?/>
????????????????????????????????</wd:SmallPanel.RenderTransform>
????????????????????????????????<Border
????????????????????????????????????Name="PART_Border"
????????????????????????????????????Background="{TemplateBinding?Background}"
????????????????????????????????????BorderBrush="{TemplateBinding?BorderBrush}"
????????????????????????????????????BorderThickness="{TemplateBinding?BorderThickness}"
????????????????????????????????????CornerRadius="{Binding?Path=(wd:ElementHelper.CornerRadius),?RelativeSource={RelativeSource?TemplatedParent}}"
????????????????????????????????????Effect="{StaticResource?WD.NormalShadowDepth}"
????????????????????????????????????SnapsToDevicePixels="True"
????????????????????????????????????UseLayoutRounding="True"?/>
????????????????????????????????<Border?Padding="10">
????????????????????????????????????<DockPanel>
????????????????????????????????????????<Path
????????????????????????????????????????????x:Name="PART_Path"
????????????????????????????????????????????Width="15"
????????????????????????????????????????????Height="15"
????????????????????????????????????????????Data="{StaticResource?WD.InformationGeometry}"
????????????????????????????????????????????Fill="{DynamicResource?WD.PrimaryNormalSolidColorBrush}"
????????????????????????????????????????????Stretch="Fill"?/>
????????????????????????????????????????<TextBlock
????????????????????????????????????????????Grid.Row="1"
????????????????????????????????????????????Grid.Column="1"
????????????????????????????????????????????Margin="5,0"
????????????????????????????????????????????VerticalAlignment="Center"
????????????????????????????????????????????FontSize="{DynamicResource?WD.NormalFontSize}"
????????????????????????????????????????????Foreground="{TemplateBinding?Foreground}"
????????????????????????????????????????????Text="{TemplateBinding?Content}"
????????????????????????????????????????????TextWrapping="Wrap"?/>
????????????????????????????????????</DockPanel>
????????????????????????????????</Border>
????????????????????????????</wd:SmallPanel>
????????????????????????????<ControlTemplate.Triggers>
????????????????????????????????<Trigger?Property="MessageType"?Value="Warning">
????????????????????????????????????<Setter?TargetName="PART_Path"?Property="Data"?Value="{StaticResource?WD.WarningGeometry}"?/>
????????????????????????????????????<Setter?TargetName="PART_Path"?Property="Fill"?Value="{StaticResource?WD.WarningSolidColorBrush}"?/>
????????????????????????????????????<Setter?Property="Foreground"?Value="{StaticResource?WD.WarningSolidColorBrush}"?/>
????????????????????????????????</Trigger>
????????????????????????????????<Trigger?Property="MessageType"?Value="Error">
????????????????????????????????????<Setter?TargetName="PART_Path"?Property="Data"?Value="{StaticResource?WD.ErrorGeometry}"?/>
????????????????????????????????????<Setter?TargetName="PART_Path"?Property="Fill"?Value="{StaticResource?WD.DangerSolidColorBrush}"?/>
????????????????????????????????????<Setter?Property="Foreground"?Value="{StaticResource?WD.DangerSolidColorBrush}"?/>
????????????????????????????????</Trigger>
????????????????????????????????<Trigger?Property="MessageType"?Value="Information">
????????????????????????????????????<Setter?TargetName="PART_Path"?Property="Data"?Value="{StaticResource?WD.InformationGeometry}"?/>
????????????????????????????????????<Setter?TargetName="PART_Path"?Property="Fill"?Value="{StaticResource?WD.SuccessSolidColorBrush}"?/>
????????????????????????????????????<Setter?Property="Foreground"?Value="{StaticResource?WD.SuccessSolidColorBrush}"?/>
????????????????????????????????</Trigger>
????????????????????????????????<Trigger?Property="MessageType"?Value="Question">
????????????????????????????????????<Setter?TargetName="PART_Path"?Property="Data"?Value="{StaticResource?WD.QuestionGeometry}"?/>
????????????????????????????????????<Setter?TargetName="PART_Path"?Property="Fill"?Value="{StaticResource?WD.NormalSolidColorBrush}"?/>
????????????????????????????????????<Setter?Property="Foreground"?Value="{StaticResource?WD.NormalSolidColorBrush}"?/>
????????????????????????????????</Trigger>
????????????????????????????????<EventTrigger?RoutedEvent="Loaded">
????????????????????????????????????<BeginStoryboard>
????????????????????????????????????????<Storyboard>
????????????????????????????????????????????<DoubleAnimation
????????????????????????????????????????????????Storyboard.TargetName="PART_SmallPanel"
????????????????????????????????????????????????Storyboard.TargetProperty="(UIElement.RenderTransform).(ScaleTransform.ScaleY)"
????????????????????????????????????????????????From="0"
????????????????????????????????????????????????To="1"
????????????????????????????????????????????????Duration="0:0:0.2"?/>
????????????????????????????????????????????<DoubleAnimation
????????????????????????????????????????????????Storyboard.TargetName="PART_SmallPanel"
????????????????????????????????????????????????Storyboard.TargetProperty="Opacity"
????????????????????????????????????????????????From="0.01"
????????????????????????????????????????????????To="1"
????????????????????????????????????????????????Duration="0:0:0.2"?/>
????????????????????????????????????????</Storyboard>
????????????????????????????????????</BeginStoryboard>
????????????????????????????????</EventTrigger>
????????????????????????????????<Trigger?SourceName="PART_SmallPanel"?Property="Opacity"?Value="0">
????????????????????????????????????<Setter?Property="Visibility"?Value="Collapsed"?/>
????????????????????????????????</Trigger>
????????????????????????????</ControlTemplate.Triggers>
????????????????????????</ControlTemplate>
????????????????????</Setter.Value>
????????????????</Setter>
????????????</Style>

5)新增 示例 代碼如下:

<wd:Window
????x:Class="MessageSample.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:local="clr-namespace:MessageSample"
????xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
????xmlns:wd="https://github.com/WPFDevelopersOrg/WPFDevelopers"
????Title="WPFDevelopers?-?Message"
????Width="800"
????Height="450"
????mc:Ignorable="d">
????<Grid>
????????<StackPanel
????????????Grid.Row="1"
????????????HorizontalAlignment="Center"
????????????VerticalAlignment="Bottom"
????????????Orientation="Horizontal">
????????????<Button
????????????????Click="AddButton_Click"
????????????????Content="Info?Message"
????????????????Style="{StaticResource?WD.SuccessPrimaryButton}"
????????????????Tag="Info"?/>
????????????<Button
????????????????Click="AddButton_Click"
????????????????Content="Error?Message"
????????????????Style="{StaticResource?WD.DangerPrimaryButton}"
????????????????Tag="Error"?/>
????????????<Button
????????????????Click="AddButton_Click"
????????????????Content="Warning?Message"
????????????????Style="{StaticResource?WD.WarningPrimaryButton}"
????????????????Tag="Warning"?/>
????????????<Button
????????????????Click="AddButton_Click"
????????????????Content="Question?Message"
????????????????Style="{StaticResource?WD.PrimaryButton}"
????????????????Tag="Question"?/>
????????????<Button
????????????????Click="AddButton_Click"
????????????????Content="Very?Long?Message"
????????????????Style="{StaticResource?WD.SuccessPrimaryButton}"
????????????????Tag="Long"?/>
????????</StackPanel>
????</Grid>
</wd:Window>

6) 示例 代碼如下:

using?System.Windows;
using?System.Windows.Controls;
namespace?MessageSample
{
????///?<summary>
????///?Interaction?logic?for?MainWindow.xaml
????///?</summary>
????public?partial?class?MainWindow
????{
????????public?MainWindow()
????????{
????????????InitializeComponent();
????????}
????????private?void?AddButton_Click(object?sender,?RoutedEventArgs?e)
????????{
????????????var?btn?=?sender?as?Button;
????????????switch?(btn.Tag)
????????????{
????????????????case?"Info":
????????????????????Message.PushMessage("這是一條成功消息",?MessageBoxImage.Information);
????????????????????break;
????????????????case?"Error":
????????????????????Message.PushMessage("這是一條錯誤消息",?MessageBoxImage.Error);
????????????????????break;
????????????????case?"Warning":
????????????????????Message.PushMessage("這是一條警告消息",?MessageBoxImage.Warning);
????????????????????break;
????????????????case?"Question":
????????????????????Message.PushMessage("這是一條詢問消息",?MessageBoxImage.Question);
????????????????????break;
????????????????default:
????????????????????Message.PushMessage("這是一條很長長長長長長長長長長長長長長長長長長長長長長長長長長長長長長長長長長長長長長長長長長長長長長長長長長長長消息",?MessageBoxImage.Information);
????????????????????break;
????????????}
????????}
????}
}

效果圖

以上就是基于WPF簡單實現(xiàn)Meesage消息提醒的詳細內(nèi)容,更多關(guān)于WPF Meesage消息提醒的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論