我有一个WPF应用程序,并且有一个显示图像(启动画面)的按钮,其中包含公司徽标和应用程序开发人员的名称。我希望在用户与任何内容交互之前显示此图像。当用户单击或输入键盘键时,图像必须关闭。请参阅我的代码中的注释行。
private void Info_BeforeCommandExecute(object sender, DevExpress.Xpf.Bars.ItemClickEventArgs e)
{
SplashScreen SS = new SplashScreen("Images/InfotecSplashScreenInfo.png");
SS.Show(false);
// Need to do something here to wait user to click or press any key
SS.Close(TimeSpan.FromSeconds(1));
}
答案 0 :(得分:3)
您可以使用AddKeyDownHandler:
为Keyboard类添加处理程序private void Info_BeforeCommandExecute(object sender, DevExpress.Xpf.Bars.ItemClickEventArgs e)
{
SplashScreen SS = new SplashScreen("Images/InfotecSplashScreenInfo.png");
SS.Show(false);
KeyEventHandler handler;
handler = (o,e) =>
{
Keyboard.RemoveKeyDownHandler(this, handler);
SS.Close(TimeSpan.FromSeconds(1));
};
Keyboard.AddKeyDownHandler(this, handler);
}
这将允许您在用户按下某个键后关闭启动画面。
答案 1 :(得分:1)
Reed的答案无疑是最简单的,但您也可以完全避免使用SplashScreen,只需使用自定义窗口即可完全控制。
<强> YourCode.cs 强>
private void Info_BeforeCommandExecute(object sender, DevExpress.Xpf.Bars.ItemClickEventArgs e)
{
SplashWindow SS = new SplashWindow();
SS.ShowDialog(); // ShowDialog will wait for window to close before continuing
}
<强> SplashWindow.xaml 强>
<Window x:Class="WpfApplication14.SplashWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="SplashWindow" Height="350" Width="525">
<Grid>
<!-- Your Image here, I would make a Custom Window Style with no border and transparency etc. -->
<Button Content="Click Me" Click="Button_Click"/>
</Grid>
</Window>
<强> SplashWindow.xaml.cs 强>
public partial class SplashWindow : Window
{
public SplashWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
// Once user clicks now we can close the window
// and/or add keyboard handler
this.Close();
}
}