在Silverlight 4中以设计模式访问模拟服务

在Silverlight 4中以设计模式访问模拟服务

问题描述:

我的请求是similar to this在Silverlight 4中以设计模式访问模拟服务

我正在使用MvvmLight,并且Viewmodel信息适合于设计时和运行时显示。但是我想把它抽象成一个Service类。所以我有我的模拟服务和实现IService的真正服务。

在app.xaml的代码隐藏中我正在检查designtime,然后根据检查返回的内容调用我的serviceloader上的方法。

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()); 

     } 
    } 

这在运行时工作得很好,但不是在设计时。如果我真的在我的视图模型中使用designtime具体实现:

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); 

      } 

它在Blend中显示。我需要做什么才能在混合中访问模拟服务?我想我记得阅读我必须以某种方式将serviceloader添加到我的应用程序资源中,类似于对viewmodels所做的操作......但我不知道如何完成它,我认为它与vm的完成方式不同,因为我不是在视图中访问服务,而是从视图模型访问服务。

问题是Blend不能执行所有的代码。为了检查这一点,你可以附加一个调试器(从Visual Studio 10到Blend 4,确保你选择托管代码V4.0),并在你的设置代码中放置一个断点。可能它不会被调用。

要解决这个问题,您可以尝试在ViewModelLocator中进行设置。由于VML是在App.xaml资源中创建的,因此Blend正在运行该代码。例如,您可以将设置代码放入静态VML构造函数中。

干杯, 洛朗

+0

我能得到它的工作通过改变方式的ServiceContainer作品。之前,当它只被调用一次时,不需要检查它是否已经运行,我现在通过LoadDesigntime服务清除它本身,如果collectionCount> 0 ...我然后将serviceloader.LoadDesigntimeservices添加到设计时调用ctor的实际viewmodel ..我也停止调用dispatcherhelper.initialize。现在似乎正在以这种方式工作。 – ecathell 2011-03-12 22:59:01