【发布时间】:2014-01-17 22:24:33
【问题描述】:
我已经为我的项目实现了一个启动画面,它可以正常工作.. 但在我的项目中,我有一个用户注销选项,这会显示提供不同登录名的起始页面(这是起始屏幕..即“选择登录.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);
}
}
启动画面应该在应用程序开始时打开..请帮助大家..
【问题讨论】:
-
因为
new SplashWindow().ShowDialog();在ChooseLogin的构造函数中,所以每次创建ChooseLogin 窗口时都会调用它。我建议将这行代码移到您在应用程序启动时调用一次的方法中。 -
另外,
.ShowDialog()会一直阻塞,直到启动画面关闭。在这发生之前它不会继续InitializeComponent,所以这不是启动画面。 -
这是生产代码吗?我真的不希望