我在帮助器下方显示或关闭给定窗口:
public static class Splasher
{
private static Window mSplash;
public static Window SplashScreen
{
get
{
return mSplash;
}
set
{
mSplash = value;
}
}
public static void ShowSplashScreen()
{
if (mSplash != null)
{
try
{
mSplash.Show(); // <--- HERE IT THROWS EXCEPTION
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
public static void ShowDialogSplashScreen()
{
if (mSplash != null)
{
mSplash.ShowDialog();
}
}
public static void CloseSplashScreen()
{
if (mSplash != null)
{
mSplash.Close();
if (mSplash is IDisposable)
{
(mSplash as IDisposable).Dispose();
}
}
}
}
我还有以下WPF窗口:
<Window x:Class="My.Tools.XAML.Controls.Windows.WpfSplashScreen"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:My.Tools.XAML.Controls.Windows"
xmlns:gif="http://wpfanimatedgif.codeplex.com"
ResizeMode="NoResize" WindowStyle="None"
WindowStartupLocation="CenterScreen"
Title="WpfSplashScreen"
Height="305" Width="585"
Loaded="Window_Loaded">
<Window.Background>
<ImageBrush ImageSource="/My.Tools.XAML;component/Resources/Background.jpg"/>
</Window.Background>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="0.5*" />
<RowDefinition Height="2*" />
<RowDefinition Height="0.5*" />
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50*" />
<ColumnDefinition Width="50*" />
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0" Orientation="Horizontal" Margin="8 5 0 0">
<Image x:Name="Logo" Width="24" Height="24" VerticalAlignment="Top" />
<Label x:Name="ScreenTitle" FontFamily="Microsoft Sans Serif" FontSize="11.25" Foreground="White"
VerticalAlignment="Top" VerticalContentAlignment="Center">ScreenTitle</Label>
</StackPanel>
<StackPanel Grid.Column="1" Orientation="Horizontal" HorizontalAlignment="Right" >
<Label x:Name="MinimizeButton" FontFamily="Webdings" FontSize="18" Foreground="White" HorizontalContentAlignment="Right" MouseLeftButtonUp="MinimizeButton_MouseLeftButtonUp" MouseEnter="MinimizeButton_MouseEnter" MouseLeave="MinimizeButton_MouseLeave">0</Label>
<Label x:Name="CloseButton" FontFamily="Webdings" FontSize="18" Foreground="White" HorizontalContentAlignment="Right" MouseLeftButtonUp="CloseButton_MouseLeftButtonUp" MouseEnter="CloseButton_MouseEnter" MouseLeave="CloseButton_MouseLeave">r</Label>
</StackPanel>
</Grid>
<StackPanel Grid.Row="1" VerticalAlignment="Center" SnapsToDevicePixels="False" Orientation="Vertical">
<Label x:Name="Message"
HorizontalAlignment="Center" HorizontalContentAlignment="Center" VerticalContentAlignment="Bottom"
FontFamily="Microsoft Sans Serif" FontSize="36" FontStyle="Italic" FontWeight="Bold"
Foreground="White" Content="Message"/>
<Label x:Name="Submessage"
Margin="0 0 0 25"
FontFamily="Microsoft Sans Serif" FontSize="10" Foreground="White"
HorizontalAlignment="Center" HorizontalContentAlignment="Center" VerticalContentAlignment="Top"
Content="Submessage"/>
<Image x:Name="img" Stretch="None" HorizontalAlignment="Center"/>
</StackPanel>
<StackPanel Grid.Row="2" Orientation="Horizontal">
<Label x:Name="CurrentTask"
Margin="8 0 0 3"
FontFamily="Microsoft Sans Serif" FontSize="18" FontStyle="Italic"
Foreground="White"
Content="Loading"
HorizontalContentAlignment="Right" VerticalContentAlignment="Bottom"/>
<Label x:Name="Dots"
Margin="0 0 0 3"
FontFamily="Microsoft Sans Serif" FontSize="18" FontStyle="Italic"
Foreground="White"
HorizontalContentAlignment="Left" VerticalContentAlignment="Bottom">
<Label.Style>
<Style TargetType="Label">
<Style.Triggers>
<EventTrigger RoutedEvent="Label.Loaded">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Content" Duration="00:00:00.8" RepeatBehavior="Forever">
<DiscreteObjectKeyFrame KeyTime="00:00:00.0" Value=""/>
<DiscreteObjectKeyFrame KeyTime="00:00:00.2" Value="."/>
<DiscreteObjectKeyFrame KeyTime="00:00:00.4" Value=".."/>
<DiscreteObjectKeyFrame KeyTime="00:00:00.6" Value="..."/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Style.Triggers>
</Style>
</Label.Style>
</Label>
</StackPanel>
</Grid>
</Window>
窗口代码隐藏:
public partial class WpfSplashScreen : Window
{
public WpfSplashScreen()
{
InitializeComponent();
}
public WpfSplashScreen(string waitMessage, string message, string submessage, string screenTitle, Icon icon)
: this()
{
ImageSource imageSource = icon.ToImageSourceW();
this.Icon = imageSource;
this.logo = imageSource;
this.CurrentTask.Content = waitMessage;
this.Message.Content = message;
this.Submessage.Content = submessage;
this.ScreenTitle.Content = screenTitle;
this.Logo.Source = logo;
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
var image = new BitmapImage();
image.BeginInit();
image.UriSource = new Uri("/My.Tools.XAML;component/Resources/metroProgressBar.gif", UriKind.Relative);
image.EndInit();
ImageBehavior.SetAnimatedSource(img, image);
ImageBehavior.SetAutoStart(img, true);
ImageBehavior.SetRepeatBehavior(img, RepeatBehavior.Forever);
}
// Other stuff
}
当我的程序启动时,我执行以下操作,从视图模型中创建的后台线程中打开启动画面:
Splasher.SplashScreen = new WpfSplashScreen("Loading",
entryAssemblyInfo.ProductTitle,
string.Format("Version {0}",
entryAssemblyInfo.Version),
entryAssemblyInfo.ProductTitle,
GetWindowIcon());
Splasher.SplashScreen.Closed += OnSplashScreenClosed;
Splasher.ShowSplashScreen();
当我调用Splasher.ShowSplashScreen()时,会调用Splasher助手类中的方法:
public static void ShowSplashScreen()
{
if (mSplash != null)
{
try
{
mSplash.Show(); // <--- HERE IT THROWS EXCEPTION
}
catch (Exception e)
{
Console.WriteLine(e.Message); // despite exception is thrown it never reaches here.
}
}
}
在上面的方法中,在调试时和按F11之后,行mSplash.Show()抛出异常:
类型'System.IO.FileNotFoundException'的未处理异常 发生在mscorlib.dll
无论如何程序正确执行。窗口正确打开。调试时,输出窗口中会显示此异常。
这发生在Window_Loaded方法之前,因此我认为在加载xaml视图时它就是这样。根据microsoft,这个异常意味着程序没有找到我指定的文件。我在WpfSplashScreen WPF窗口中指定的唯一文件是视图中的背景:
<Window.Background>
<ImageBrush ImageSource="/My.Tools.XAML;component/Resources/Background.jpg"/>
</Window.Background>
Background.jpg属性在编译操作中设置为资源,不要复制。
在Window_Loaded事件中,动画进度条gif但尚未执行Window_Loaded(在此之前抛出异常)。
我做错了什么?