UWP - InvalidCastException when accessing ProductListing.CurrencyCode

时间秒杀一切 提交于 2019-12-10 23:10:10

问题


I'm facing a really weird issue. I need to store the CurrencyCode property of a ProductListing item in an WSA_10_0 environment.

Here is the code

foreach (ProductListing product in listings.ProductListings.Values)
{
    Debug.Log("CURRENCY_CODE/"+product.CurrencyCode);
}

When trying to execute the code I get the following error

Additional information: Unable to cast object of type 'Windows.ApplicationModel.Store.ProductListing' to type 'Windows.ApplicationModel.Store.IProductListing2'.

Now, this is really weird because as this documentation page states ProductListing objects inherit from IProductListing2.

The entire exception content is here

System.InvalidCastException occurred
  HResult=-2147467262
  Message=Unable to cast object of type 'Windows.ApplicationModel.Store.ProductListing' to type 'Windows.ApplicationModel.Store.IProductListing2'.
  Source=mscorlib
  StackTrace:
       at System.StubHelpers.StubHelpers.GetCOMIPFromRCW_WinRT(Object objSrc, IntPtr pCPCMD, IntPtr& ppTarget)
       at Windows.ApplicationModel.Store.ProductListing.get_CurrencyCode()
       at CI.WSANative.IAPStore.WSANativeStore.<GetProductListingsAsync>d__5.MoveNext()
  InnerException: 

UPDATE:

I've tried accessing the CurrencyCode property of the listings object, which is of type ListingInformation and despite it is supposed to have that property, basing on the class documentation, it gives a similar error.

Additional information: Unable to cast object of type 'Windows.ApplicationModel.Store.ListingInformation' to type 'Windows.ApplicationModel.Store.IListingInformation2'.


回答1:


CurrencyCode property, along with IListingInformation2 interface were introduced on Windows version 10586. If you build your application against 10586 or newer SDK, you will be able to use it in your code, but if you run the application on a machine with Windows version 10240, CLR will still attempt to call that method by first doing a query interface on your Windows Runtime object for the interface that property getter is on (in CurrencyCode case, it's IListingInformation2), but the query interface call will fail, and CLR will throw an InvalidCastException.



来源:https://stackoverflow.com/questions/43678462/uwp-invalidcastexception-when-accessing-productlisting-currencycode

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!