我有一個'加載數據'進程,可能需要1到20秒的任何地方,所以我想在我的窗口上顯示一個狀態消息。我的窗口網格有一行,根據綁定到我的視圖模型的觸發器將會顯示或隱藏一行。這很好。我的問題是我如何改變綁定,並獲得用戶界面刷新之前我長時間運行的任務開始。現在發生的情況是Status屬性已設置,然後我開始執行我的任務,但直到我的任務完成後才刷新UI。這是我的xaml和視圖模型代碼。WPF MVVM綁定刷新顯示狀態
<Grid Grid.Row="4" Background="{StaticResource appPrimaryBackColorLight}" Height="20">
<Grid.Style>
<Style TargetType="Grid">
<Style.Triggers>
<DataTrigger Binding="{Binding StatusMessageVisibility}" Value="Collapsed">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="Height" From="20" To="0" Duration="0:0:0.1" />
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
<DataTrigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="Height" From="0" To="20" Duration="0:0:.1" />
</Storyboard>
</BeginStoryboard>
</DataTrigger.ExitActions>
</DataTrigger>
</Style.Triggers>
</Style>
</Grid.Style>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center">
<Image Width="20" Height="20" Grid.RowSpan="2" ani:ImageBehavior.AnimatedSource="/Images/spinner_status_20.gif"></Image>
<TextBlock Text="{Binding StatusMessage}" Height="20" VerticalAlignment="Center" Padding="5,1,0,0" Style="{StaticResource appTextBlockSmallDark}" Foreground="White" />
</StackPanel>
</Grid>
private void LoadImportData()
{
// Show the status message by changing the view model properties
StatusMessage = "Loading data from import file. Please stand by...";
StatusMessageVisibility = Visibility.Visible;
Task.Factory.StartNew(() =>
{
//This invokes UI specific code inside module initialization
LoadDataNow();
}, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.FromCurrentSynchronizationContext());
}
「StatusMessage」和「StatusMessageVisibility」的設置看起來像什麼?兩個都實現了INotifyPropertyChanged嗎? – Jay
是的......我正在使用MVVM Light併爲每個調用RaisePropertyChanged(),以便通知視圖。 – Mikkeee