2016-08-25 438 views
7

我想在this example的windows 10中創建簡單的Toast通知到動作中心。但我在步驟2上遇到問題:Windows.UI.Notifications缺失

using Windows.UI.Notifications; 

它缺少。但是我花了很多時間找到它並沒有得到任何結果。我真的不知道我在哪裏可以找到或至少下載它。

我試了一下:

  1. 漫長的探索後,我發現Windows.UI.dllC:\Windows\System32但是當我嘗試將其添加爲引用到項目中,我得到這個錯誤。即使在我試圖複製它,做出這個完全訪問沒有任何改變

enter image description here

  • 我試過。NET(我真的使用4.5.2),重新安裝
  • 安裝了Windows 10 SDK
  • 試圖與全球
  • 新增
  • 導入
    <PropertyGroup> 
         <TargetPlatformVersion>10.0</TargetPlatformVersion> 
    </PropertyGroup> 
    
  • 新增System.Runtime.dll參考
  • 示例代碼這可能是你沒用:

    using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Text; 
    using System.Threading.Tasks; 
    using Microsoft.Toolkit.Uwp.Notifications; 
    using Microsoft.QueryStringDotNET; 
    using Windows.UI.Notifications; 
    
    
    namespace MessagerClient.Notifications { 
        class DefaultWindowsNotification { 
    
         public static void notificationTest() { 
          string title = "Andrew sent you a picture"; 
          string content = "Check this out, Happy Canyon in Utah!"; 
          string image = "http://blogs.msdn.com/something.jpg"; 
          string logo = "ms-appdata:///local/Andrew.jpg"; 
          ToastVisual visual = new ToastVisual() { 
    
           BindingGeneric = new ToastBindingGeneric() { 
    
            Children = 
            { 
             new AdaptiveText() 
             { 
              Text = title 
             }, 
    
             new AdaptiveText() 
             { 
              Text = content 
             }, 
    
             new AdaptiveImage() 
             { 
              Source = image 
             } 
            }, 
    
            AppLogoOverride = new ToastGenericAppLogo() { 
             Source = logo, 
             HintCrop = ToastGenericAppLogoCrop.Circle 
            } 
           } 
          }; 
          Console.WriteLine("NOTIFICATION"); 
          //Can`t use because of Windows.UI library 
          ToastNotificationManager.CreateToastNotifier().Show(visual); 
         } 
        } 
    } 
    
    +0

    也許[這篇文章](http://stackoverflow.com/questions/16486460/showing-windows-8-toast-from-windows-forms-app)可以幫助你。 –

    +0

    [請閱讀**本文**以瞭解如何在桌面應用中使用UWP通知。](https://docs.microsoft.com/en-us/windows/uwp/porting/desktop-to-uwp-enhance) – Mike

    回答

    13

    你必須非常艱苦的鬥爭Visual Studio來在Winforms應用程序中使用這些UWP合約。您錯誤地用錯誤的TargetPlatformVersion錯開了腳步,很難從中恢復。完整步驟:

    使用文本編輯器編輯.csproj文件,記事本將執行此操作。插入此:

    <PropertyGroup> 
         <TargetPlatformVersion>10.0.10586</TargetPlatformVersion> 
        </PropertyGroup> 
    

    這假定您的計算機上安裝了10586 SDK版本。目前,這些版本變化很快。通過查看C:\ Program Files文件(x86)\ Windows Kits \ 10 \ Include with Explorer中的複選框,可以看到該目錄中列出的已安裝版本。

    打開Winforms項目,使用Project> Add Reference> Windows tab>勾選Windows.DataWindows.UI合同。再次添加引用並使用瀏覽選項卡選擇System.Runtime。我選擇了C:\ Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework \ .NETFramework \ v4.6.1 \ Facades中的一個。此引用顯示一個警告圖標,不知道它想說什麼,但它似乎沒有任何副作用。

    通過在窗體上放置一個按鈕來測試它,雙擊以添加Click事件處理程序。最基本的代碼:

    using Windows.UI.Notifications; 
    ... 
    
    private void button1_Click(object sender, EventArgs e) { 
        var xml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText01); 
        var text = xml.GetElementsByTagName("text"); 
        text[0].AppendChild(xml.CreateTextNode("Hello world")); 
        var toast = new ToastNotification(xml); 
        ToastNotificationManager.CreateToastNotifier("anythinggoeshere").Show(toast); 
    } 
    

    通過使用不同的ToastTemplateType添加圖像或更多行文本進行修飾。請記住,您的程序只能在Win10機器上運行。

    +0

    感謝你,幫助。 – DEADMC

    +0

    我沒有使用「項目」>「添加引用」>「Windows」選項卡>勾選Windows.Data和Windows.UI合同。之後,它的工作,該死的我非常糟糕 - – DEADMC