必须设置ServiceLocationProvider

v.g*_*.g. 8 .net c# xaml mvvm-light windows-phone-8.1

我正在使用MVVM Light.当我在我的资源中添加更多值转换器时,我的应用程序崩溃,但异常:

Microsoft.Practices.ServiceLocation.DLL中出现"System.InvalidOperationException"类型的异常,但未在用户代码中处理

附加信息:必须设置ServiceLocationProvider.

App.xaml.csOnLaunched活动中,我有这一行

ServiceLocator.Current.GetInstance<MyViewModel>();
Run Code Online (Sandbox Code Playgroud)

它在那里崩溃..在这个ServiceLocator中我可以看到有一个SetLocatorProvider方法,它接受ServiceLocatorProvider作为参数.我无法在Web和微软MSDN页面上找到任何内容:

protected override async void OnLaunched(LaunchActivatedEventArgs e)
    {
        Frame rootFrame = Window.Current.Content as Frame;

        if (rootFrame == null)
        {
            ...
        }

        if (rootFrame.Content == null)
        {
            ...
        }

        Window.Current.Activate();

        DispatcherHelper.Initialize();

        ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);

        ServiceLocator.Current.GetInstance<MyViewModel>();
    }
Run Code Online (Sandbox Code Playgroud)

编辑:这是完整的OnLaunched活动.放完之后

ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
Run Code Online (Sandbox Code Playgroud)

发生异常:

GalaSoft.MvvmLight.Extras.DLL中发生了类型Microsoft.Practices.ServiceLocation.ActivationException'的异常,但未在用户代码中处理

其他信息:在缓存中找不到类型:cMC.ViewModel.MyViewModel.

这是ViewModelLocator的代码

public class ViewModelLocator
{
    public ViewModelLocator()
    {
        ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);

        SimpleIoc.Default.Register<MyViewModel>();
    }

    public MyViewModel MyVM
    {
        get
        {
            return ServiceLocator.Current.GetInstance<MyViewModel>();
        }
    }

    public static void Cleanup() {}
}
Run Code Online (Sandbox Code Playgroud)

v.g*_*.g. 5

我有点想通了.

还需要注册ViewModel,这是ViewModelLocator构造函数中发生的事情,但由于某种原因,构造函数稍后执行.所以我修改了ViewModelLocator类,如下所示:

public class ViewModelLocator
{
    public ViewModelLocator()
    {

    }

    public static void SetAndReg()
    {
        ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);

        SimpleIoc.Default.Register<MyViewModel>();
    }

    public MyViewModel MyVM
    {
        get
        {
            return ServiceLocator.Current.GetInstance<MyViewModel>();
        }
    }

    public static void Cleanup() {}
}
Run Code Online (Sandbox Code Playgroud)

}

然后在App.xaml.cs中:

...OnLaunched(...)
{
...
        DispatcherHelper.Initialize();

        ViewModelLocator.SetAndReg();

        ServiceLocator.Current.GetInstance<MyViewModel>();
...
}
Run Code Online (Sandbox Code Playgroud)


Xeu*_*eun 1

您没有设置 LocationProvider (错误消息非常明显..):

您需要为 ServiceLocator 提供您选择的 IoC 容器:请参阅此使用 Unity 和适配器的示例:

static ViewModelLocator()
    {
        var container = new UnityContainer();
        ServiceLocator.SetLocatorProvider(() => new UnityServiceLocatorAdapter(container));

        container.RegisterInstance<ILoggingService>(new ConsoleLoggingService());
        container.RegisterInstance<IMessageBoxService>(new SimpleMessageBoxService());
        container.RegisterInstance<ITestSuiteService>(new TestSuiteService());
        container.RegisterInstance<IApplicationService>(new ApplicationService());
    }

    /// <summary>
    /// Gets the <see cref="BackstageAboutViewModel"/>.
    /// </summary>
    public BackstageAboutViewModel BackstageAboutViewModel
    {
        get
        {
            return ServiceLocator.Current.GetInstance<BackstageAboutViewModel>();
        }
    }
Run Code Online (Sandbox Code Playgroud)