我正在将我的 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/