我有一個WPF應用程序,我使用的是MVVM。等待光標在WPF PropertyChanged和綁定之間
在我看來,我的模型有:
private string logs;
public string Logs
{
get { return logs; }
set
{
logs = value;
OnPropertyChanged("Logs");
}
}
private void ExecLoadData()
{
using (new WaitCursor())
Logs = LogFile.ReturnContent();
}
private RelayCommand loadData;
public ICommand LoadData
{
get
{
if (loadData == null)
loadData = new RelayCommand(param => this.ExecLoadData());
return loadData;
}
}
在View:
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<i:InvokeCommandAction Command="{Binding LoadData}" />
</i:EventTrigger>
</i:Interaction.Triggers>
我注意到的是,OnPropertyChanged的頁面上的拍攝和呈現數據之間出現延遲。
我需要一種方法來顯示等待光標到要顯示在屏幕上的數據。
已經實現了WaitCursor()方法,但等待光標纔會出現,直到數據文件加載到內存中,也就是加載數據到內存中,直到數據顯示在頁面上,光標保持正常。
任何提示?
編輯(與AngelWPF的幫助,最終的解決方案):)
private Boolean isBusy = false;
public Boolean IsBusy
{
get { return isBusy; }
set
{
if (isBusy == value)
return;
isBusy = value;
OnPropertyChanged("IsBusy");
}
}
private string logs;
public string Logs
{
get { return logs; }
set
{
logs = value;
OnPropertyChanged("Logs");
}
}
public void ExecuteBusy(DoWorkEventHandler doWorkEventHandler)
{
IsBusy = true;
var backgroundWorker = new BackgroundWorker();
backgroundWorker.DoWork += doWorkEventHandler;
backgroundWorker.RunWorkerCompleted += (sender, e) => { IsBusy = false; };
backgroundWorker.RunWorkerAsync();
}
protected override void ExecLoadData()
{
LoadLogs();
}
private void LoadLogs()
{
ExecuteBusy((sender, e) =>
{
Logs = LogFile.ReturnContent();
});
}
<Page.Resources>
<ut:BooleanVisibilityConverter x:Key="BooleanVisibilityConverter" />
</Page.Resources>
<Page.DataContext>
<vm:ManutencaoMonitoracaoLogsViewModel/>
</Page.DataContext>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<i:InvokeCommandAction Command="{Binding LoadData}" />
</i:EventTrigger>
</i:Interaction.Triggers>
<Grid>
<TextBox Text="{Binding Logs, Mode=OneWay}" VerticalScrollBarVisibility="Auto" IsReadOnly="True" BorderBrush="White" />
<Border BorderBrush="Black" BorderThickness="1" Background="#80DBDBDB" Grid.RowSpan="3"
Visibility="{Binding IsBusy, Converter={StaticResource BooleanVisibilityConverter}}">
<Grid>
<ct:LoadingAnimation HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</Border>
</Grid>
嗨麥克!我編輯帖子e放更多的代碼。所以,我只需要知道如何在視圖中獲取「綁定後」事件。 – Alexandre