首页 > 解决方案 > UWP System.InvalidCastException:无法转换 COM 对象

问题描述

我有一个针对 FCU 的 UWP 应用程序。从今天开始,我收到以下异常:

System.InvalidCastException: Unable to cast COM object of type 'System.__ComObject' to class type 'System.String'. Instances of types that represent COM components cannot be cast to types that do not represent COM components; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface.
   at MoneyFox.Windows.Views.AccountListView.AccountListView_obj1_Bindings.Update_CurrentBalance(Double obj, Int32 phase)
   at MoneyFox.Windows.

据我所见,一旦与 TextBlock 的类型为 double 的绑定更新,就会发生这种情况。但我没有更改该代码的任何内容。

我有最新的 VS 15.7.3。我还尝试针对不同平台和不同版本的 Microsoft.NETCore.UniversalWindowsPlatform(当前为 6.0.8)。

存储库的链接:https ://github.com/NPadrutt/MoneyFox.Windows/tree/XamarinFormsNew

那会是什么?

标签: uwp

解决方案


我发现了问题。我有一个转换器,它通过 Xamarin.Forms 实现 IValueConverter 并从 MvxValueConverter 继承,因此我可以将它用于 Xamarin Forms Bindings 以及没有 XF 的 UWP 应用程序。

public class AmountFormatConverter : MvxValueConverter, IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        // Converter Logic
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    {
        // Convert Back Logic
    }
}

我的问题是,我没有为实现的方法设置 Override 关键字。似乎导致了这个问题。

public class AmountFormatConverter : MvxValueConverter, IValueConverter
{
    public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        // Converter Logic
    }

    public override object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    {
        // Convert Back Logic
    }
}

推荐阅读