ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2022-06-26 22:00:11  阅读:158  来源: 互联网

标签:Toast toast ShowToast 仿安卓 time new WPF public


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、运行一下看一下效果,可以看到想要的基本效果已经完成了

 

标签:Toast,toast,ShowToast,仿安卓,time,new,WPF,public
来源: https://www.cnblogs.com/sexintercourse/p/16414497.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有