我有一个窗口,它只显示一个图像和一个字符串。如果5秒过去了,我"淡出"窗口,通过改变其Top属性。如果发生这种情况,它完全窃取了我的主窗口的焦点,我不能做任何事情,因为主窗口被阻塞了。我已经尝试了一些本地方法,但这对我不起作用。有没有一种方法可以完全删除这个窗口的焦点,这样它就不会窃取主窗口的焦点,也不会阻止它,当改变其他窗口的顶部属性?
UI xaml code:
<Window x:Class="Rikkachan.PlayedVideo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="PlayedVideo" Height="307" Width="542"
ResizeMode="NoResize" AllowsTransparency="True"
WindowStyle="None" Background="Transparent" ShowActivated="False">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<Image Name="picBox" Grid.Row="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Opacity="100"/>
<Label Name="lblPlayedVideo" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row="1" Opacity="100" Background="DimGray"/>
</Grid>
</Window>
下面是淡出之类的代码
public partial class PlayedVideo : Window
{
#region "Variables"
Rect workingArea = SystemParameters.WorkArea;
#endregion
#region "Initilizing"
public PlayedVideo()
{
InitializeComponent();
#region "Setting window at the lower right bottom"
this.EnableWindowMoving();
this.Left = workingArea.Right - this.Width;
this.Top = workingArea.Bottom - this.Height;
#endregion
picBox.Source = Screenshot.CreateScreenshotFromWindow("vlc").ToBitmapSource();
lblPlayedVideo.Content = "Media: " + PlayerProcessInformation.GetCurrentPlayedFile(SupportedPlayer.VLCMediaPlayer);
StartCloseTimer();
this.Closed += (sender, e) =>
{
Debug.WriteLine("I'm dead");
};
}
#endregion
#region "StartCloseTimer"
private void StartCloseTimer(double sec = 5)
{
this.Dispatcher.BeginInvoke((Action)(() =>
{
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(sec);
timer.Tick += (sender, e) =>
{
for (double i = this.Top; i < workingArea.Bottom; i += 10, Opacity -= 0.099) // look if we get this smoother
{
this.Top = i;
Debug.WriteLine("Top: " + this.Top + "rnBottom: " + workingArea.Bottom);
Thread.Sleep(200);
}
timer.Stop();
Debug.WriteLine("Timer stopped, now I'm going to die!");
this.Close();
};
timer.Start();
}));
}
#endregion
}
UI XAML代码:
<Window x:Class="Rikkachan.PlayedVideo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="PlayedVideo" Height="307" Width="542"
ResizeMode="NoResize" AllowsTransparency="True"
WindowStyle="None" Background="Transparent" ShowActivated="False"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Window.Triggers>
<EventTrigger RoutedEvent="Window.Loaded">
<BeginStoryboard>
<Storyboard Name="storyBoardBegin" Completed="storyBoardBegin_Completed">
<DoubleAnimation AutoReverse="False" Storyboard.TargetProperty="Opacity" From="0" To="1" Duration="0:0:3" FillBehavior="HoldEnd" RepeatBehavior="1x" />
<DoubleAnimation AutoReverse="False" Storyboard.TargetProperty="Top" From="{Binding TopEnd}" To="{Binding TopBegin}" Duration="0:0:1" RepeatBehavior="1x"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Window.Triggers>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<Image Name="picBox" Grid.Row="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Opacity="100"/>
<Label Name="lblPlayedVideo" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row="1" Opacity="100" Background="DimGray"/>
</Grid>
</Window>
背后的代码:
public partial class PlayedVideo : Window
{
#region "Variables"
Rect workingArea = SystemParameters.WorkArea;
private bool closeStoryBoardCompleted = false;
private Window m_wnd = null;
//private Storyboard storyBoardBegin;
//private Storyboard storyBoardEnd;
#endregion
#region "Initilizing"
public PlayedVideo(Window wnd)
{
InitializeComponent();
m_wnd = wnd;
#region "Setting window at the lower right bottom"
this.EnableWindowMoving();
this.Left = workingArea.Right - this.Width;
this.Top = workingArea.Bottom - this.Height;
#endregion
StartCloseTimer();
this.Closed += (sender, e) =>
{
Debug.WriteLine("I'm dead");
};
}
#endregion
#region "StartCloseTimer"
private void StartCloseTimer(double sec = 5)
{
Task.Run(() =>
{
this.Dispatcher.BeginInvoke((Action)(() =>
{
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(sec);
timer.Tick += (sender, e) =>
{
if (!closeStoryBoardCompleted)
{
DoubleAnimation animation = new DoubleAnimation(0, (Duration)TimeSpan.FromSeconds(sec));
DoubleAnimation animation2 = new DoubleAnimation(workingArea.Bottom, (Duration)TimeSpan.FromSeconds(sec));
m_wnd.Focus();
animation.Completed += (s, _) =>
{
Debug.WriteLine("Timer stopped, now I'm going to die!");
this.Close();
};
this.BeginAnimation(UIElement.OpacityProperty, animation);
this.BeginAnimation(Window.TopProperty, animation2);
}
};
timer.Start();
}));
});
}
#endregion
private void storyBoardBegin_Completed(object sender, EventArgs e)
{
picBox.Source = Screenshot.CreateScreenshotFromWindow("vlc").ToBitmapSource();
lblPlayedVideo.Content = "Media: " + PlayerProcessInformation.GetCurrentPlayedFile(SupportedPlayer.VLCMediaPlayer);
}
public double TopBegin{get { return workingArea.Bottom - this.Height; }}
public double TopEnd { get { return workingArea.Bottom; } }
}
查看WPF动画。您可以使用内置的WPF特性集,而不是使用现有的代码。动画开始和持续时间的计时器包含在功能集中,它的设计是为了更好地管理UI焦点,而不是使用自己的Dispatcher调用。
可以在这里找到一个入门教程:
http://dotnetslackers.com/articles/wpf/IntroductionToWPFAnimations.aspx下面是一个基于代码(而不是基于xaml)的思想介绍:
http://www.codeproject.com/Articles/23257/Beginner-s-WPF-Animation-Tutorial还有,官方文档。让元素淡入淡出的教程可能特别适用。
http://msdn.microsoft.com/en-us/library/ms752312.aspx