这是我的具有bool和string属性的ViewModel:
ViewModel : ValidatableBindableBase {
private bool _isBusy;
public bool IsBusy {
get { return _isBusy; }
set { SetProperty(ref _isBusy, value); }
}
private string _busyMessage;
public string BusyMessage {
get { return _busyMessage; }
set { SetProperty(ref _busyMessage, value); }
}
//Triggered when the button is clicked.
private async void Login() {
ShowBusyOverlay("Signing in");
// it will show if task.delay is added
//await Task.Delay(5000);
await loginService.SignIn();
}
private async void ShowBusyOverlay(string message) {
IsBusy = true;
BusyMessage = message;
}
}
XAML:注意:在Windows 10 15063上,可见性无需绑定即可绑定bool属性
<Grid Grid.RowSpan="3" Grid.ColumnSpan="3" Visibility="{Binding Path=IsBusy, UpdateSourceTrigger=PropertyChanged}">
<Rectangle Fill="Black" Opacity="0.4" />
<StackPanel VerticalAlignment="Center" HorizontalAlignment="Center">
<ProgressRing IsActive="True" Width="40" Height="40"/>
<TextBlock Text="{Binding BusyMessage}" FontSize="20" FontWeight="Thin"/>
</StackPanel>
</Grid>
目标:
它显示网格的速度不够快,因此它将导航到其他页面而不显示它,如何在它进入ShowBusyOverlay时立即显示该加载/网格?并建议吗?
答案 0 :(得分:1)
如何将加载/网格显示在ShowBusyOverlay上?
通过同步激活它,然后给UI线程时间来更新屏幕。
示例:
private async void Login()
{
BusyMessage = message;
IsBusy = true;
await Task.Delay( TimeSpan.FromSeconds( 10 ) );
IsBusy = false;
}
但是,如果您异步执行的任何任务都很快结束(或同步执行),那么忙碌的叠加层只会闪烁或根本不显示。我会使其淡入淡出,而不是立即出现/消失以减少闪烁的可见性/讨厌性。