public ref class TaskbarItemInfo sealed : System::Windows::Freezable
public sealed class TaskbarItemInfo : System.Windows.Freezable
type TaskbarItemInfo = class
inherit Freezable
Public NotInheritable Class TaskbarItemInfo
Inherits Freezable
TaskbarItemInfo
以下示例演示如何在标记中创建
TaskbarItemInfo
。 包含
TaskbarItemInfo
对象的集合
ThumbButtonInfo
,这些对象提供对任务栏项中“播放”和“停止”命令的访问权限。
<Window.TaskbarItemInfo>
<TaskbarItemInfo x:Name="taskBarItemInfo1"
Overlay="{StaticResource ResourceKey=StopImage}"
ThumbnailClipMargin="80,0,80,140"
Description="Taskbar Item Info Sample">
<TaskbarItemInfo.ThumbButtonInfos>
<ThumbButtonInfoCollection>
<ThumbButtonInfo
DismissWhenClicked="False"
Command="MediaCommands.Play"
CommandTarget="{Binding ElementName=btnPlay}"
Description="Play"
ImageSource="{StaticResource ResourceKey=PlayImage}"/>
<ThumbButtonInfo
DismissWhenClicked="True"
Command="MediaCommands.Stop"
CommandTarget="{Binding ElementName=btnStop}"
Description="Stop"
ImageSource="{StaticResource ResourceKey=StopImage}"/>
</ThumbButtonInfoCollection>
</TaskbarItemInfo.ThumbButtonInfos>
</TaskbarItemInfo>
</Window.TaskbarItemInfo>
以下标记和代码在其完整上下文中显示了上一个示例。 应用程序使用 从
BackgroundWorker
0 到 100 进行计数,并在用户界面中显示其进度。 可以从任务栏预览启动和停止任务。 进度显示在任务栏按钮中。
<Window x:Class="Shell_TaskbarItemSample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="300" Width="300">
<Window.Resources>
<DrawingImage x:Key="PlayImage">
<DrawingImage.Drawing>
<DrawingGroup>
<DrawingGroup.Children>
<GeometryDrawing Brush="Green" Geometry="F1 M 50,25L 0,0L 0,50L 50,25 Z "/>
</DrawingGroup.Children>
</DrawingGroup>
</DrawingImage.Drawing>
</DrawingImage>
<DrawingImage x:Key="StopImage">
<DrawingImage.Drawing>
<DrawingGroup>
<DrawingGroup.Children>
<GeometryDrawing Brush="Gray" Geometry="F1 M 0,0L 50,0L 50,50L 0,50L 0,0 Z "/>
</DrawingGroup.Children>
</DrawingGroup>
</DrawingImage.Drawing>
</DrawingImage>
</Window.Resources>
<Window.CommandBindings>
<CommandBinding Command="MediaCommands.Play"
Executed="StartCommand_Executed"
CanExecute="StartCommand_CanExecute"/>
<CommandBinding Command="MediaCommands.Stop"
Executed="StopCommand_Executed"
CanExecute="StopCommand_CanExecute"/>
</Window.CommandBindings>
<Window.TaskbarItemInfo>
<TaskbarItemInfo x:Name="taskBarItemInfo1"
Overlay="{StaticResource ResourceKey=StopImage}"
ThumbnailClipMargin="80,0,80,140"
Description="Taskbar Item Info Sample">
<TaskbarItemInfo.ThumbButtonInfos>
<ThumbButtonInfoCollection>
<ThumbButtonInfo
DismissWhenClicked="False"
Command="MediaCommands.Play"
CommandTarget="{Binding ElementName=btnPlay}"
Description="Play"
ImageSource="{StaticResource ResourceKey=PlayImage}"/>
<ThumbButtonInfo
DismissWhenClicked="True"
Command="MediaCommands.Stop"
CommandTarget="{Binding ElementName=btnStop}"
Description="Stop"
ImageSource="{StaticResource ResourceKey=StopImage}"/>
</ThumbButtonInfoCollection>
</TaskbarItemInfo.ThumbButtonInfos>
</TaskbarItemInfo>
</Window.TaskbarItemInfo>
<StackPanel>
<TextBlock x:Name="tbCount" FontSize="72" HorizontalAlignment="Center"/>
<StackPanel Orientation="Horizontal">
<Button x:Name="btnPlay" Content="Play" Command="MediaCommands.Play" />
<Button x:Name="btnStop" Content="Stop" Command="MediaCommands.Stop" />
</StackPanel>
</StackPanel>
</Grid>
</Window>
// MainWindow.xaml.cs
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shell;
namespace Shell_TaskbarItemSample
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
private BackgroundWorker _backgroundWorker = new BackgroundWorker();
public MainWindow()
InitializeComponent();
// Set up the BackgroundWorker.
this._backgroundWorker.WorkerReportsProgress = true;
this._backgroundWorker.WorkerSupportsCancellation = true;
this._backgroundWorker.DoWork += new DoWorkEventHandler(bw_DoWork);
this._backgroundWorker.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
this._backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
private void StartCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
e.CanExecute = true;
e.Handled = true;
private void StartCommand_Executed(object sender, ExecutedRoutedEventArgs e)
if (this._backgroundWorker.IsBusy == false)
this._backgroundWorker.RunWorkerAsync();
// When the task is started, change the ProgressState and Overlay
// of the taskbar item to indicate an active task.
this.taskBarItemInfo1.ProgressState = TaskbarItemProgressState.Normal;
this.taskBarItemInfo1.Overlay = (DrawingImage)this.FindResource("PlayImage");
e.Handled = true;
private void StopCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
e.CanExecute = this._backgroundWorker.WorkerSupportsCancellation;
e.Handled = true;
private void StopCommand_Executed(object sender, ExecutedRoutedEventArgs e)
this._backgroundWorker.CancelAsync();
e.Handled = true;
void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
// When the task ends, change the ProgressState and Overlay
// of the taskbar item to indicate a stopped task.
if (e.Cancelled == true)
// The task was stopped by the user. Show the progress indicator
// in the paused state.
this.taskBarItemInfo1.ProgressState = TaskbarItemProgressState.Paused;
else if (e.Error != null)
// The task ended with an error. Show the progress indicator
// in the error state.
this.taskBarItemInfo1.ProgressState = TaskbarItemProgressState.Error;
// The task completed normally. Remove the progress indicator.
this.taskBarItemInfo1.ProgressState = TaskbarItemProgressState.None;
// In all cases, show the 'Stopped' overlay.
this.taskBarItemInfo1.Overlay = (DrawingImage)this.FindResource("StopImage");
void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
this.tbCount.Text = e.ProgressPercentage.ToString();
// Update the value of the task bar progress indicator.
this.taskBarItemInfo1.ProgressValue = (double)e.ProgressPercentage / 100;
void bw_DoWork(object sender, DoWorkEventArgs e)
BackgroundWorker _worker = sender as BackgroundWorker;
if (_worker != null)
for (int i = 1; i <= 100; i++)
if (_worker.CancellationPending == true)
e.Cancel = true;
break;
System.Threading.Thread.Sleep(25);
_worker.ReportProgress(i);
' MainWindow.xaml.vb
Imports System.ComponentModel
Imports System.Windows.Shell
Class MainWindow
Private _backgroundWorker As New BackgroundWorker
Public Sub New()
InitializeComponent()
' Set up the BackgroundWorker
Me._backgroundWorker.WorkerReportsProgress = True
Me._backgroundWorker.WorkerSupportsCancellation = True
AddHandler Me._backgroundWorker.DoWork, AddressOf bw_DoWork
AddHandler Me._backgroundWorker.ProgressChanged, AddressOf bw_ProgressChanged
AddHandler Me._backgroundWorker.RunWorkerCompleted, AddressOf bw_RunWorkerCompleted
End Sub
Private Sub StartCommand_CanExecute(ByVal sender As System.Object, ByVal e As System.Windows.Input.CanExecuteRoutedEventArgs)
e.CanExecute = True
e.Handled = True
End Sub
Private Sub StartCommand_Executed(ByVal sender As System.Object, ByVal e As System.Windows.Input.ExecutedRoutedEventArgs)
If Me._backgroundWorker.IsBusy = False Then
Me._backgroundWorker.RunWorkerAsync()
' When the task is started, change the ProgressState and Overlay
' of the taskbar item to indicate an active task.
Me.taskBarItemInfo1.ProgressState = Shell.TaskbarItemProgressState.Normal
Me.taskBarItemInfo1.Overlay = Me.FindResource("PlayImage")
End If
e.Handled = True
End Sub
Private Sub StopCommand_CanExecute(ByVal sender As System.Object, ByVal e As System.Windows.Input.CanExecuteRoutedEventArgs)
e.CanExecute = Me._backgroundWorker.WorkerSupportsCancellation
e.Handled = True
End Sub
Private Sub StopCommand_Executed(ByVal sender As System.Object, ByVal e As System.Windows.Input.ExecutedRoutedEventArgs)
Me._backgroundWorker.CancelAsync()
e.Handled = True
End Sub
Private Sub bw_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs)
' When the task ends, change the ProgressState and Overlay
' of the taskbar item to indicate a stopped task.
If e.Cancelled = True Then
' The task was stopped by the user. Show the progress indicator
' in the paused state.
Me.taskBarItemInfo1.ProgressState = TaskbarItemProgressState.Paused
ElseIf e.Error IsNot Nothing Then
' The task ended with an error. Show the progress indicator
' in the error state.
Me.taskBarItemInfo1.ProgressState = TaskbarItemProgressState.Error
' The task completed normally. Remove the progress indicator.
Me.taskBarItemInfo1.ProgressState = TaskbarItemProgressState.None
' In all cases, show the 'Stopped' overlay.
Me.taskBarItemInfo1.Overlay = Me.FindResource("StopImage")
End If
End Sub
Private Sub bw_ProgressChanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs)
Me.tbCount.Text = e.ProgressPercentage.ToString()
' Update the value of the task bar progress indicator.
Me.taskBarItemInfo1.ProgressValue = e.ProgressPercentage / 100
End Sub
Private Sub bw_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
Dim _worker As BackgroundWorker = CType(sender, BackgroundWorker)
If _worker IsNot Nothing Then
For i As Integer = 1 To 100 Step 1
If _worker.CancellationPending = True Then
e.Cancel = True
Return
System.Threading.Thread.Sleep(25)
_worker.ReportProgress(i)
End If
End If
End Sub
End Class
类
TaskbarItemInfo
为 Windows 7 任务栏功能提供托管包装器。 有关 Windows shell 和本机任务栏 API 的详细信息,请参阅
任务栏扩展
。
TaskbarItemInfo
作为
Window.TaskbarItemInfo
上的
Window
依赖属性公开。
Windows 7 任务栏提供增强功能,使你可以使用任务栏项向用户传达状态,并在窗口最小化或隐藏时公开常见任务。 类公开
TaskbarItemInfo
的功能在早于 Windows 7 的 Windows 版本中不可用。 使用
TaskbarItemInfo
类的应用程序仍然可以在早期版本的 Windows 中运行;但是,这些任务栏增强功能在早期版本中不可用。
在 Windows 7 中,某些任务栏功能可能不可用,具体取决于用户的设置。 例如,如果 Windows Aero 处于禁用状态,或者应用程序是使用提升的权限启动的,则任务栏功能不可用。 应用程序应提供其他方式来与不依赖于 Windows 7 中增强的任务栏功能的用户进行交互。
通知区域中的程序图标(位于任务栏最右侧)通常用于向用户传达应用程序状态。 默认情况下,Windows 7 任务栏隐藏通知区域中的程序图标。 但是,可以设置
Overlay
属性以将图像添加到任务栏按钮以传达状态,例如消息应用程序中的联机状态。 覆盖图像允许用户查看应用程序状态,即使他们看不到通知区域中的程序图标。 还可以通过设置 和
ProgressValue
属性,在任务栏按钮中显示正在运行的任务的
ProgressState
进度。
将鼠标指针移到任务栏按钮上时,Windows 7 任务栏会显示应用程序的缩略图。 默认情况下,将显示整个应用程序窗口。 可以通过设置
ThumbnailClipMargin
属性指定要显示在缩略图中的窗口的特定部分。 还可以指定
Description
显示在任务栏缩略图上方的工具提示中的 。 即使由于用户设置而看不到缩略图,也会显示工具提示。
可以将按钮添加到任务栏缩略图,以提供对常见任务的访问权限,而无需切换到应用程序窗口。 例如,窗口媒体播放器提供“播放”、“暂停”、“前进”和“后退”按钮,使你可以在应用程序最小化时从任务栏缩略图控制媒体播放。 任务栏缩略图中的按钮由
ThumbButtonInfo
对象表示,并包含在集合中
ThumbButtonInfos
。
下图显示了 Windows 7 任务栏的增强功能。
Windows 任务栏增强功能
对指定依赖属性的值进行强制。 通过对调用方
DependencyObject
上存在的依赖属性的属性元数据中所指定的任何
CoerceValueCallback
函数进行调用来完成此操作。
(继承自
DependencyObject
)
重写
OnPropertyChanged(DependencyPropertyChangedEventArgs)
的
DependencyObject
实现,以同时调用任何响应类型
Freezable
不断变化的依赖属性的
Changed
处理程序。
(继承自
Freezable
)
引发
Freezable
的
Changed
事件并调用其
OnChanged()
方法。 从
Freezable
派生的类应在修改的类成员不存储为依赖属性的任何 API 的末尾调用此方法。
(继承自
Freezable
)
验证
Freezable
是否未被冻结,并且是否正在从有效的线程上下文中访问它。
Freezable
的继承项应当在任何 API 一开始写入不属于依赖项属性的数据成员时调用此方法。
(继承自
Freezable
)