• WPF开发随笔收录仿安卓Toast


    WPF开发随笔收录-仿安卓Toast

     

    一、前言

    在项目中,经常需要用到消息提醒功能,在以前接触安卓开发那会使用过Toast,于是打算在WPF上也来模仿一个,话不多说,撸起袖子干起来!

    二、正文

    1、首先新建一个工程,工程的目录如下

     2、编写Toast.cs的代码,这里因为只需要显示文本信息,所以Toast继承Label即可,然后添加一个定时关闭的方法

    复制代码
    public class Toast : Label
    {
        public Toast()
        {
                
        }
    
        public void SetTimeClose(TimeSpan time)
        {
            new Thread(() =>
            {
                Thread.Sleep(time);
                if (this.Parent is Panel)
                {
                    this.Dispatcher.BeginInvoke(new Action(() =>
                    {
                        (this.Parent as Panel).Children.Remove(this);
                    }));
                }
            })
            { IsBackground = true }.Start();
        }
    }
    复制代码

    3、接着编写一下Toast控件的样式

    复制代码
    <Style TargetType="{x:Type ctls:Toast}">
        <Setter Property="Foreground" Value="White" />
        <Setter Property="FocusVisualStyle" Value="{x:Null}" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ctls:Toast}">
                    <Border
                        MinWidth="50"
                        MinHeight="50"
                        Padding="25,0"
                        Background="#90000000"
                        CornerRadius="2">
                        <Border.Effect>
                            <DropShadowEffect BlurRadius="10" ShadowDepth="1" />
                        </Border.Effect>
                        <ContentPresenter
                            HorizontalAlignment="Center"
                            VerticalAlignment="Center"
                            TextBlock.FontSize="14" />
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    复制代码

    4、编辑MainWindow.xaml文件,其中StackPanel是用来添加Toast控件的容器

    复制代码
    <Window x:Class="ToastDemo.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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:ToastDemo"
            mc:Ignorable="d"
            Title="MainWindow" Height="450" Width="800">
        <Grid>
            <Grid>
                <Button 
                    Width="100"
                    Height="50"
                    Content="Button"
                    Click="Button_Click"/>
            </Grid>
            <StackPanel
                Name="ToastPanel"
                Margin="0,80,30,0"
                HorizontalAlignment="Center"
                VerticalAlignment="Top" />
        </Grid>
    </Window>
    复制代码

    5、接着编写后台代码,添加一个ShowToast方法来生成一个Toast到ToastPanel

    复制代码
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            ShowToast("这是一个Toast");
        }
    
        public void ShowToast(string text, TimeSpan? time = null)
        {
            Toast toast = new Toast();
            toast.Content = text;
            toast.Margin = new Thickness(0, 10, 0, 0);
            ToastPanel.Children.Add(toast);
            if (time == null)
            {
                toast.SetTimeClose(TimeSpan.FromSeconds(5));
            }
            else
            {
                toast.SetTimeClose(time.Value);
            }
        }
    }
    复制代码

    6、运行一下看一下效果,可以看到想要的基本效果已经完成了

  • 相关阅读:
    redis学习--Hashes数据类型
    redis学习--String数据类型。
    redis学习一
    redis命令大全
    MongoDB学习笔记(索引)
    ECharts的使用(经典博客)
    php中五种常见的设计模式
    实用的借口
    php中socket的使用
    jquery仿凡客诚品图片切换的效果实例代码
  • 原文地址:https://www.cnblogs.com/sexintercourse/p/16414497.html
Copyright © 2020-2023  润新知