西西軟件園多重安全檢測(cè)下載網(wǎng)站、值得信賴的軟件下載站!
軟件
軟件
文章
搜索

首頁(yè)編程開(kāi)發(fā)C#.NET → WPF 4 開(kāi)發(fā)Windows 7 跳轉(zhuǎn)列表(JumpList)

WPF 4 開(kāi)發(fā)Windows 7 跳轉(zhuǎn)列表(JumpList)

相關(guān)軟件相關(guān)文章發(fā)表評(píng)論 來(lái)源:Gnielee時(shí)間:2010/4/3 23:37:41字體大。A-A+

作者:佚名點(diǎn)擊:291次評(píng)論:0次標(biāo)簽: WPF

Actipro WPF Studiov11.1.0541 注冊(cè)版
  • 類型:編程控件大小:58.9M語(yǔ)言:英文 評(píng)分:6.6
  • 標(biāo)簽:
立即下載

 在之前寫過(guò)的《Windows 7 任務(wù)欄開(kāi)發(fā)系列》中我們通過(guò)Visual Studio 2008 借助微軟提供的Windows API Code Pack 對(duì)應(yīng)用程序的任務(wù)欄進(jìn)行開(kāi)發(fā),即將到來(lái)的Visual Studio 2010 為我們提供了更方便的開(kāi)發(fā)方式,新版本的WPF 4 只需要通過(guò)XAML 代碼即可實(shí)現(xiàn)Windows 7 任務(wù)欄的特性。本篇將針對(duì)JumpList(跳轉(zhuǎn)列表)進(jìn)行介紹,同時(shí)體驗(yàn)下.NET Framework 4.0 的新功能。

用XAML 編寫JumpList

     在WPF 4 中開(kāi)發(fā)任務(wù)欄的方便之處就在于可以使用XAML 直接編寫相應(yīng)的功能代碼,無(wú)須再使用API 編寫繁瑣的C# 程序。首先打開(kāi)App.xaml 文件加入我們想要的JumpList 程序,其中JumpList 類為創(chuàng)建跳轉(zhuǎn)列表提供了方法,JumpTask 類可以創(chuàng)建列表中的鏈接?梢詫(duì)比一下通過(guò)API 編寫的JumpList,很明顯XAML 的方式更為簡(jiǎn)單清晰。

<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>

通過(guò)閱讀上面的程序,很容易看出我們加入了兩個(gè)應(yīng)用程序(“記事本”、“畫(huà)版”)和一個(gè)“網(wǎng)站鏈接”,其中的屬性參數(shù)使用起來(lái)也十分方便。

用C# 編寫JumpList

     上面使用XAML 方式編寫了一個(gè)簡(jiǎn)單的JumpList,當(dāng)然C# 同樣也能實(shí)現(xiàn)相同的效果。首先在MainWindow 中拖入兩個(gè)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>

     為它們分別添加點(diǎn)擊事件,其中一個(gè)是為JumpList 增加“計(jì)算器”鏈接,另一個(gè)是將所有鏈接清空。創(chuàng)建JumpList 時(shí)需要使用System.Windows.Shell 命名空間,是不是有點(diǎn)像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();
}

分別點(diǎn)擊兩個(gè)按鍵后的效果:

        

    相關(guān)評(píng)論

    閱讀本文后您有什么感想? 已有人給出評(píng)價(jià)!

    • 8 喜歡喜歡
    • 3 頂
    • 1 難過(guò)難過(guò)
    • 5 囧
    • 3 圍觀圍觀
    • 2 無(wú)聊無(wú)聊

    熱門評(píng)論

    最新評(píng)論

    發(fā)表評(píng)論 查看所有評(píng)論(0)

    昵稱:
    表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
    字?jǐn)?shù): 0/500 (您的評(píng)論需要經(jīng)過(guò)審核才能顯示)