2

I have a WPF Application and there's a button which shows an image (Splash Screen) containing the company logo and the name of the developers of the application. I want this image be shown until the user interacts with whatever. When user clicks or enters a keyboard key, the image must close. Refer to the comment line in my code.

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));
}
4

2 回答 2

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);        
}

这将允许您在用户按下某个键后关闭启动屏幕。

于 2013-11-07T19:46:33.383 回答
1

Reed 的答案无疑是最简单的,但您也可以完全避免使用 SplashScreen,而只需使用自定义 Window 即可完全控制。

你的代码.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();
    }
}
于 2013-11-07T22:22:37.430 回答