Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

c# - SimpleIoc.Default.Register fails at IsInDesignModeStatic if the Interface is in different assembly

In the static constructor of ViewModelLocator class the SimpleIoc.Default.Register<T>(); fails at IsInDesignModeStatic mode, if the Interface is in a different project. As a result the MainWindow.xaml designer is empty at design time.

I've made a simple solution to test it. Only changes that I've made is to move the DataItem class and the IDataService interface into the Domain project.

Download here

I've found a walk-around: Add a link to the IDataService.cs in the ClientWpf project.

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

        if (ViewModelBase.IsInDesignModeStatic) {

            // It fails if the IDataService is in different assembly
            // Delete the link of IDataService.cs from the ViewModel folder...
            SimpleIoc.Default.Register<IDataService, Design.DesignDataService>();
        }
        else {
            SimpleIoc.Default.Register<IDataService, DataService>();
        }

        SimpleIoc.Default.Register<MainViewModel>();
    }
...
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
    if (ViewModelBase.IsInDesignModeStatic) {


        // put these lines here:
        if (SimpleIoc.Default.IsRegistered<IDataService>()) {
            SimpleIoc.Default.Unregister<IDataService>();
        }

        SimpleIoc.Default.Register<IDataService, Design.DesignDataService>();
    }
    else {
        SimpleIoc.Default.Register<IDataService, DataService>();
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...