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

标签 c# win-universal-app

我正在将我的 Windows Phone 8 应用程序迁移到 Windows 通用应用程序。在那里,我使用进度指示器显示一些文本,如“正在加载请稍候..”,直到我收到服务器对 Web 服务调用的响应。现在我也想在 Windows 8.1 App 中实现同样的目的。在 Windows 8.1 中有 Progress Ring 控件,但在该 Text 属性中不存在。谁能用一些示例代码建议如何实现这一目标。我想在我的整个应用程序中使用它吗?

甚至,我曾经在 Progress Indicator 中显示的文本都存储在本地存储中的 json 文件中。

此外,我想在不使用 Xaml 的情况下使用 Dispatcher 来实现此目的。

最佳答案

您可以创建自己的 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();
    }
}

然后您可以在将它们添加到窗口/页面时引用这些属性。

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

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

关于c# - 如何在 Windows 通用应用程序中显示带有文本的进度环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28897663/

相关文章:

c# - 对不同行长度的文件进行二分搜索

c# - 匹配时抓取一部分文本,去掉其余部分,因为它没用

c# - 从动态数字数组中找到唯一的整数和

javascript - [WINJS][UWP] 标题栏上的返回按钮

c# - 正则表达式在 C# Azure 函数中包含一个(小写、大写、数字、给定的特殊字符)

c# - c# - 如何从与C#中的System不同的命名空间获取类型?

c# - Windows Phone 8.1 中的 BackButtonPressed 问题?

wpf - Windows 10 UWP - Windows Phone 模拟器上的空白

c# - 单击搜索图标时,SearchBox 不会在一部手机上触发 QuerySubscribed (UWP 10)

windows-phone-8 - WP8.1 Universal 与 Silverlight 8.1/Windows 8.1/更早版本中的默认按钮行为不一致