在 Silverlight 4 的设计模式下访问 Mock 服务
我的请求是与此类似。
我正在使用 MvvmLight,并且 Viewmodel 信息在设计时和运行时正确显示。然而我想将它抽象成一个服务类。所以我有我的模拟服务和真正的服务,它们都实现了 IService。
在 app.xaml 的代码隐藏中,我正在检查设计时间,然后根据检查返回的内容调用服务加载器上的方法。
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());
}
}
这在运行时工作得很好,但在设计时则不行。如果我实际上在我的视图模型中使用设计时具体实现:
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 中。我需要做什么才能允许访问混合中的模拟服务?我想我记得读过我必须以某种方式将服务加载器添加到我的应用程序资源中,类似于使用视图模型所做的事情......但我不确切地知道它需要如何完成,我假设它与虚拟机的完成方式不同,因为我不是在视图中访问服务,而是从视图模型访问服务。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是 Blend 并不执行所有代码。要检查这一点,您可以附加一个调试器(从 Visual Studio 10 到 Blend 4,确保选择托管代码 V4.0)并在安装代码中放置一个断点。可能它不会被调用。
要解决这个问题,您可以尝试在 ViewModelLocator 中进行设置。由于 VML 是在 App.xaml 资源中创建的,因此 Blend 正在运行该代码。例如,您可以将设置代码放入静态 VML 构造函数中。
干杯,
洛朗
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