Continuando con la temática de mi post anterior, hoy vamos a ver otra de las funcionalidades que se pueden aplicar a la barra de tareas de Windows 7. En esta ocasión veremos como mostrar una barra de progreso sobre el icono de nuestra aplicación mínimizada. De esta manera como su propio nombre indica veremos el progreso de la misma: activa, pausada, si se produce error, si esta pensando ….
Partiendo del proyecto anterior lo que hacemos es añadir un nuevo control WPF, cuyo código de definición es el siguiente:
- <UserControl x:Class="TaskBarIcons.ProgressBar"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
- <Grid Width="300">
- <Grid.RowDefinitions>
- <RowDefinition Height="Auto" />
- <RowDefinition Height="Auto" />
- <RowDefinition Height="Auto" />
- <RowDefinition Height="Auto" />
- <RowDefinition Height="Auto" />
- <RowDefinition Height="*" />
- </Grid.RowDefinitions>
- <Rectangle Style="{DynamicResource BorderStyle}" Grid.RowSpan="4" />
- <TextBlock Text="Taskbar Progress" Margin="{DynamicResource BoxedContentMargin}" Style="{DynamicResource SectionTitle}" />
- <DockPanel Grid.Row="1" Margin="{DynamicResource BoxedContentMargin}">
- <TextBlock Text="Status" DockPanel.Dock="Left"></TextBlock>
- <ComboBox x:Name="ProgressStateSelection" Margin="5,0,0,0" HorizontalAlignment="Stretch"
- SelectionChanged="ProgressStateSelection_SelectionChanged"/>
- </DockPanel>
- <Slider x:Name="progressSlider" MaxWidth="300" Height="40" Minimum="0" Maximum="100" Grid.Row="2"
- ValueChanged="progressSlider_ValueChanged" Margin="{DynamicResource BoxedContentMargin}"/>
- <CheckBox x:Name="ShowProgressBar" Click="ShowProgressBar_Click" Grid.Row="3"
- Margin="{DynamicResource BoxedContentMargin}">
- <TextBlock Text="Show slider's value in progress bar" />
- </CheckBox>
- </Grid>
- </UserControl>
- </form>
Y cuya lógica de funcionamiento viene definida a través del siguiente código:
- using System;
- using System.Windows;
- using System.Windows.Controls;
- using Microsoft.WindowsAPICodePack.Taskbar;
- namespace TaskBarIcons
- {
- /// <summary>
- /// Interaction logic for ProgressBar.xaml
- /// </summary>
- public partial class ProgressBar : UserControl
- {
- public ProgressBar()
- {
- InitializeComponent();
- this.Loaded += ProgressBar_Loaded;
- this.Unloaded += ProgressBar_Unloaded;
- }
- private void ProgressBar_Unloaded(object sender, RoutedEventArgs e)
- {
- TaskbarManager.Instance.SetProgressState(TaskbarProgressBarState.NoProgress);
- }
- private void ProgressBar_Loaded(object sender, RoutedEventArgs e)
- {
- if (this.ProgressStateSelection.ItemsSource == null)
- {
- this.ProgressStateSelection.ItemsSource = Enum.GetValues(typeof(TaskbarProgressBarState));
- ProgressStateSelection.SelectedItem = TaskbarProgressBarState.NoProgress;
- }
- }
- private void progressSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
- {
- UpdateProgress();
- }
- //This method is part of the lab. If we are asked to show a progress bar,
- //choose the appropriate TaskbarProgressState value and use the UpdateProgress
- //method to perform the update.
- private void ShowProgressBar_Click(object sender, RoutedEventArgs e)
- {
- if (ShowProgressBar.IsChecked.Value)
- {
- ProgressStateSelection.SelectedItem = TaskbarProgressBarState.Normal;
- }
- else
- {
- ProgressStateSelection.SelectedItem = TaskbarProgressBarState.NoProgress;
- }
- UpdateProgress();
- }
- //This method is part of the lab. When the progress bar state changes,
- //synchronize it with the checkbox and use the UpdateProgress method to
- //perform the update.
- private void ProgressStateSelection_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- if ((TaskbarProgressBarState)ProgressStateSelection.SelectedItem == TaskbarProgressBarState.NoProgress)
- {
- ShowProgressBar.IsChecked = false;
- }
- else
- {
- ShowProgressBar.IsChecked = true;
- }
- UpdateProgress();
- }
- //This method is part of the lab. When asked to update the progress state and
- //we should indeed display a taskbar progress bar, use the TaskbarManager's
- //SetProgressValue and SetProgressState methods to update the progress bar state.
- private void UpdateProgress()
- {
- if (ShowProgressBar.IsChecked.Value)
- {
- TaskbarManager.Instance.SetProgressValue((int)progressSlider.Value, 100);
- TaskbarManager.Instance.SetProgressState((TaskbarProgressBarState)ProgressStateSelection.SelectedItem);
- }
- else
- {
- TaskbarManager.Instance.SetProgressState(TaskbarProgressBarState.NoProgress);
- }
- }
- }
- }
El control que hemos creado es el que se observa en la parte inferior de la imagen:
A través de él podemos seleccionar los distintos estados:
– en progreso, que se mostrará como una barra verde gradual
– error, se mostrará una barra roja gradual
– en pausa, se mostrará una barra amarilla gradual
– pensando se mostrará una barra verde intermitente
De manera que veremos los estados de nuestra aplicación de la siguiente forma:
En Progreso Error Pensando
Desde mi punto de vista, este tipo de desarrollos son muy interesantes no sólo visualmente, sino también a nivel de usabilidad.