WPF 制作一个固定时间自动关闭的弹出页面
<Window x:Class="NewCallSystem.Views.WindowMessageView"
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:NewCallSystem.Views"
AllowsTransparency="True"
WindowStyle="None"
mc:Ignorable="d"
WindowState="Maximized"
Title="WindowX"
Background="Transparent">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/NewCallSystem;component/Resources/FontFamilies.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style TargetType="Button"
x:Key="exitButton">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border CornerRadius="10"
BorderThickness="1">
<TextBlock HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
FontSize="{TemplateBinding FontSize}"
Foreground="{TemplateBinding Foreground}"
Width="80"
FontFamily="{StaticResource MediumFontFamily}"
Height="auto"
FontWeight="{TemplateBinding FontWeight}"
Text="{TemplateBinding Content}" />
<Border.Background>
#5E80FB
</Border.Background>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
</Window.Resources>
<Border Background="#50262A31">
<Border Background="#FFFFFF"
CornerRadius="20"
Width="896"
Height="470"
BorderThickness="1">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="180" />
<RowDefinition Height="100" />
<RowDefinition />
</Grid.RowDefinitions>
<StackPanel VerticalAlignment="Top"
HorizontalAlignment="Right"
Orientation="Horizontal"
Margin="0,20,20,0">
<TextBlock FontSize="40"
d:Text="30"
Foreground="#8C93AA"
FontFamily="{StaticResource BasicFontFamily}"
Text="{Binding Path=Secord,RelativeSource={RelativeSource AncestorType=Window}}" />
<TextBlock FontSize="40"
Foreground="#8C93AA"
FontFamily="{StaticResource BasicFontFamily}"
Text="s" />
</StackPanel>
<Image Width="120"
Height="120"
VerticalAlignment="Bottom"
Source="pack://application:,,,/NewCallSystem;component/Images/icon_warn.png" />
<TextBlock FontSize="50"
Grid.Row="1"
Margin="0,40,0,0"
d:Text="考试信息不存在"
Text="{Binding Path=TipsInfo,RelativeSource={RelativeSource AncestorType=Window}}"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Foreground="#0D2060"
FontFamily="{StaticResource MediumFontFamily}" />
<Button Content="返回"
Grid.Row="2"
Width="200"
FontSize="40"
Height="100"
Foreground="#FFFFFF"
Style="{StaticResource exitButton}"
x:Name="Cancel" />
</Grid>
</Border>
</Border>
</Window>
public partial class WindowMessageView : Window
{
public int Secord
{
get { return (int)GetValue(SecordProperty); }
set { SetValue(SecordProperty, value); }
}
public static readonly DependencyProperty SecordProperty =
DependencyProperty.Register("Secord", typeof(int), typeof(WindowMessageView), new FrameworkPropertyMetadata(20));
public string TipsInfo
{
get { return (string)GetValue(TipsInfoProperty); }
set { SetValue(TipsInfoProperty, value); }
}
// Using a DependencyProperty as the backing store for TipsInfo. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TipsInfoProperty =
DependencyProperty.Register("TipsInfo", typeof(string), typeof(WindowMessageView), new PropertyMetadata(""));
private ICommand cancelCommand;
public ICommand CancelCommand
{
get
{
return cancelCommand;
}
set
{
cancelCommand = value;
this.Cancel.Command = value;
}
}
Timer _timer;
public bool _isStoped = false;
public WindowMessageView()
{
InitializeComponent();
Secord = (int)Secord;
if (Secord <= 0)
{
Secord = 0;
}
Secord++;
_timer = new Timer(OnTimerTicked, null, Timeout.Infinite, Timeout.Infinite);
_timer.Change(1000, Timeout.Infinite);
}
public WindowMessageView(string Tips,int Second)
{
InitializeComponent();
this.TipsInfo = Tips;
Secord = (int)Second;
if (Secord <= 0)
{
Secord = 0;
}
// Secord++;
_timer = new Timer(OnTimerTicked, null, Timeout.Infinite, Timeout.Infinite);
_timer.Change(1000, Timeout.Infinite);
}
private void OnTimerTicked(object state)
{
Dispatcher.Invoke(new Action(() =>
{
Secord--;
if (Secord <= 0)
{
// TriggerDefaultButton();
_isStoped = true;
_timer.Dispose();
this.Close();
}
}));
if (!_isStoped)
{
_timer.Change(1000, Timeout.Infinite);
}
}
}
调用: WindowMessageView window = new WindowMessageView("系统正在运行,无法重复运行", 5);
window.CancelCommand = new DelegateCommand(() =>
{
window.Close();
});
window.ShowDialog();