开发者

Accessing Mock services in designmode in Silverlight 4

My request is similar to this.

I am using MvvmLight and the Viewmodel information shows properly for designtime and runtime. However I want to abstract it away into a Service class. So I have my Mock Service and the real Service that both Implement IService.

in the codebehind for app.xaml I am checking for designtime, then calling the method on my serviceloader depending on what the check returns.

if (IsInDesignModeStatic)
            {
                ServiceLoader.LoadDesignTimeServices开发者_高级运维();
            }
            else
            {
                ServiceLoader.LoadRunTimeServices();

            }


public sealed class ServiceLoader
    {
        private ServiceLoader()
        {
        }

        public static void LoadDesignTimeServices()
        {
            ServiceContainer.Instance.AddService<IQuestionsService>(new dt.QuestionsService());
        }

        public static void LoadRunTimeServices()
        {

            ServiceContainer.Instance.AddService<IQuestionsService>(new rt.QuestionsService());

        }
    }

This works in Runtime just fine, but not in designtime. If I actually use the designtime concrete implementation in my viewmodel:

if (IsInDesignMode)
            {

                //var s = Infrastructure.GetService<IQuestionsService>();
                var s = new ReadmissionTrackingApplication.Client.Services.DesignTime.QuestionsService();

                QuestionCollectionView_Refresh(s.getQuestions());
            }
            else
            {
                //Listens for New Questionairre request. It receives the current ReadmitPatientResult
                Messenger.Default.Register<fnReadmitPatientList_Result>(this, ReceiveNewQuestionairreRequest);

                //TODO for testing only
                ReceiveNewQuestionairreRequest(null);

            }

it shows up in Blend. What do I need to do to allow access to the mock service in blend? I think I remember reading I have to somehow add the serviceloader to my application resources similar to what is done with the viewmodels...but I dont know exactly how it needs to be done, I assume its different from how the vm is done, because I am not accessing the service in the view but from the viewmodel.


The problem is that Blend does not execute all the code. To check this out, you can attach a debugger (from Visual Studio 10 to Blend 4, making sure that you select Managed Code V4.0) and place a breakpoint in your setup code. Probably it does not get called.

To solve this, you can try and do the setup in the ViewModelLocator. Since the VML is created in the App.xaml resources, Blend is running that code. You can, for example, put the setup code in the static VML constructor.

Cheers, Laurent

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜