私の WPF アプリケーションには、アプリケーション情報とバージョンを含む About Box があります。このウィンドウが読み込まれると、特に初めて開く場合は少し時間がかかります。アプリケーションが引き続き応答しているように見えるように、ウィンドウが開いている間に読み込みアニメーションを実装しようとしています。
これを実装するために C# BackgroundWorkerを使用しようとしましたが、読み込みアニメーション (about ボックスを開く) を追加しようとしているプロセスは UI スレッドでしか実行できないため、機能しません。新しいスレッドを作成して STA アパートメントに配置しようとしましたが、うまくいきませんでした。
これは、アバウト ボックスを起動し、読み込みアニメーションの開始/停止を制御する方法です。
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>