在 App.xaml 中注册过多的值转换器.cs会导致崩溃



我在 App.xaml.cs 中添加了我的值转换器,但是当我再添加一个时,它会在应用程序启动时给我一个我不知道如何处理的异常。它在应用程序.cs中:

例外情况:

#if DEBUG && !DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION
        UnhandledException += (sender, e) =>
        {
            if (global::System.Diagnostics.Debugger.IsAttached) global::System.Diagnostics.Debugger.Break();
        };
#endif

这是我在 App.xaml 中的转换器.cs

<Application x:Class="cMC.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="using:myApp">
  <Application.Resources>
    <ResourceDictionary>
     <ResourceDictionary.MergedDictionaries>
       <ResourceDictionary Source="/Assets/ResourceDictionaries/ResrcDict1.xaml" />
       <ResourceDictionary Source="/DataTemplates/DataTemplates.xaml" />
     </ResourceDictionary.MergedDictionaries>
        <vm:ViewModelLocator x:Key="Locator" xmlns:vm="using:myApp.ViewModel" />
        <ic:InverseBooleanConverter x:Key="InverseBoolToVisibility" xmlns:ic="using:myApp.Helpers"/>
        <mc:SegmentPartWrapper x:Key="SegmentPartWrapper" xmlns:mc="using:myApp.ViewModel"/>
        <mc:SegmentDateCNVRTR x:Key="SegmentDateCNVRTR" xmlns:mc="using:myApp.ViewModel"/>
        <mc:RailIconBooleanConverter x:Key="RailIconBooleanConverter" xmlns:mc="using:myApp.ViewModel"/>
        <mc:CarIconBooleanConverter x:Key="CarIconBooleanConverter" xmlns:mc="using:myApp.ViewModel"/>
    </ResourceDictionary>

这会导致问题"CarIconBooleanConverter",但它在ViewModel中的代码与"RailIconBooleanConverter

"完全相同。
<mc:CarIconBooleanConverter x:Key="CarIconBooleanConverter" xmlns:mc="using:myApp.ViewModel"/>

编辑:

我从 App.xaml 中删除了其他转换器之一.cs并且"CarIconBooleanConverter"工作正常。是因为转换器太多吗?

您的 MVVM Light 类 ViewModelLocator 似乎存在问题。您可能没有正确设置它。或者您尚未声明默认的 ServiceLocationProvider,如异常中所述。

    static ViewModelLocator()
    {
        ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
    }

查看它是否在您的视图模型定位器类中。

最新更新