WPF 4 開發Windows 7 捷徑清單(JumpList)

來源:互聯網
上載者:User

     在之前寫過的《Windows 7 工作列開發系列》中我們通過Visual Studio 2008 藉助微軟提供的Windows API Code Pack 對應用程式的工作列進行開發,即將到來的Visual Studio 2010 為我們提供了更方便的開發方式,新版本的WPF 4 只需要通過XAML 代碼即可實現Windows 7 工作列的特性。本篇將針對JumpList(捷徑清單)進行介紹,同時體驗下.NET Framework 4.0 的新功能。

用XAML 編寫JumpList

     在WPF 4 中開發工作單位欄的方便之處就在於可以使用XAML 直接編寫相應的功能代碼,無須再使用API 編寫繁瑣的C# 程式。首先開啟App.xaml 檔案加入我們想要的JumpList 程式,其中JumpList 類為建立捷徑清單提供了方法,JumpTask 類可以建立列表中的連結。可以對比一下通過API 編寫的JumpList,很明顯XAML 的方式更為簡單清晰。

<Application x:Class="Win7TaskbarDemo.App"             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"             StartupUri="MainWindow.xaml">    <Application.Resources>             </Application.Resources>    <JumpList.JumpList>        <JumpList ShowFrequentCategory="True"                  ShowRecentCategory="True">            <JumpTask ApplicationPath="notepad.exe"                       CustomCategory="Microsoft Tools"                       Description="Start Notepad"                       Title="Notepad"                       IconResourcePath="notepad.exe"                       IconResourceIndex="0" />                        <JumpTask ApplicationPath="mspaint.exe"                      CustomCategory="Microsoft Tools"                       Description="Start Paint"                       Title="Paint"                      IconResourcePath="mspaint.exe"                       IconResourceIndex="0" />            <JumpTask ApplicationPath="http://gnielee.cnblogs.com/"                      CustomCategory="Blog Link"                       Description="Go to {GnieTech}"                       Title="Gnie's Blog"                      IconResourcePath="C:\\Program Files\\Internet Explorer\\iexplore.exe" />        </JumpList>    </JumpList.JumpList></Application>

通過閱讀上面的程式,很容易看出我們加入了兩個應用程式(“記事本”、“畫版”)和一個“網站連結”,其中的屬性參數使用起來也十分方便。


用C# 編寫JumpList

     上面使用XAML 方式編寫了一個簡單的JumpList,當然C# 同樣也能實現相同的效果。首先在MainWindow 中拖入兩個Button:

<Window x:Class="Win7TaskbarDemo.MainWindow"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        Title="MainWindow" Height="211" Width="363" Icon="/Win7TaskbarDemo;component/Resources/App.ico">        <Grid>        <Button Content="Clear All Tasks" Height="23" HorizontalAlignment="Right" Margin="0,29,59,0"                 Name="ClearBtn" VerticalAlignment="Top" Width="89" Click="ClearBtn_Click" />        <Button Content="Add New Task" Height="23" HorizontalAlignment="Left" Margin="60,29,0,0"                 Name="AddBtn" VerticalAlignment="Top" Width="93" Click="AddBtn_Click" />    </Grid></Window>

     為它們分別添加點擊事件,其中一個是為JumpList 增加“計算機”連結,另一個是將所有連結清空。建立JumpList 時需要使用System.Windows.Shell 命名空間,是不是有點像API 中的Microsoft.WindowsAPICodePack.Shell。

private void AddBtn_Click(object sender, RoutedEventArgs e){   JumpTask jumpTask = new JumpTask();   //Create a new Calculator JumpTask   jumpTask.ApplicationPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "calc.exe");   jumpTask.IconResourcePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "calc.exe");   jumpTask.Title = "Calculator";   jumpTask.Description = "Start Calculator";   jumpTask.CustomCategory = "New Microsoft Tools";               //Add Calculator to JumpList   JumpList jumpList = JumpList.GetJumpList(App.Current);   jumpList.JumpItems.Add(jumpTask);   jumpList.Apply();}private void ClearBtn_Click(object sender, RoutedEventArgs e){   JumpList jumpList1 = JumpList.GetJumpList(App.Current);   jumpList1.JumpItems.Clear();   jumpList1.Apply();}

分別點擊兩個按鍵後的效果:

        

相關參考資料

1.Windows 7 工作列開發 之 捷徑清單(Jump Lists)

http://www.cnblogs.com/gnielee/archive/2010/03/16/windows7-taskbar-jumplists.html

2.What's New in WPF Version 4

http://msdn.microsoft.com/en-us/library/bb613588(VS.100).aspx

3.JumpList Class

http://msdn.microsoft.com/en-us/library/system.windows.shell.jumplist(v=VS.100).aspx

原始碼下載

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.