• UWP笔记-消息弹窗自动淡出


    为了让用户有个更好的UI交互,可以增加自动淡出的消息弹窗,例如:网易云音乐UWP,切换播放模式时,出现的类似消息提示。

    右键项目,添加用户控件

    UserControlDemo.xaml:

    <UserControl
        <UserControl.Resources>
            <Storyboard x:Name="story_Board" >
                <DoubleAnimationUsingKeyFrames Storyboard.TargetName="main_Grid"
                                    Storyboard.TargetProperty="Opacity"
                                    BeginTime="0:0:0">
                    <SplineDoubleKeyFrame  KeyTime="00:00:00.00" Value="1"/>
                    <SplineDoubleKeyFrame  KeyTime="00:00:00.400" Value="0.0"/>
                </DoubleAnimationUsingKeyFrames>
            </Storyboard>
        </UserControl.Resources>
        <Grid x:Name="main_Grid">
                <Border  CornerRadius="10"
                    Background="Transparent"
                    HorizontalAlignment="Center" 
                VerticalAlignment="Center"
                Padding="15,5">              
                        <TextBlock x:Name="showContent_textBlock" Margin="10,0,0,2" Foreground="WhiteSmoke"  FontSize="30"/>                              
                </Border>
        </Grid>
    </UserControl>
    

    UserControlDemo.xaml.cs:

    public sealed partial class UserControlDemo : UserControl
        {
            private Popup popup;
            private string str;
            private TimeSpan showTime_tmr;
            public UserControlDemo()
            {
                this.InitializeComponent();
                popup = new Popup();
                popup.Child = this;
                MeasurePopupSize();
                this.Loaded += NotifyPopup_Loaded;
                this.Unloaded += NotifyPopup_Unloaded;
            }
    
            public VolumeContentDialog(string content, TimeSpan showTime) : this()
            {
                this.str = content;
                this.showTime_tmr = showTime;
    
            }
    
            public VolumeContentDialog(string content) : this(content, TimeSpan.FromSeconds(2))
            {
            }
    
            public void Show()
            {
                this.popup.IsOpen = true;
            }
           
            public void Hide()
            {
                this.popup.IsOpen = false;
            }
    
            private void MeasurePopupSize()
            {
                this.Width = ApplicationView.GetForCurrentView().VisibleBounds.Width;
    
                double marginTop = 0;
                if (ApiInformation.IsTypePresent("Windows.UI.ViewManagement.StatusBar"))
                    marginTop = StatusBar.GetForCurrentView().OccludedRect.Height;
                this.Height = ApplicationView.GetForCurrentView().VisibleBounds.Height;
                this.Margin = new Thickness(0, marginTop, 0, 0);
            }
    
            private void NotifyPopup_Loaded(object sender, RoutedEventArgs e)
            {
                this.showContent_textBlock.Text = str;
                this.story_Board.BeginTime = this.showTime_tmr;
                this.story_Board.Begin();
                this.story_Board.Completed += storyBoard_Completed;
                ApplicationView.GetForCurrentView().VisibleBoundsChanged += NotifyPopup_VisibleBoundsChanged;
            }
            private void NotifyPopup_VisibleBoundsChanged(ApplicationView sender, object args)
            {
                MeasurePopupSize();
            }
    
            private void storyBoard_Completed(object sender, object e)
            {
                this.popup.IsOpen = false;
            }
    
            private void NotifyPopup_Unloaded(object sender, RoutedEventArgs e)
            {
                ApplicationView.GetForCurrentView().VisibleBoundsChanged -= NotifyPopup_VisibleBoundsChanged;
            }
        }

    然后直接在MianPage.cs中实例化引用就可以了。

    MainPage.xaml.cs:

    UserControlDemo demo = new UserControlDemo("Demo");
    demo.Show();
    
  • 相关阅读:
    【276】◀▶ Python 字符串函数说明
    Spring事务配置的五种方式 巨全!不看后悔,一看必懂!
    Android Developers:两个视图渐变
    《Linux命令行与shell脚本编程大全》 第二十七章 学习笔记
    Android的TextView与Html相结合的用法
    嵌入式C语言优化小技巧
    vxworks获取系统时间编程
    【算法与数据结构】在n个数中取第k大的数(基础篇)
    字符集转换 字符类型转换 utf-8 gb2312 url
    java 从零开始,学习笔记之基础入门<Oracle_基础>(三十三)
  • 原文地址:https://www.cnblogs.com/singhwong/p/11918463.html
Copyright © 2020-2023  润新知