WPF自动隐藏的消息框的实例代码
(鼠标放上去将一直显示,移开动画继续),提供normal和error两种边框。
介绍:传统的确定,取消,OK,CANCAL之类的对话框太繁琐了,由于项目需要而诞生的仿手机式提示对话框。当然传统的对话框项目中也有,这里就不做介绍了。
出场和退场动画做得很简单,就用Blend随便鼓捣了一番,将就用吧。
预览效果如下:
思路其实很简单:将窗体透明化->布局和样式设计->后台传值调用。
准备工作:Microsoft.Expression.Interactions.dll和System.Windows.Interactivity.dll的引用。Blend中大多数行为需要需要这2个dll,必备啊!
1,将窗体透明化,无边框化:(在.net 4.5中 拖动和缩放窗体再也不用自己写代码了,集成的 <WindowChrome/>就可以实现拖动缩放窗体等诸多功能。)
关键设置如下:
AllowsTransparency="True" HorizontalAlignment="Center" Background="Transparent" xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" WindowStyle="None"
2,布局,主要包括初始布局和动画过渡2个方面:
静态界面布局:
<Grid HorizontalAlignment="Center" VerticalAlignment="Top" x:Name="back"> <Border Padding="38 0" x:Name="br" HorizontalAlignment="Center" VerticalAlignment="Center"> <i:Interaction.Triggers> <i:EventTrigger EventName="MouseEnter"> <ei:ControlStoryboardAction Storyboard="{StaticResource ShowSb}" ControlStoryboardOption="Stop"/> </i:EventTrigger> <i:EventTrigger EventName="MouseLeave"> <ei:ControlStoryboardAction Storyboard="{StaticResource MouseLeave}"/> </i:EventTrigger> </i:Interaction.Triggers> <Grid HorizontalAlignment="Center" VerticalAlignment="Center"> <Grid HorizontalAlignment="Center" VerticalAlignment="Center"> <Grid.RowDefinitions> <RowDefinition Height="12"></RowDefinition> <RowDefinition Height="auto"></RowDefinition> <RowDefinition Height="12"></RowDefinition> </Grid.RowDefinitions> <Border Visibility="Visible" x:Name="grid1" Grid.RowSpan="3" RenderTransformOrigin="0.5,0.5" BorderBrush="#00A0E9" BorderThickness="1" CornerRadius="8"> <Border.RenderTransform> <TransformGroup> <ScaleTransform/> <SkewTransform/> <RotateTransform/> <TranslateTransform/> </TransformGroup> </Border.RenderTransform> <Border.Background> <LinearGradientBrush EndPoint="1,1" MappingMode="RelativeToBoundingBox" StartPoint="0,0"> <GradientStop Color="#EFF0F2" Offset="0.75"/> <GradientStop Color="#EFF0F2" Offset="0.25"/> <GradientStop Color="#EFF0F2" Offset="1"/> <GradientStop Color="#EFF0F2"/> </LinearGradientBrush> </Border.Background> </Border> <Border x:Name="grid2" Visibility="Visible" Opacity="1" Grid.RowSpan="3" RenderTransformOrigin="0.5,0.5" BorderBrush="#F35150" BorderThickness="1" CornerRadius="8"> <Border.RenderTransform> <TransformGroup> <ScaleTransform/> <SkewTransform/> <RotateTransform/> <TranslateTransform/> </TransformGroup> </Border.RenderTransform> <Border.Background> <LinearGradientBrush EndPoint="1,1" MappingMode="RelativeToBoundingBox" StartPoint="0,0"> <GradientStop Color="#EFF0F2" Offset="0.75"/> <GradientStop Color="#EFF0F2" Offset="0.25"/> <GradientStop Color="#EFF0F2" Offset="1"/> <GradientStop Color="#EFF0F2"/> </LinearGradientBrush> </Border.Background> </Border> <TextBlock Margin="38 0 38 0" Grid.Row="1" FontSize="16" Foreground="#64676d" x:Name="tb" Text="{Binding Message,RelativeSource={RelativeSource AncestorType=Window},FallbackValue=失败信息}" TextWrapping="Wrap" MinWidth="200" VerticalAlignment="Center" HorizontalAlignment="Center" RenderTransformOrigin="0.5,0.5" MaxWidth="600" TextAlignment="Center" FontFamily="Microsoft YaHei"> <TextBlock.RenderTransform> <TransformGroup> <ScaleTransform/> <SkewTransform/> <RotateTransform/> <TranslateTransform/> </TransformGroup> </TextBlock.RenderTransform></TextBlock> </Grid> </Grid> </Border> </Grid>
进入和退出的动画控制:
<Storyboard x:Key="ShowSb" Completed="Storyboard_Completed"> <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)" Storyboard.TargetName="grid1"> <EasingDoubleKeyFrame KeyTime="0" Value="{Binding YOffSet}"/> <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="0"/> <EasingDoubleKeyFrame KeyTime="0:0:3" Value="0"/> <EasingDoubleKeyFrame KeyTime="0:0:3.5" Value="{Binding YOffSet}"/> </DoubleAnimationUsingKeyFrames> <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)" Storyboard.TargetName="grid2"> <EasingDoubleKeyFrame KeyTime="0" Value="{Binding YOffSet}"/> <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="0"/> <EasingDoubleKeyFrame KeyTime="0:0:3" Value="0"/> <EasingDoubleKeyFrame KeyTime="0:0:3.5" Value="{Binding YOffSet}"/> </DoubleAnimationUsingKeyFrames> <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)" Storyboard.TargetName="tb"> <EasingDoubleKeyFrame KeyTime="0" Value="{Binding YOffSet}"/> <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="0"/> <EasingDoubleKeyFrame KeyTime="0:0:3" Value="0"/> <EasingDoubleKeyFrame KeyTime="0:0:3.5" Value="{Binding YOffSet}"/> </DoubleAnimationUsingKeyFrames> </Storyboard> <Storyboard x:Key="MouseLeave" Completed="Storyboard_Completed"> <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="grid1"> <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="0"/> </DoubleAnimationUsingKeyFrames> <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="grid2"> <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="0"/> </DoubleAnimationUsingKeyFrames> <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="tb"> <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="0"/> </DoubleAnimationUsingKeyFrames> <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)" Storyboard.TargetName="grid1"> <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="{Binding YOffSet}"/> </DoubleAnimationUsingKeyFrames> <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)" Storyboard.TargetName="grid2"> <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="{Binding YOffSet}"/> </DoubleAnimationUsingKeyFrames> <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)" Storyboard.TargetName="tb"> <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="{Binding YOffSet}"/> </DoubleAnimationUsingKeyFrames> </Storyboard>
注意:鼠标移动上去停止动画和移出后快速消失的代码为:
<i:Interaction.Triggers> <i:EventTrigger EventName="MouseEnter"> <ei:ControlStoryboardAction Storyboard="{StaticResource ShowSb}" ControlStoryboardOption="Stop"/> </i:EventTrigger> <i:EventTrigger EventName="MouseLeave"> <ei:ControlStoryboardAction Storyboard="{StaticResource MouseLeave}"/> </i:EventTrigger> </i:Interaction.Triggers>
后台代码逻辑:
private bool iserror = false; public void Show(string messageBoxText, bool iserror = false) { this.iserror = iserror; this.Message = messageBoxText; this.Show(); } public OnlyShowMessageBox() { InitializeComponent(); this.DataContext = new model() { YOffSet = -300d }; this.Loaded += (y, k) => { this.Top = 41; this.Left = (SystemParameters.WorkArea.Width) / 2 - this.ActualWidth / 2; if (iserror) { this.grid.Visibility = Visibility.Collapsed; } else { this.grid.Visibility = Visibility.Collapsed; } (this.Resources["ShowSb"] as Storyboard).Begin(); }; } private void Storyboard_Completed(object sender, EventArgs e) { this.Close(); }
其中 :
public class model : ModelBase { private double YOffset; public double YOffSet { get { return YOffset; } set { YOffset = value; RaisePropertyChangedEvent("YOffSet"); } } }
其中ModelBase在 可分组的选择框控件(MVVM下)(Toggle样式 仿造单选框RadioButton,复选框CheckBox功能) 中有介绍。
最后,调用方法:
new OnlyShowMessageBox().Show("请注意:前方高能,禁止入内!", false);
或者
new OnlyShowMessageBox().Show("不存在此对象!", true);
以上所述是小编给大家介绍的WPF自动隐藏的消息框的全部叙述,希望对大家有所帮助,如果大家想了解更多内容敬请关注我们!
您可能感兴趣的文章
- 01-10WinForm实现程序一段时间不运行自动关闭的方法
- 01-10C#及WPF获取本机所有字体和颜色的方法
- 01-10WPF实现类似360安全卫士界面的程序源码分享
- 01-10C#.NET实现网页自动登录的方法
- 01-10C#实现的三种模拟自动登录和提交POST信息的方法
- 01-10WPF实现时钟特效
- 01-10超炫酷的WPF实现Loading控件效果
- 01-10举例讲解C#中自动实现的属性
- 01-10C#隐藏主窗口的方法小结
- 01-10VS中模仿WPF模板创建最简单的WPF程序
阅读排行
本栏相关
- 01-10C#通过反射获取当前工程中所有窗体并
- 01-10关于ASP网页无法打开的解决方案
- 01-10WinForm限制窗体不能移到屏幕外的方法
- 01-10WinForm绘制圆角的方法
- 01-10C#实现txt定位指定行完整实例
- 01-10WinForm实现仿视频播放器左下角滚动新
- 01-10C#停止线程的方法
- 01-10C#实现清空回收站的方法
- 01-10C#通过重写Panel改变边框颜色与宽度的
- 01-10C#实现读取注册表监控当前操作系统已
随机阅读
- 08-05dedecms(织梦)副栏目数量限制代码修改
- 01-10delphi制作wav文件的方法
- 01-10C#中split用法实例总结
- 04-02jquery与jsp,用jquery
- 01-10使用C语言求解扑克牌的顺子及n个骰子
- 08-05DEDE织梦data目录下的sessions文件夹有什
- 01-10SublimeText编译C开发环境设置
- 01-11Mac OSX 打开原生自带读写NTFS功能(图文
- 01-11ajax实现页面的局部加载
- 08-05织梦dedecms什么时候用栏目交叉功能?