每次调用项目的起始窗口时都会出现启动画面。

时间:2014-01-16 10:28:46

标签: c# wpf xaml

我已经为我的项目实现了一个启动画面,它可以根据需要运行良好..但在我的项目中我有一个用户注销选项,这显示了提供不同登录的开始页面(这是起始屏幕..即,“chooselogin.xaml”)。因此,当用户点击“选择其他登录信息”时,他已经在应用程序中选择了一个..再次出现闪屏,这不是必需的,看起来很奇怪。

以下代码是我认为会导致问题......伙伴

public partial class Chooselogin : Window
 {
     public Chooselogin()
     {
         new SplashWindow().ShowDialog();
         InitializeComponent();
     }

...

此代码是我的“App.xaml”..

<Application x:Class="WpfApplication1.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 StartupUri="Chooselogin.xaml">
       <Application.Resources>
           <ResourceDictionary Source="/Themes/ExpressionDark.xaml"/>
       </Application.Resources>

启动画面代码如下..

public partial class SplashWindow : Window
    {
        Thread loadingThread;
        Storyboard Showboard;
        Storyboard Hideboard;
        private delegate void ShowDelegate(string txt);
        private delegate void HideDelegate();
        ShowDelegate showDelegate;
        HideDelegate hideDelegate;

        public SplashWindow()
        {
            InitializeComponent();
            showDelegate = new ShowDelegate(this.showText);
            hideDelegate = new HideDelegate(this.hideText);
            Showboard = this.Resources["showStoryBoard"] as Storyboard;
            Hideboard = this.Resources["HideStoryBoard"] as Storyboard;
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            loadingThread = new Thread(load);
            loadingThread.Start();
        }
        private void load()
        {
            Thread.Sleep(1000);
            this.Dispatcher.Invoke(showDelegate, "Loading assets...please wait");
            Thread.Sleep(2000);
            //do some loading work
            this.Dispatcher.Invoke(hideDelegate);

            Thread.Sleep(2000);
            this.Dispatcher.Invoke(showDelegate, "Loading profiles..");
            Thread.Sleep(2000);
            //do some loading work
            this.Dispatcher.Invoke(hideDelegate);

            Thread.Sleep(2000);
            this.Dispatcher.Invoke(showDelegate, "Loading Data... almost done");
            Thread.Sleep(2000);
            this.Dispatcher.Invoke(hideDelegate);

            //close the window
            Thread.Sleep(2000);
            this.Dispatcher.Invoke(DispatcherPriority.Normal,
        (Action)delegate() { Close(); });
        }
        private void showText(string txt)
        {
            txtLoading.Text = txt;
            BeginStoryboard(Showboard);
        }
        private void hideText()
        {
            BeginStoryboard(Hideboard);
        }

}

启动画面应该在应用程序启动时打开..请帮助人..

2 个答案:

答案 0 :(得分:1)

这样简单的事情怎么样?:

public partial class Chooselogin : Window
{
    private static bool isFirstTime = true;

    public Chooselogin()
    {
        if (isFirstTime)
        {
            new SplashWindow().ShowDialog();
            isFirstTime = false;
        }
        InitializeComponent();
    }

    ...
}

现在它只会显示一次启动画面。

答案 1 :(得分:1)

我推荐Kent Boogaart的reading this post

帖子中的示例

“WPF提供了一个SplashScreen类。它设计简单,解决了启动屏幕的主要目标:即时反馈。借助于放弃WPF堆栈,而是依靠Windows Imaging Component(WIC)来显示图像,它提供了在编写自己的本机引导程序时获得屏幕效果的最快路径。“