![](/img/trans.png)
[英]Visual Studio - Windows UI Library - set TargetPlatformMinVersion and TargetPlatformVersion (C#)
[英]Compile a Visual Studio C# project for two TargetPlatformVersion
我開發了一個可在Windows 7和8.1上運行的.NET Windows應用程序。 然后,我添加了Windows 8附帶的Toast通知功能(此問題來自: 如何從常規(非存儲)Win32 .NET應用程序中使用Windows.UI命名空間? )。 這也起作用,我只需要添加:
<PropertyGroup> <TargetPlatformVersion>8.0</TargetPlatformVersion> </PropertyGroup>
到項目文件。
當我從Windows 8.1 SDK C:\\Program Files (x86)\\Windows Kits\\8.1\\References\\CommonConfiguration\\Neutral\\Windows.winmd
文件時,該可執行文件不再在Windows 7上啟動! 我雙擊,僅此而已。 沒有錯誤,沒有消息。
由於我沒有在線找到任何解決方案,所以我的問題來了:我該怎么做:為用戶提供Toast功能並在Windows 7上運行相同的.exe?
先感謝您!
編輯結果表明,盡管TargetPlatformVersion設置為8.0,該可執行文件還是會在Windows 7上啟動,但是一旦程序嘗試加載Windows 8庫,它就會崩潰:
An unhandled exception of type 'System.TypeLoadException' occurred in ToastTester.exe. Additional information: Could not find Windows Runtime type 'Windows.UI.Notifications.ToastNotificationManager'.
在線Application.Run(new Form1());
在第9行的Form1.cs中,我已經using Windows.UI.Notifications;
即使可以預期該可執行文件將在Windows 7等Windows.UI.Notifications命名空間絕對不可用的環境中運行,在運行時避免此異常的最佳方法是什么?
我設計了自己的變通辦法,以便能夠支持Windows 8烤面包,同時防止在Windows 7上運行時由於缺少庫而導致應用程序崩潰。注意:我使用的是Singleton設計模式(成員INSTANCE
),但您始終可以否則。
ShellLink.cs 從這里獲取
Win8Toaster.cs:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Windows.Data.Xml.Dom;
using Windows.UI.Notifications;
namespace ToastManager
{
class Win8Toaster
{
public const string APPUSERMODELID = "YourCompany.YourApplicationName";
public static string ShortcutLocation;
public static ToastNotifier ToastNotifier;
private static Win8Toaster _INSTANCE = null;
public static Win8Toaster INSTANCE
{
get
{
if (_INSTANCE == null)
{
_INSTANCE = new Win8Toaster();
}
return _INSTANCE;
}
}
public Win8Toaster()
{
ShortcutLocation = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\Microsoft\Windows\Start Menu\Programs\YourCompany\YourApplication.lnk");
//We need a start menu shortcut (a ShellLink object) to show toasts.
if (!File.Exists(ShortcutLocation))
{
string directory = Path.GetDirectoryName(ShortcutLocation);
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
using (ShellLink shortcut = new ShellLink())
{
shortcut.TargetPath = System.Reflection.Assembly.GetEntryAssembly().Location;
shortcut.Arguments = "";
shortcut.AppUserModelID = APPUSERMODELID;
shortcut.Save(ShortcutLocation);
}
}
ToastNotifier = ToastNotificationManager.CreateToastNotifier(APPUSERMODELID);
}
public void ShowToast(ToastContent Content)
{
XmlDocument ToastContent = new XmlDocument();
ToastContent.LoadXml("<toast><visual><binding template=\"ToastImageAndText02\"><image id=\"1\" src=\"file:///" + Content.ImagePath + "\"/><text id=\"1\">" + Content.Text1 + "</text><text id=\"2\">" + Content.Text2 + "</text></binding></visual></toast>");
ToastNotification thisToast = new ToastNotification(ToastContent);
ToastNotifier.Show(thisToast);
}
}
}
Toaster.cs
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace ToastManager
{
public static class Toaster
{
private static Win8Toaster ActiveToaster;
public static bool Win8ToasterAvailable = true;
public static void ShowToast(ToastContent Content)
{
if (Win8ToasterAvailable)
{
if (ActiveToaster == null)
{
if (Environment.OSVersion.Version.Major > 6 || Environment.OSVersion.Version.Major == 6 && Environment.OSVersion.Version.Minor >= 2)
{
try
{
ActiveToaster = Win8Toaster.INSTANCE;
}
catch (Exception ex)
{
Win8ToasterAvailable = false;
}
}
else
{
Win8ToasterAvailable = false;
}
}
ActiveToaster.ShowToast(Content);
}
else
{
//Use alternative notifications because Windows 8 Toasts are not available
}
}
}
//I also wrote my own toast content structure:
public class ToastContent
{
public string ImagePath, Text1, Text2;
public ToastContent(string ImagePath, string Text1, string Text2)
{
this.ImagePath = ImagePath;
this.Text1 = Text1;
this.Text2 = Text2;
}
}
}
現在您已經擁有了必要的類,這是如何使用它(非常簡單,是吧?):
ToastManager.Toaster.ShowToast(new ToastManager.ToastContent(@"..\path\toyour\image.png", "Your Application Name", "Time: " + DateTime.Now.ToLongTimeString()));
此示例顯示帶有當前系統時間的Toast通知,如果您使用的是Windows 7,則不顯示任何內容。
設計建議:
我使用WinForms設計了一個通知窗口,該窗口看起來與Windows 8中的窗口相似,並且使用我自己的表單模擬了相同的功能。 或者,您也可以實現任務欄圖標並顯示一些通知氣泡。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.