如何在 Windows 通用应用程序中显示带有文本的进度环?

How to show a progress ring with a text in Windows Universal Apps?

我正在将我的 Windows Phone 8 应用程序迁移到 Windows 通用应用程序。在那里,我使用进度指示器显示一些文本,如 "Loading Please Wait..",直到我收到 Web 服务调用的响应表单服务器。现在我也想在 Windows 8.1 应用程序中实现相同的目的。在 Windows 8.1 中有 Progress Ring 控件,但其中没有 Text 属性。谁能用一些示例代码建议如何实现这一目标。我想在我的整个应用程序中使用它吗?

甚至,我以前在进度指示器中显示的文本都存储在本地存储的 json 文件中。

此外,我想使用 Dispatcher 实现此目的,而在 c# 中不使用 Xaml。

您可以创建自己的 UserControl,它将包含消息的 ProgressRing 和 TextBlock,下面是一个示例:

<UserControl
x:Class="YourNamespace.ProgressRingWithText"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:YourNamespace"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400"
x:Name="uc">

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>

    <ProgressRing IsActive="{Binding IsActive, ElementName=uc}"/>
    <TextBlock Text="{Binding Text, ElementName=uc}" HorizontalAlignment="Center"/>
</Grid>

和 C#:

public sealed partial class ProgressRingWithText : UserControl
{
    public bool IsActive
    {
        get { return (bool)GetValue(IsActiveProperty); }
        set { SetValue(IsActiveProperty, value); }
    }

    // Using a DependencyProperty as the backing store for IsActive.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty IsActiveProperty =
        DependencyProperty.Register("IsActive", typeof(bool), typeof(ProgressRingWithText), new PropertyMetadata(false));

    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(ProgressRingWithText), new PropertyMetadata("Loading..."));

    public ProgressRingWithText()
    {
        this.InitializeComponent();
    }
}

您可以在将这些属性添加到 window/page 时引用它们。

您甚至可以更进一步,使用布尔值到可见性转换器将 IsActive 属性 转换为可见性,以更改 TextBlock 的可见性。

当然,这是一个非常简单的 UI,但请尝试一下,看看是否适合您。