添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
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
Object
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 对 a Window . 的依赖属性公开。

Windows 7 个任务栏提供了增强的功能,使你能够使用任务栏项向用户通信状态,并在窗口最小化或隐藏时公开常见任务。 类公开 TaskbarItemInfo 的功能在早于 Windows 7 的 Windows 版本中不可用。 使用 TaskbarItemInfo 此类的应用程序仍可在早期版本的 Windows 中运行;但是,这些任务栏增强功能在早期版本中不可用。

在 Windows 7 中,某些任务栏功能可能不可用,具体取决于用户的设置。 例如,如果禁用 Windows Aero,或者应用程序以提升的权限启动,则任务栏功能不可用。 应用程序应提供其他方式来与不依赖于 Windows 7 中增强的任务栏功能的用户进行交互。

通知区域中的程序图标(位于任务栏最右侧)通常用于向用户传达应用程序状态。 默认情况下,Windows 7 个任务栏隐藏通知区域中的程序图标。 但是,可以设置属性 Overlay ,将图像添加到任务栏按钮以传达状态,例如消息应用程序中的联机状态。 覆盖图像允许用户查看应用程序状态,即使他们看不到通知区域中的程序图标。 还可以通过设置 ProgressState ProgressValue 属性来显示任务栏按钮中正在运行的任务的进度。

当你将鼠标指针移到任务栏按钮上时,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 )