如何在Windows Universal Apps中显示带有文本的进度响铃?

时间:2015-03-06 11:16:57

标签: c# win-universal-app

我正在将Windows Phone 8应用程序迁移到Windows Universal Apps。在那里,我使用进度指示器来显示一些文本,例如" Loading Please Wait .."直到我收到Web服务调用的响应表单服务器。现在我想在Windows 8.1 App中实现相同的目的。在Windows 8.1中有进度环控件,但是Text属性不存在。任何人都可以建议一些示例代码如何实现这一目标。我想在我的整个应用程序中使用它?

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

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

1 个答案:

答案 0 :(得分:2)

您可以创建自己的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属性转换为可见性,以便更改TextBlock的可见性。

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