仅在特定时间显示 window 然后关闭它
Show window only for a specifc time and then close it
我想在特定时间后关闭我的 window,但这取决于 属性。如果这是真的,它应该在 6 秒后关闭,否则在 3 秒后关闭。我想完成 xaml 中的几乎所有内容。我对 C# 代码不满意。
到目前为止我已经尝试过:
我的xaml:
<Window x:Class="embedUserc.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
xmlns:embedUserc="clr-namespace:embedUserc">
<Window.Triggers>
<DataTrigger Binding="{Binding IsSomething}" Value="False" >
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard Duration="0:0:6" FillBehavior="Stop" >
<BooleanAnimationUsingKeyFrames Storyboard.TargetName="CloseBehavior" Storyboard.TargetProperty="Close">
<DiscreteBooleanKeyFrame KeyTime="0:0:5" Value="True" />
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
<DataTrigger Binding="{Binding IsSomething}" Value="True" >
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard Duration="0:0:4" FillBehavior="Stop" >
<BooleanAnimationUsingKeyFrames Storyboard.TargetName="CloseBehavior" Storyboard.TargetProperty="Close" >
<DiscreteBooleanKeyFrame KeyTime="0:0:3" Value="True" />
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</Window.Triggers>
<Grid>
<embedUserc:WindowCloseBehavior x:Name="CloseBehavior"/>
</Grid>
我的 C# 代码:
using System.Windows;
namespace embedUserc
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new ViewModel();
}
}
public class WindowCloseBehavior : FrameworkElement
{
public static void SetClose(DependencyObject target, bool value)
{
target.SetValue(CloseProperty, value);
}
public static readonly DependencyProperty CloseProperty =
DependencyProperty.RegisterAttached(
"Close",
typeof(bool),
typeof(WindowCloseBehavior),
new UIPropertyMetadata(false, OnClose));
private static void OnClose(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
if (e.NewValue is bool && ((bool)e.NewValue))
{
Window window = GetWindow(sender);
if (window != null)
window.Close();
}
}
private static Window GetWindow(DependencyObject sender)
{
Window window = null;
if (sender is Window)
window = (Window)sender;
return window ?? (Window.GetWindow(sender));
}
}
public class ViewModel
{
public bool IsSomething { get { return true; } }
}
}
我现在明白了,这里是工作 xaml 使用相同的 c# 代码:
<Window.Resources>
<Style x:Key="CloseStyle">
<Style.Triggers>
<DataTrigger Binding="{Binding IsSomething}" Value="False" >
<DataTrigger.EnterActions>
<BeginStoryboard >
<Storyboard Duration="0:0:6" FillBehavior="Stop" >
<BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="Close" >
<DiscreteBooleanKeyFrame KeyTime="0:0:5" Value="True" />
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
<DataTrigger Binding="{Binding IsSomething}" Value="True" >
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard Duration="0:0:4" FillBehavior="Stop" >
<BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="Close" >
<DiscreteBooleanKeyFrame KeyTime="0:0:3" Value="True" />
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</Style.Triggers>
</Style>
我想在特定时间后关闭我的 window,但这取决于 属性。如果这是真的,它应该在 6 秒后关闭,否则在 3 秒后关闭。我想完成 xaml 中的几乎所有内容。我对 C# 代码不满意。
到目前为止我已经尝试过:
我的xaml:
<Window x:Class="embedUserc.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
xmlns:embedUserc="clr-namespace:embedUserc">
<Window.Triggers>
<DataTrigger Binding="{Binding IsSomething}" Value="False" >
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard Duration="0:0:6" FillBehavior="Stop" >
<BooleanAnimationUsingKeyFrames Storyboard.TargetName="CloseBehavior" Storyboard.TargetProperty="Close">
<DiscreteBooleanKeyFrame KeyTime="0:0:5" Value="True" />
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
<DataTrigger Binding="{Binding IsSomething}" Value="True" >
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard Duration="0:0:4" FillBehavior="Stop" >
<BooleanAnimationUsingKeyFrames Storyboard.TargetName="CloseBehavior" Storyboard.TargetProperty="Close" >
<DiscreteBooleanKeyFrame KeyTime="0:0:3" Value="True" />
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</Window.Triggers>
<Grid>
<embedUserc:WindowCloseBehavior x:Name="CloseBehavior"/>
</Grid>
我的 C# 代码:
using System.Windows;
namespace embedUserc
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new ViewModel();
}
}
public class WindowCloseBehavior : FrameworkElement
{
public static void SetClose(DependencyObject target, bool value)
{
target.SetValue(CloseProperty, value);
}
public static readonly DependencyProperty CloseProperty =
DependencyProperty.RegisterAttached(
"Close",
typeof(bool),
typeof(WindowCloseBehavior),
new UIPropertyMetadata(false, OnClose));
private static void OnClose(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
if (e.NewValue is bool && ((bool)e.NewValue))
{
Window window = GetWindow(sender);
if (window != null)
window.Close();
}
}
private static Window GetWindow(DependencyObject sender)
{
Window window = null;
if (sender is Window)
window = (Window)sender;
return window ?? (Window.GetWindow(sender));
}
}
public class ViewModel
{
public bool IsSomething { get { return true; } }
}
}
我现在明白了,这里是工作 xaml 使用相同的 c# 代码:
<Window.Resources>
<Style x:Key="CloseStyle">
<Style.Triggers>
<DataTrigger Binding="{Binding IsSomething}" Value="False" >
<DataTrigger.EnterActions>
<BeginStoryboard >
<Storyboard Duration="0:0:6" FillBehavior="Stop" >
<BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="Close" >
<DiscreteBooleanKeyFrame KeyTime="0:0:5" Value="True" />
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
<DataTrigger Binding="{Binding IsSomething}" Value="True" >
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard Duration="0:0:4" FillBehavior="Stop" >
<BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="Close" >
<DiscreteBooleanKeyFrame KeyTime="0:0:3" Value="True" />
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</Style.Triggers>
</Style>