在我的WPF应用程序中,我有一个关于Box的应用程序信息和版本。加载此窗口时需要花费一点时间,特别是当它第一次打开时。我正在尝试在窗口打开时实现加载动画,以便应用程序继续显示响应。
我已经尝试使用C#BackgroundWorker来实现这一点,但它不会起作用,因为我尝试添加加载动画的过程(关于框打开)是can only be run on the UI thread。我尝试创建一个新线程并将其放在STA公寓中,但它没有用。
这是我启动about框并控制加载动画的开始/停止的方法:
private void AboutMenuItem_OnClick(object sender, RoutedEventArgs e)
{
LoadingCircle.Start();
LoadingCircle.Visibility = Visibility.Visible;
var aboutBox = new AboutBox { Owner = this };
aboutBox.Show();
LoadingCircle.Stop();
LoadingCircle.Visibility = Visibility.Hidden;
}
在调用aboutBox.Show()
之前,加载圈不会出现并开始移动,我无法理解为什么会这样。如果我使用上面的代码运行我的应用程序,加载循环将在加载窗口之前短暂出现,但它不会旋转。
修改
似乎创建短延迟的只是窗口的创建,创建AboutBox的代码很简单:
public partial class AboutBox : Window
{
public AboutBox()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Close();
}
}
public class Version
{
public string UiVersion { get; set; }
public string ServiceVersion { get; set; }
public static Version GetVersion()
{
var ver = new Version();
Assembly assembly = Assembly.GetExecutingAssembly();
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
ver.UiVersion = fvi.FileVersion;
ver.ServiceVersion = "<Service Version>";
return ver;
}
}
这是XAML:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
x:Class="NDO.PC.DataViewer.AboutBox"
mc:Ignorable="d"
Height="384" Width="600" Title="About NanoDrop One" WindowStartupLocation="CenterOwner" AllowsTransparency="true" WindowStyle="None" Background="White">
<Window.Resources>
<Style x:Key="ButtonFocusVisual">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Rectangle Margin="2" SnapsToDevicePixels="true" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<LinearGradientBrush x:Key="ButtonNormalBackground" EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="#F3F3F3" Offset="0"/>
<GradientStop Color="#EBEBEB" Offset="0.5"/>
<GradientStop Color="#DDDDDD" Offset="0.5"/>
<GradientStop Color="#CDCDCD" Offset="1"/>
</LinearGradientBrush>
<SolidColorBrush x:Key="ButtonNormalBorder" Color="#FF707070"/>
<Style x:Key="OKButton" TargetType="{x:Type Button}">
<Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/>
<Setter Property="Background" Value="{StaticResource ButtonNormalBackground}"/>
<Setter Property="BorderBrush" Value="{StaticResource ButtonNormalBorder}"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Padding" Value="1"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate x:Name="OKButton" TargetType="{x:Type Button}">
<Border x:Name="Chrome" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" SnapsToDevicePixels="true" CornerRadius="5" BorderThickness="1">
<ContentPresenter Name="TextName" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsKeyboardFocused" Value="true">
<Setter Property="BorderBrush" TargetName="Chrome" Value="White"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="#ADADAD"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" Value="White" TargetName="Chrome"/>
<Setter Property="TextBlock.Foreground" Value="#FF0086FF" TargetName="TextName"/>
<Setter Property="BorderBrush" Value="#FF0086FF" TargetName="Chrome"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid x:Uid="ImageGrid" x:Name="ImageGrid" Grid.Row="0" VerticalAlignment="Top" HorizontalAlignment="Left">
<Grid.RowDefinitions>
<RowDefinition Height="79"/>
<RowDefinition Height="13"/>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Image x:Uid="AboutImage" x:Name="AboutImage" Source="Resources/AboutPageImage.jpg" Width="600" Height="79" Stretch="UniformToFill" />
<Border Grid.Row="1" x:Uid="Border_1" Margin="0,0,0,0" Height="13" MinWidth="600" VerticalAlignment="Bottom">
<Border.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<LinearGradientBrush.RelativeTransform>
<TransformGroup>
<ScaleTransform CenterY="0.5" CenterX="0.5"/>
<SkewTransform CenterY="0.5" CenterX="0.5"/>
<RotateTransform Angle="90" CenterY="0.5" CenterX="0.5"/>
<TranslateTransform/>
</TransformGroup>
</LinearGradientBrush.RelativeTransform>
<GradientStop Color="#FFE5EAEE" Offset="1"/>
<GradientStop Color="#FF0086FF" Offset="0.36"/>
</LinearGradientBrush>
</Border.Background>
</Border>
<Grid Margin="36,18,36,36" Grid.Row="2" Height="238">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="128px"/>
</Grid.ColumnDefinitions>
<StackPanel>
<TextBlock FontFamily="Segoe UI Semibold" FontSize="20" FontWeight="Bold" Foreground="#FF0086FF" Margin="0,0,0,12" VerticalAlignment="Top" HorizontalAlignment="Left"><Run Text="About Application"/></TextBlock>
<StackPanel Orientation="Horizontal">
<TextBlock Foreground="#FF0086FF"
Text="Software UI version: " />
<TextBlock Foreground="#FF0086FF"
Margin="5,0,0,0"
Text="{Binding UiVersion}" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Foreground="#FF0086FF"
Text="Software Service version: " />
<TextBlock Foreground="#FF0086FF"
Margin="5,0,0,0"
Text="{Binding ServiceVersion}" />
</StackPanel>
</StackPanel>
<Image Grid.Column="1" Source="Resources/TS_logo_rgb 200x61with spacing.png" Width="128" VerticalAlignment="Bottom" Margin="0,0,-38,-18"/>
<Button Height="25" Width="75" Background="#FF0086FF" BorderBrush="{x:Null}" Foreground="White" VerticalAlignment="Bottom" HorizontalAlignment="Left" Click="Button_Click" Style="{DynamicResource OKButton}" Content="OK"/>
</Grid>
</Grid>
答案 0 :(得分:4)
您尝试在UI线程上执行所有操作的问题。你需要背景或不阻止长时间运行的逻辑。
我建议使用await/async
。像这样的东西会起作用:
LoadingCircle.Start();
LoadingCircle.Visibility = Visibility.Visible;
var aboutBox = new AboutBox { Owner = this };
await Task.Run(() =>
{
aboutBox.Init();
});
aboutBox.Show();
LoadingCircle.Stop();
LoadingCircle.Visibility = Visibility.Hidden;
将长逻辑放入异步Task
并停止执行仅当前方法直到它返回。它不会阻止调用线程。请注意,您需要为此标记事件处理程序async
。
我在About框中创建了一个假的“Init”方法,这样你的构造函数就可以有效地做任何事情了(但是在UI线程上做了)。
此外,当使用MVVM以“正确”方式执行WPF时,这些问题会消失,因此请考虑将来使用该模式。它迫使你将你的观点和业务逻辑分开,因此后者的线程变得微不足道。
答案 1 :(得分:2)
如前所述,您应该分析窗口加载这么长时间的原因。您还可以选择其他选项:
移动代码,以便窗口快速显示,然后其他代码点击。使用实际窗口上的忙碌指示器标记正在加载
不要在点击时加载about窗口,之前加载它,就像在启动时一样,只需在单击按钮时显示它。
使用about窗口上的Window_Loaded
事件来完成工作,将其工作放在其他线程中。不要使用窗口的构造函数来做东西。
你应该结帐WPF Toolkit(也可以通过nuget获得)它有一个名为忙碌指标的控件。
繁忙指示器是一个控件,只显示进度条或其他内容并使背景变暗。您只需将属性 IsBusy 设置为true即可启用。因此,如果您的窗口正在进行大量工作,您可以在about窗口中显示此内容,直到该工作完成。
Here是一个教程。