私はフレームとツリービューを持つWPFアプリケーションを持っています。 TreeViewは私のアプリケーションのメニューを表示し、アイテムがクリックされると、FrameはNavigate
を呼び出してそのメニュー項目の内容を表示します。その呼び出しには数秒かかる場合があり、ナビゲーションが完了するとUIがブロックされます。フレームの移動中に読み込みアイコンを表示するにはどうすればよいですか?
フレームがロードされている間に表示したいアニメーション旋回円(私はWaitIconと呼んでいます)を持つコントロールがありますが、表示されません(表示されても回転しません)スレッドはブロックされ、フレームのコンテンツがロードされます。フレームがUIスレッドによって所有されているため、私はバックグラウンドスレッドでNavigate
と呼ぶことはできません。フレームの読み込み中にWaitIconを表示する方法はありますか? WaitIconを所有する2番目のスレッドを作成している可能性がありますか?
UPDATE - 以下の問題を示すサンプルコード:ここで
がメインウィンドウです。私はちょうどWaitIconをシミュレートするためのアニメーション楕円を使用しました:
<Window x:Class="FrameLoadingTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<Storyboard x:Key="Animation" RepeatBehavior="Forever">
<ColorAnimation From="Red" To="Black" Duration="0:0:2" Storyboard.TargetName="Ellipse1" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)" />
</Storyboard>
</Window.Resources>
<Window.Triggers>
<EventTrigger RoutedEvent="FrameworkElement.Loaded">
<BeginStoryboard Storyboard="{StaticResource Animation}" />
</EventTrigger>
</Window.Triggers>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="5*" />
</Grid.ColumnDefinitions>
<Button Click="Button_Click">Load Frame</Button>
<Frame Name="ContentFrame" Grid.Column="1"></Frame>
<Ellipse Name="Ellipse1" Height="100" Width="100" Visibility="Hidden" Fill="Red">
</Ellipse>
</Grid>
</Window>
ボタンのクリックイベント:
private void Button_Click(object sender, RoutedEventArgs e)
{
Ellipse1.Visibility = Visibility.Visible;
ContentFrame.Source = new Uri("TestPage.xaml", UriKind.Relative);
}
テストページ:
<Page x:Class="FrameLoadingTest.TestPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="TestPage">
<Grid>
<TextBlock>Hello, world</TextBlock>
</Grid>
</Page>
そして、私は睡眠を置きます長時間実行される初期化をシミュレートするためにコンストラクタ内で実行します。
public partial class TestPage : Page
{
public TestPage()
{
InitializeComponent();
Thread.Sleep(3000);
}
}
楕円は、ページが読み込まれるまで表示されません。
サンプルを更新しました –