UWP 加载指示器未出现
UWP Loading indicator does not appear
我有一个 UWP 应用程序,我想在加载数据时显示一个内容对话框。
这是我的加载对话框,通过 LoadedCommand 调用。
private async Task LoadDataAsync()
{
await dialogService.ShowLoadingDialogAsync();
await LoadPaymentsAsync(new PaymentListFilterChangedMessage { TimeRangeStart = DateTime.Now.AddYears(DEFAULT_MONTH_BACK) });
//Refresh balance control with the current account
await BalanceViewModel.UpdateBalanceCommand.ExecuteAsync();
await dialogService.HideLoadingDialogAsync();
}
我还尝试通过 DispatcherHelper 调用它:
private async Task LoadDataAsync()
{
await DispatcherHelper.ExecuteOnUIThreadAsync(async () =>
{
await dialogService.ShowLoadingDialogAsync();
await LoadPaymentsAsync(new PaymentListFilterChangedMessage { TimeRangeStart = DateTime.Now.AddYears(DEFAULT_MONTH_BACK) });
//Refresh balance control with the current account
await BalanceViewModel.UpdateBalanceCommand.ExecuteAsync();
await dialogService.HideLoadingDialogAsync();
});
}
但这并没有改变任何事情。 DialogService 中的方法如下所示:
/// <summary>
/// Shows a loading Dialog.
/// </summary>
public async Task ShowLoadingDialogAsync(string? message = null)
{
// Be sure no other dialog is open.
await HideLoadingDialogAsync();
loadingDialog = new LoadingDialog { Text = message ?? Strings.LoadingLabel };
CoreApplicationView coreWindow = CoreApplication.MainView;
// Dispatcher needed to run on UI Thread
CoreDispatcher dispatcher = coreWindow.CoreWindow.Dispatcher;
// RunAsync all of the UI info.
await dispatcher.RunAsync(CoreDispatcherPriority.High,
async () =>
{
await loadingDialog.ShowAsync();
});
}
/// <summary>
/// Hides the previously opened Loading Dialog.
/// </summary>
public Task HideLoadingDialogAsync()
{
loadingDialog?.Hide();
return Task.CompletedTask;
}
在其他情况下很有趣,例如,当我从网络服务而不是数据库加载数据时,这确实没有任何问题。
UWP Loading indicator does not appear
在测试过程中,LoadingDialog
Text 属性 导致了问题,如果你想让 Text 属性 可以影响 xaml 内容,请将其作为依赖属性喜欢下面的内容。
Xaml
<Grid>
<TextBlock
x:Name="main_content"
VerticalAlignment="Center"
Text="{x:Bind Text, Mode=OneWay}" />
</Grid>
代码隐藏
public sealed partial class LoadingDialog : ContentDialog
{
public LoadingDialog()
{
this.InitializeComponent();
}
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
// Using a DependencyProperty as the backing store for Text. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(LoadingDialog), new PropertyMetadata(0));
private void ContentDialog_PrimaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
{
}
private void ContentDialog_SecondaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
{
}
}
现在您可以使用 DialogService
正确显示对话框。
private async void MainPage_Loaded(object sender, RoutedEventArgs e)
{
await dialogService.ShowLoadingDialogAsync();
await Task.Delay(3000);
await dialogService.HideLoadingDialogAsync();
}
更新
另一种猜测是加载过程非常快,导致对话框在显示之前就消失了。所以你尝试在之后手动添加任务延迟
await BalanceViewModel.UpdateBalanceCommand.ExecuteAsync();
行
我有一个 UWP 应用程序,我想在加载数据时显示一个内容对话框。
这是我的加载对话框,通过 LoadedCommand 调用。
private async Task LoadDataAsync()
{
await dialogService.ShowLoadingDialogAsync();
await LoadPaymentsAsync(new PaymentListFilterChangedMessage { TimeRangeStart = DateTime.Now.AddYears(DEFAULT_MONTH_BACK) });
//Refresh balance control with the current account
await BalanceViewModel.UpdateBalanceCommand.ExecuteAsync();
await dialogService.HideLoadingDialogAsync();
}
我还尝试通过 DispatcherHelper 调用它:
private async Task LoadDataAsync()
{
await DispatcherHelper.ExecuteOnUIThreadAsync(async () =>
{
await dialogService.ShowLoadingDialogAsync();
await LoadPaymentsAsync(new PaymentListFilterChangedMessage { TimeRangeStart = DateTime.Now.AddYears(DEFAULT_MONTH_BACK) });
//Refresh balance control with the current account
await BalanceViewModel.UpdateBalanceCommand.ExecuteAsync();
await dialogService.HideLoadingDialogAsync();
});
}
但这并没有改变任何事情。 DialogService 中的方法如下所示:
/// <summary>
/// Shows a loading Dialog.
/// </summary>
public async Task ShowLoadingDialogAsync(string? message = null)
{
// Be sure no other dialog is open.
await HideLoadingDialogAsync();
loadingDialog = new LoadingDialog { Text = message ?? Strings.LoadingLabel };
CoreApplicationView coreWindow = CoreApplication.MainView;
// Dispatcher needed to run on UI Thread
CoreDispatcher dispatcher = coreWindow.CoreWindow.Dispatcher;
// RunAsync all of the UI info.
await dispatcher.RunAsync(CoreDispatcherPriority.High,
async () =>
{
await loadingDialog.ShowAsync();
});
}
/// <summary>
/// Hides the previously opened Loading Dialog.
/// </summary>
public Task HideLoadingDialogAsync()
{
loadingDialog?.Hide();
return Task.CompletedTask;
}
在其他情况下很有趣,例如,当我从网络服务而不是数据库加载数据时,这确实没有任何问题。
UWP Loading indicator does not appear
在测试过程中,LoadingDialog
Text 属性 导致了问题,如果你想让 Text 属性 可以影响 xaml 内容,请将其作为依赖属性喜欢下面的内容。
Xaml
<Grid>
<TextBlock
x:Name="main_content"
VerticalAlignment="Center"
Text="{x:Bind Text, Mode=OneWay}" />
</Grid>
代码隐藏
public sealed partial class LoadingDialog : ContentDialog
{
public LoadingDialog()
{
this.InitializeComponent();
}
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
// Using a DependencyProperty as the backing store for Text. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(LoadingDialog), new PropertyMetadata(0));
private void ContentDialog_PrimaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
{
}
private void ContentDialog_SecondaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
{
}
}
现在您可以使用 DialogService
正确显示对话框。
private async void MainPage_Loaded(object sender, RoutedEventArgs e)
{
await dialogService.ShowLoadingDialogAsync();
await Task.Delay(3000);
await dialogService.HideLoadingDialogAsync();
}
更新
另一种猜测是加载过程非常快,导致对话框在显示之前就消失了。所以你尝试在之后手动添加任务延迟
await BalanceViewModel.UpdateBalanceCommand.ExecuteAsync();
行