完全详解--打造自动消失的对话框

简介:
 
 
 
1:新建自定义控件PopupBorder,作为Popup的child。代码如下:

 

<UserControl x:Class="SLStudy.PopupBorder"
   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"
    mc:Ignorable="d">
    <UserControl.Resources>
        <Storyboard x:Name="myStoryboard">
            <DoubleAnimation
                Storyboard.TargetName="LayoutRoot"
                Storyboard.TargetProperty="Opacity"
                    From="1.0" To="0" Duration="0:0:1"
                    AutoReverse="True" />
        </Storyboard>
    </UserControl.Resources>

    <Grid x:Name="LayoutRoot" >
        <!--<Border Background="#FFCC0D0D" BorderThickness="0" CornerRadius="5">-->
        <Border BorderThickness="0" CornerRadius="5">
            <Border.Background>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="#FF27A3D7" Offset="0.51"/>
                    <GradientStop Color="#FF76C2E1" Offset="0.004"/>
                    <GradientStop Color="#FF27A3D7" Offset="1"/>
                </LinearGradientBrush>
            </Border.Background>
            <Border.Effect>
                <DropShadowEffect BlurRadius="10" Color="#FFCC0D0D" ShadowDepth="0"/>
            </Border.Effect>
            <TextBlock x:Name="txtMessage" HorizontalAlignment="Center" Margin="10" VerticalAlignment="Center"
                       FontSize="28" Foreground="White" FontFamily="Comic Sans MS"
                       >This is a Simple Example</TextBlock>
        </Border>
    </Grid>
</UserControl>

image

 

新建PopupDemo页面,代码如下:
<Grid x:Name="LayoutRoot">
        <StackPanel>
            <Button Content="Show" Click="ShowPopup_Clicked"></Button>
        </StackPanel>
    </Grid>
 
后台cs代码为:
private void ShowPopup_Clicked(object sender, RoutedEventArgs e)
        {
            Popup popup = new Popup();

            //设置popup的Child属性为自定义的用户控件。
             popup.Child = new PopupBorder();
            popup.IsOpen = true;
        }

 

运行,效果如下:

 

注册popUp的LayoutUpdated事件,在触发LayoutUpdated事件时,设置popUp的Margin属性:

 

popUp.LayoutUpdated += delegate
            {
                popUp.Margin = new Thickness(
                        (App.Current.Host.Content.ActualWidth - pborder.ActualWidth) / 2,
                        (App.Current.Host.Content.ActualHeight - pborder.ActualHeight) / 2,
                        0,
                        0);
            };

完整的代码如下:

 

PopupBorder pborder = new PopupBorder();

Popup popUp = new Popup();

//设置popup的Child属性为自定义的用户控件。
popUp.Child = pborder;
popUp.LayoutUpdated += delegate
{
    popUp.Margin = new Thickness(
            (App.Current.Host.Content.ActualWidth - pborder.ActualWidth) / 2,
            (App.Current.Host.Content.ActualHeight - pborder.ActualHeight) / 2,
            0,
            0);
};
popUp.IsOpen = true;

 

 

运行可以发现弹出的消息已经居中了,那么如何让它自动消失呢??,

要想自动消失还得使用定时器,过了一段时间后定时器将popUp控件的 IsOpen属性设置为false,这样窗口就关闭了。

于是在LayoutUpdated中增加定时器代码:

popUp.LayoutUpdated += delegate
            {
                popUp.Margin = new Thickness(
                        (App.Current.Host.Content.ActualWidth - pborder.ActualWidth) / 2,
                        (App.Current.Host.Content.ActualHeight - pborder.ActualHeight) / 2,
                        0,
                        0);

                System.Threading.Timer timer = new System.Threading.Timer(
                    (state) =>
                    {
                        popUp.Dispatcher.BeginInvoke(() =>
                        {
                            popUp.IsOpen = false;
                        });
                    }, null, 500, 500);
            };

 

在过了500秒后,将popUp. IsOpen设置为false。

运行可以发现窗口可以自动消失了。

 

可以看到弹出窗口一下就关闭了,那么能不能慢慢的渐变的消失呢??

为了实现渐变的消失,那么就应该使用动画了。

首先在PopupBorder中增加

<UserControl.Resources>
        <Storyboard x:Name="myStoryboard">
            <DoubleAnimation
                Storyboard.TargetName="LayoutRoot"
                Storyboard.TargetProperty="Opacity"
                    From="1.0" To="0" Duration="0:0:1"
                    AutoReverse="True" />
        </Storyboard>
    </UserControl.Resources>

 

当然上面的PopupBorder代码中已经有了这段代码了,动画使用了DoubleAnimation,设置LayoutRoot 对象的Opacity属性在1秒的时间内从1,变到0。

接着在PopupDemo页面的按钮事件里面在popUp.Child = pborder;后面增加如下代码来执行动画:

//设置popup的Child属性为自定义的用户控件。
popUp.Child = pborder;

pborder.myStoryboard.Completed += delegate
{
    popUp.IsOpen = false;
};
pborder.myStoryboard.Begin();

 

然后将Clicked中的代码进行重构。

新建MessageBoxHelper类:

代码如下:

public class MessageBoxHelper
{
    #region 提示消?息¡é

    /// <summary>
    /// 弹出提示消息标题为提示,按钮为确定
    /// </summary>
    /// <param name="msg"></param>
    public static void ShowMessage(string msg)
    {
        //ShowFriendMessage(msg, "提示", MessageBoxButton.OK);

        PopupBorder pborder = new PopupBorder();
        pborder.txtMessage.Text = " " + msg + " ";

        pborder.UpdateLayout();

        Popup popUp = new Popup();
        popUp.Child = pborder;


        pborder.myStoryboard.Completed += delegate
        {
            popUp.IsOpen = false;
        };
        pborder.myStoryboard.Begin();


        popUp.InvokeOnLayoutUpdated(() =>
        {
            popUp.Margin = new Thickness(
                (App.Current.Host.Content.ActualWidth - pborder.ActualWidth) / 2,
                (App.Current.Host.Content.ActualHeight - pborder.ActualHeight) / 2,
                0,
                0);

            System.Threading.Timer timer = new System.Threading.Timer(
                (state) =>
                {
                    popUp.Dispatcher.BeginInvoke(() =>
                    {
                        popUp.IsOpen = false;
                    });
                }, null, 500, 500);
        });
        popUp.IsOpen = true;

    }

    /// <summary>
    /// 弹出提示消息按钮为确定
    /// </summary>
    /// <param name="msg"></param>
    public static void ShowMessage(string msg, string title)
    {
        ShowMessage(msg, title, MessageBoxButton.OK);
    }

    /// <summary>
    /// 弹出提示消息
    /// </summary>
    /// <param name="msg"></param>
    public static void ShowMessage(string msg, string title, MessageBoxButton buttons)
    {
        MessageBox.Show(msg, title, buttons);
    }

    #endregion
}

使用的时候只需要MessageBoxHelper.ShowMessage(“Hello World”);就可以了。注意别忘记了PopupBorder控件。






本文转自LoveJenny博客园博客,原文链接:http://www.cnblogs.com/LoveJenny/archive/2011/07/14/2105869.html,如需转载请自行联系原作者
目录
相关文章
|
5月前
点击增加按钮,添加input,超过三个则增加按钮隐藏
点击增加按钮,添加input,超过三个则增加按钮隐藏
61 0
对话框完全显示后,马上执行一个按钮的事件
对话框完全显示后,马上执行一个按钮的事件
创建动态工具栏并设置工具栏按钮提示
1、创建对话框程序,导入8个图标资源。 2、在对话框头文件中声明变量,代码如下: CToolBar m_ToolBar; CImageList m_ImageList; CString  m_TipText;   3、在对话框中oninitdialg函数中创建工具栏,如下: //创建图像列表 m_ImageList.
932 0
|
C# Windows
C# 程序关闭托盘图标不会自动消失
原文:C# 程序关闭托盘图标不会自动消失 c#程序关闭托盘图标不会自动消失,进程的托盘图标却不能随着进程的结束而自动消失  必须将鼠标移到图标上面时才能消失?  请问如何才能做到图标随着进程的结束而自动消失呢(外部强行结束,如在任务管理器将其结束), windows系统好多程序都会这样。
1207 0
自定义状态切换按钮
最近在做一个项目,一个界面的按钮UI给画成了这样(默认状态是蓝色的然后触摸后变成灰色的) UI效果 然后本着给低版本系统APP适配的职业素养(其实是不想画这种按钮),想让UI兄弟给将图标改成整个按钮效果的图片,可是。
918 0