2013-10-25 52 views
1

我在VS 2012中爲Windows 8商店應用程序創建了一個類庫項目(dll),但現在Windows 8.1引入了一些可用於操作系統的新API(例如,廣告ID唯一標識一個用戶),我想在我的dll中使用,但我不想發佈針對Windows 8.1的單獨的dll。我的目標是分發一個可以在Windows 8和Windows 8.1商店應用中引用的單個dll。如果我將創建一個針對8.1的dll,那麼8.0應用程序將無法使用我的dll。針對Windows 8和Windows 8.1的單個DLL

有沒有什麼辦法來檢查在運行時使用的Windows 8.1的應用程序特定的API或任何預處理,使我的DLL識別操作系統在運行時執行的代碼如

string deviceId=string.Empty; 

#if W8.1 
deviceId=Windows.System.UserProfile.AdvertisingManager.AdvertisingId; 
#endif 

或者請提出任何其他方式這樣我就只能向用戶分發一個dll文件了?

+0

您可以使用[版本幫助器API](http://msdn.microsoft.com/en-us/library/windows/desktop/dn424972%28v=vs.85%29.aspx) –

+0

您可以執行它通過反思,假設你允許在Windows應用程序中反射? (但我看不出爲什麼)按名稱訪問類和屬性 – Rup

+0

我試圖找到使用Type的API可用性,但類型tp = Type.GetType(「Windows.System.UserProfile.AdvertisingManager」); '返回null。但是當我使用類型tp1 = typeof(Windows.System.UserProfile.AdvertisingManager);'它返回我的類,但它會給我編譯VS2012錯誤,因爲這個api不可用 –

回答

2

反射最後做。 AdvertisingManager API在Windows 8中不可用,但如果應用程序在Windows 8.1上運行,相同的dll(目標框架是Windows 8)將通過反射訪問AdvertisingManager。所以,不需要爲不同版本分發兩個dll。

 Type tp = Type.GetType("Windows.System.UserProfile.AdvertisingManager, Windows.System, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime"); 
     if (tp != null) 
     { 
      PropertyInfo properties = tp.GetRuntimeProperty("AdvertisingId"); 
      if (properties != null) 
      { 
       string deviceId = (string)properties.GetValue(null); 
      } 
     } 

輸出

案例1:Windows 8應用在Windows 8

運行在這種情況下,TP將返回null作爲AdvertisingManager API不可用在Windows 8 。

案例2:Windows 8應用程序在Windo上運行ws 8.1

由於AdvertisingManager API在Windows 8.1中可用,所有針對Windows 8的應用程序都可以訪問此API並在此情況下獲得AdvertisingId。

案例3:Windows 8.1中的應用程序在Windows 8.1

運行的API可以直接在Windows 8.1的應用程序。所以,不需要經過反射路徑。

相關問題