Blend Designer Error Due to Service Locator

Blend Designer Error Due to Service Locator

I was just about to get started using Blend to layout a page and noticed that there was an error in the Results pane in Blend, stating that the ServiceLocatorProvider must be set.

image

I was pretty certain that this was something I was doing during startup but clearly that code isn’t being run correctly at design time. Turns out I don’t really care at design time since I’m going to predominantly use design time data. This means that in the ViewModelLocator constructor I can simply exit if it’s being invoked at design time. Unfortunately the usual design mode property that you can query to determine if the code is being run at design time doesn’t exist in the view model locator scope. Luckily an alternative is to simply query if the service locator provider has been set:

public ViewModelLocator()
{
    if (!ServiceLocator.IsLocationProviderSet) return;
    DataService = ServiceLocator.Current.GetInstance<IDataService>();
    SyncService = ServiceLocator.Current.GetInstance<ISyncService>();
    NavigateService = ServiceLocator.Current.GetInstance<INavigateService>();
}

Leave a Reply

Your email address will not be published. Required fields are marked *