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

3
我正在将我的Windows Phone 8应用迁移到Windows通用应用程序。在此过程中,我使用了一个进度指示器来显示一些文本,例如“加载中,请稍候…”,直到我从Web服务调用中收到响应。现在,我也想在Windows 8.1应用程序中实现相同的目的。在Windows 8.1中,有一个进度环控件,但其中没有文本属性。是否有人可以提供一些示例代码来建议如何解决这个问题?而且我想在我的整个应用程序中都使用它。
甚至,我之前在进度指示器中展示的文本存储在本地存储的json文件中。
此外,我想使用Dispatcher和C#来实现这一点,而不是使用Xaml。
1个回答

3

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

当你将这些属性添加到你的窗口/页面时,你可以引用它们。

你甚至可以更进一步使用一个布尔转可见性转换器,将IsActive属性转换为Visibility,以改变TextBlock的可见性。

当然,这是一个非常简单的用户界面,但你可以尝试一下,看看它是否适合你。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接