【问题标题】:C# XAML Windows Universal Loading DialogC# XAML Windows 通用加载对话框
【发布时间】:2018-01-12 20:55:44
【问题描述】:

是否可以在 Windows 通用应用程序中创建类似于 Android 进度对话框的弹出式加载对话框?

我的应用程序的一部分可能需要一些时间才能完成,因此我想向用户展示该应用程序实际上正在运行。我想通过弹出某种遮蔽背景以停止用户交互的对话框覆盖来做到这一点。

用于比较的 Android 进度对话框:

【问题讨论】:

  • 看看 Popup 类和 ProgressRing
  • 将 ContentDialog 或 UserControl 与 ProgressRing 一起使用。
  • 将弹出窗口与 ProgressRing 结合使用

标签: c# xaml win-universal-app


【解决方案1】:

您可以创建一个UserControl,背景颜色为黑色,不透明度约为 0.6

然后,将ProgressRing 放入其中,创建一个DependencyProperty 来控制从UserControl 外部激活环。将 UserControl 放在 XAML 中或在运行时创建它并注入到页面中。

【讨论】:

    【解决方案2】:

    UWP 进度对话框

    <UserControl
    x:Class="HelperUWP.Controls.BusyIndicator"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:HelperUWP.Controls"
    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">
    <UserControl.Resources>
        <Style x:Key="LockScreenTextStyle" BasedOn="{StaticResource BaseTextBlockStyle}" TargetType="TextBlock">
            <Setter Property="FontSize" Value="18"/>
            <Setter Property="HorizontalAlignment" Value="Center"/>
            <Setter Property="Margin" Value="15,0,0,0"/>
            <Setter Property="TextAlignment" Value="Left"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
        </Style>
    </UserControl.Resources>
    <Grid>
        <Grid Background="Black" Opacity="0.4"/>
    
        <Grid x:Name="gridBackground"  HorizontalAlignment="Center" 
              VerticalAlignment="Center"
              Background="Transparent" Width="470" Height="120">
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
    
            <StackPanel Grid.Row="1" Margin="15,5,15,5" Orientation="Vertical" HorizontalAlignment="Center">
                <ProgressRing x:Name="progressRing" Height="40" Width="40" HorizontalAlignment="Center" IsActive="True" 
                             />
                <TextBlock HorizontalAlignment="Center" Foreground="White" x:Name="TitleTextBlock"
                           Style="{StaticResource LockScreenTextStyle}" Text="Performing Operation"/>
            </StackPanel>
        </Grid>
    </Grid>
    

    //cs

    public sealed partial class BusyIndicator : UserControl
    {
        private Popup ParentPopup = null;
        public BusyIndicator(string title = "Loading...", SolidColorBrush backgroundColor = null, SolidColorBrush foregroundColor = null)
        {
            this.InitializeComponent();
            TitleTextBlock.Text = title;
            this.Text = title;
            if (backgroundColor != null)
                this.gridBackground.Background = backgroundColor;
            if (foregroundColor != null)
            {
                this.progressRing.Foreground = foregroundColor;
                this.TitleTextBlock.Foreground = foregroundColor;
            } 
        }
        static SolidColorBrush def = Application.Current.Resources["SystemControlBackgroundAccentBrush"] as SolidColorBrush;
        #region Public Methods
    
        /// <summary>
        /// Closes the BusyIndicator.
        /// </summary>
        public void Close()
        {
            // Close the parent; closes the dialog too.
           // ((Popup)Parent).IsOpen = false;
           if(this.ParentPopup!= null && this.ParentPopup.IsOpen == true)
                this.ParentPopup.IsOpen = false;
        }
    
        #endregion Public Methods
    
    
        #region Public Static Methods
        DispatcherTimer timer = new DispatcherTimer() { Interval = TimeSpan.FromMilliseconds(500) };
    
        /// <summary>
        /// Locks the screen ans starts the BusyIndicator by creating a popup.
        /// </summary>
        /// <param name="title">The title to be displayed by the BusyIndicator.</param>
        /// <returns>The BusyIndicator.</returns>
        public void Start()
        {
            // Create a popup with the size of the app's window.
            Popup popup = new Popup()
            {
                Height = Window.Current.Bounds.Height,
                IsLightDismissEnabled = false,
                Width = Window.Current.Bounds.Width
            };
    
            // Create the BusyIndicator as a child, having the same size as the app.
            BusyIndicator busyIndicator = new BusyIndicator()
            {
                Height = popup.Height,
                Width = popup.Width,
                Text = this.Text,
            };
            this.timer.Start();
            this.timer.Tick += (f, y) =>
            {
                busyIndicator.Text = Text;
            };
            // Set the child of the popop
            popup.Child = busyIndicator;
    
            // Postion the popup to the upper left corner
            popup.SetValue(Canvas.LeftProperty, 0);
            popup.SetValue(Canvas.TopProperty, 0);
    
            // Open it.
            this.ParentPopup = popup;
            popup.IsOpen = true;
    
            // Return the BusyIndicator
           // return (busyIndicator);
        }
        //string
        public static readonly DependencyProperty SetMessageProperty = DependencyProperty.Register("Text", typeof(string),
            typeof(BusyIndicator), new PropertyMetadata("Loading...", new PropertyChangedCallback(OnMessageTextChanged)));
    
        public string Text
        {
            get { return (string)GetValue(SetMessageProperty); }
            set { SetValue(SetMessageProperty, value); }
        }
        private static void OnMessageTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            BusyIndicator borderControl = d as BusyIndicator;
            borderControl.OnMessageTextChanged(e);
        }
        private void OnMessageTextChanged(DependencyPropertyChangedEventArgs e)
        {
            this.TitleTextBlock.Text = e.NewValue.ToString();
        }
        //string
        public static readonly DependencyProperty SetColorProperty = DependencyProperty.Register("SetColor", typeof(SolidColorBrush),
            typeof(BusyIndicator), new PropertyMetadata(Colors.Green, new PropertyChangedCallback(OnColorTextChanged)));
    
        public SolidColorBrush SetColor
        {
            get { return (SolidColorBrush)GetValue(SetColorProperty); }
            set { SetValue(SetColorProperty, value); }
        }
        private static void OnColorTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            BusyIndicator borderControl = d as BusyIndicator;
            borderControl.OnColorTextChanged(e);
        }
        private void OnColorTextChanged(DependencyPropertyChangedEventArgs e)
        {
            this.TitleTextBlock.Foreground =(SolidColorBrush) e.NewValue;
            this.progressRing.Foreground = (SolidColorBrush)e.NewValue;
        }
    }
    

    // Result

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多