MVVM SimpleIOC Expression Blend无法创建ViewModelLocator的实例

时间:2015-05-18 09:44:40

标签: c# wpf mvvm-light expression-blend blend

我在使用SimpleIOC设置设计数据时遇到了麻烦。

我正在使用System.Net.Http.HttpClient课程。

通过注册:SimpleIoc.Default.Register<HttpClient>();我从MVVMlight收到有关多个构造函数的错误消息。所以我搜索了一个解决方法:

SimpleIoc.Default.Register<HttpClient>(()=>new HttpClient());

它在运行时工作但不在设计模式下工作。我收到有关已注册工厂方法的错误。最后是一条消息:

A first chance exception of type 'Microsoft.Expression.Platform.InstanceBuilders.InstanceBuilderException' occurred in Microsoft.Expression.Platform.DLL

Additional information: Cannot create an instance of "ViewModelLocator".

有解决方法吗?

更新

通过制作 ViewModelLocator 构造函数static使工作正常。我不知道这是否正确。

感谢您的时间。

ViewModelLocator类:

    public class ViewModelLocator
{
    public HttpClient _httpClient;
    public const string TwitchHeader = "application/vnd.twitchtv.v3+json";
    public const string TwitchURL = "https://api.twitch.tv/kraken/";

    public static ViewModelLocator Instance
    {
        get
        {
            return Application.Current.Resources["Locator"] as ViewModelLocator;
        }
    }

    /// <summary>
    /// Initializes a new instance of the ViewModelLocator class.
    /// </summary>
    /// 
    public ViewModelLocator()
    {
        ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
        if (ViewModelBase.IsInDesignModeStatic)
        {
            // Create design time view services and models
            SimpleIoc.Default.Register<HttpClient>(() => new HttpClient()); //Throws exception
            SimpleIoc.Default.Register<IDataService, DesignDataService>();
        }
        else
        {
            // Create run time view services and models
            SimpleIoc.Default.Register<HttpClient>(() => new HttpClient(new HttpClientHandler { AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip })); //Throws exception
            _httpClient = SimpleIoc.Default.GetInstance<HttpClient>();
            _httpClient.DefaultRequestHeaders.Accept.Clear();
            _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(TwitchHeader));
            _httpClient.BaseAddress = new Uri(TwitchURL);
            SimpleIoc.Default.Register<IDataService, TwitchDataService>();
        }

        SimpleIoc.Default.Register<ITwitchChannelModel,TwitchChannelModel>();
        SimpleIoc.Default.Register<ITwitchStreamModel, TwitchStreamModel>();

        SimpleIoc.Default.Register<ITwitchStreamsModel, TwitchStreamsModel>();
        SimpleIoc.Default.Register<ITwitchChannelsModel, TwitchChannelsModel>();



        SimpleIoc.Default.Register<TwitchChannelsViewModel>();
        SimpleIoc.Default.Register<TwitchStreamsViewModel>();


        SimpleIoc.Default.Register<TwitchChannelViewModel>();
        SimpleIoc.Default.Register<TwitchStreamViewModel>();

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

    public MainViewModel Main
    {
        get
        {
            return ServiceLocator.Current.GetInstance<MainViewModel>();
        }
    }

    public TwitchChannelsViewModel ChannelsViewModel
    {
        get
        {
            return ServiceLocator.Current.GetInstance<TwitchChannelsViewModel>();
        }
    }

    public static void Cleanup()
    {
        // TODO Clear the ViewModels
    }
}

0 个答案:

没有答案