d:具有接口类型的DesignInstance [英] d:DesignInstance with an interface type

查看:160
本文介绍了d:具有接口类型的DesignInstance的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将一个UI绑定到一个接口(由多个演示者实现,无法从UI程序集访问)。

I'm binding an UI to an interface (which is implemented by several presenters, not accessible from the UI assembly).

我真的很喜欢d:DesignInstance

I really like d:DesignInstance in designer because it (kind of) makes xaml strongly typed using R#.

不幸的是,d:DesignInstance不支持接口类型:无法创建接口实例。

Sadly, d:DesignInstance does not support interface types: "Cannot create an instance of an interface."

我想的第一件事是:没问题,让我们创建一个自定义标记扩展,该扩展以System.Type作为参数,并且ProvideValue方法返回其伪造实例(虚拟)。

The first thing I thought is: Ok, no problem, let's create a custom markup extension which takes a System.Type as parameter, and which ProvideValue method returns a fake instance of it (dummy implementation of this interface, generated by dynamic IL emission).

此方法效果很好,绑定在设计时已解决(我可以在设计面板中看到,因为标记扩展用lorem-ipsum填充对象属性)

This works quite well, bindings are resolved at design-time (I can see that in the design panel since my markup extension fills the object properties with a lorem-ipsum)

BUT 最好的R#功能不起作用:R esharper无法识别datacontext类型,仅给出一条消息无法在类型'object'的数据上下文中解析属性'{0}'

BUT the nicest R# feature dont work: Resharper does not recognize datacontext type, and just gives a message "Cannot resolve property '{0}' in data context of type 'object'"

有人知道如何解决此问题吗?

Does someone know how to fix this ?

(任何允许我让R#知道接口数据上下文类型的方法都很好)

(any alternative which would allow me to let R# know about an interface datacontext type would be great)

谢谢!

ps: 我也试图创建另一个标记扩展,该扩展返回生成的运行时类型,以提供给它到DesignInstance: {d:DesignInstance Type = {utilsUi:InstanceType commons:User}} =>给出错误不能将类型'InstanceType'的对象转换为类型'System.Type'
...看来DesignInstance不支持内部标记扩展:(

ps: I also tried to create another markup extension which returns the generated runtime type in order to give it to DesignInstance: "{d:DesignInstance Type={utilsUi:InstanceType commons:User}}" => Gives the error "Object of type 'InstanceType' cannot be converted to type 'System.Type'" ... seems that DesignInstance does not support inner markup extensions :(

推荐答案

我刚刚研究了或多或少的同一个问题……实际上,我要做的是遵循以下原则f MvvMLight 。准确地讲,我使用了ViewModelLocator(或多或少是静态的),以便在运行时或设计时注入正确的 ViewModel。魔术在于MvvMLight框架提供的函数 ViewModelBase.IsInDesignModeStatic 。最后,我的 ViewModelLocator 类看起来像

I have just investigated more or less the same question... Actually, what I did is to follow the principles of MvvMLight. Precisely, I used a ViewModelLocator (which will be more or less static) so that the "right" ViewModel is injected at runtime or at design time. The magic lies in the function ViewModelBase.IsInDesignModeStatic provided by the MvvMLight framework. Finally, my ViewModelLocator class looks like

public class ViewModelLocator
    {
        private static readonly IKernel _kernel;

        static ViewModelLocator()
        {
            _kernel = new StandardKernel();
            if (ViewModelBase.IsInDesignModeStatic)
            {
                 _kernel.Bind<IBasicVM>().To<DesignBasicVm>();
            }
            else
            {
                _kernel.Bind<IBasicVM>().To<BasicVm>();
            }
        }

        public IBasicVM BasicVm { get { return _kernel.Get<IBasicVM>(); } }
    }

您可以忽略 Ninject _kernel ,但如果要使用IoC构建ViewModel,则可能需要它(或类似的Ioc)。

You may ignore the Ninject _kernel but you may need it (or a similar Ioc) if you are building your ViewModels with an IoC.

App.xaml将 ViewModelLocator 声明为资源

The App.xaml declares the ViewModelLocator as a ressource

  <Application.Resources>
    <ResourceDictionary>
      <viewModel:ViewModelLocator x:Key="ViewModelLocator" />
    </ResourceDictionary>
  </Application.Resources>

MainWindow.DataContext 属性绑定到 BasicVM ViewModelLocator 的成员。 Text属性绑定到接口 IBasicVM GetContent 成员,该成员被R#(对于V2012至少为R#7.1)静态识别。

The MainWindow.DataContext property is bound to the BasicVM member of the ViewModelLocator. The Text property is bound the the GetContent member of the interface IBasicVM which is recognized statically by R# (at least R# 7.1 with VS2012)

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        DataContext="{Binding BasicVm, Source={StaticResource ViewModelLocator}}"
        >
    <Grid>
        <TextBlock  Text="{Binding GetContent}"/>
    </Grid>
</Window>

您可以检查此存储库,我已将其创建为模板。

You may check this repository which I have created as a template.

这篇关于d:具有接口类型的DesignInstance的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆