mahapps ShowProgressAsync 不显示加载程序异步 - WPF (C#)
本文关键字:WPF 异步 程序 ShowProgressAsync 显示 加载 mahapps | 更新日期: 2023-09-27 18:34:50
我对mahapps有问题,显示ShowProgressAsync方法,但没有在底部显示加载颗粒。我认为渲染保持同步是一个问题。
谁能给我一个解决方案?我很难找到问题.
Xaml:
<controls:MetroWindow x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button Content="Clicca" Width="150" Click="Button_Click" Height="50"></Button>
</Grid>
</controls:MetroWindow>
代码隐藏:
using MahApps.Metro.Controls;
using MahApps.Metro.Controls.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : MetroWindow
{
ProgressDialogController x;
public MainWindow()
{
InitializeComponent();
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
x = await this.ShowProgressAsync("test","loading", false) as ProgressDialogController;
}
}
}
他们应该在流动的镜头底部出来,但仍然被阻挡
您的意思是要将进度条设置为不确定吗?如果是这种情况,您将在 ShowProgressAsync 之后立即丢失方法调用
private async void Button_Click(object sender, RoutedEventArgs e)
{
x = await this.ShowProgressAsync("test", "loading", false) as ProgressDialogController;
x.SetIndeterminate();
}
如果要显示实际进度,则应使用 x 的方法SetProgress
来报告进度,值从 0 到 1。
您的工作代码不应阻止 UI 线程,以便让 ProgressWindow 显示
var controller = await this.ShowProgressAsync("Please wait", "Doing Something...");
// Your code here
await controller.CloseAsync();
如果代码将阻止 UI 线程,请将其包装在新任务中
var controller = await this.ShowProgressAsync("Please wait", "Doing Something...")
await Task.Run(() =>
{
// Your code here
}
await controller.CloseAsync();
我一直用来结束异步操作的一些代码是这样的......
var x = await this.ShowProgressAsync("Please wait", "Doing Something...");
// Your Code Here.
await x.CloseAsync().ConfigureAwait(false);
如果您只想让它出现 1 秒钟,那么您可以使用...
var x = await this.ShowProgressAsync("Please wait", "Waiting for 1 second.");
await Task.Delay(1000);
await x.CloseAsync().ConfigureAwait(false);