在WPF设计时无法使用MVVM进行数据绑定 - ViewModel属性永远不会被调用 [英] Can't databind at design time in WPF using MVVM - ViewModel property never gets called

查看:188
本文介绍了在WPF设计时无法使用MVVM进行数据绑定 - ViewModel属性永远不会被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我把头发拉过来,所以任何帮助都会非常感谢!



我正在使用MVVM模式构建一个WPF应用程序。



为了在设计时获取数据,我正在使用Ninject依赖注入框架与服务定位器(非常类似于 http://jonas.follesoe.no/YouCardRevisitedImplementingDependencyInjectionInSilverlight.aspx



(因为我正在使用WPF而不是Silverlight我检查设计时间属性略有不同,但否则我认为它是适用的)。



我可以不要在设计时工作,无论我做什么,绑定似乎都没有被调用,它在运行时工作正常。



这里是代码对于我的Ninject模块:

  public class ViewModelModule:标准模块
{
public override void Load()
{
bool isRuntime =!ViewModelBase.IsInDesignMode;

if(isRuntime)
{
绑定< IViewModel>()。到< MyViewModel>();
}
else
{
绑定< IViewModel>()。到< MyDesignTimeViewModel>();
}
}
}

MyDesignTimeViewModel 是一个简单的CLR对象,返回硬编码的数据,代替 MyViewModel 上的所有相同属性。



服务定位器如下:

  public class ViewModelLocator 
{
private static内核内核;

static ViewModelLocator()
{
if(kernel == null)
{
kernel = new StandardKernel(new ViewModelModule());
}
}

public IViewModel ViewModel
{
get
{
var vm = kernel.Get< IViewModel> );
return vm;
}
}
}

XAML绑定DataContext的页面如下(虽然我已经尝试了几种不同的方式来声明它,都具有相同的结果):

  < Page.DataContext> 
< Binding Source ={StaticResource viewModelLocator}Path =ViewModel/>
< /Page.DataContext>

(在该文件顶部合并的ResourceDictionary中声明了ViewModelLocator) / p>

正如我所说,这在运行时工作正常,即使我在运行时将Ninject绑定切换为使用MyDesignTimeViewModel,它也可以成功显示虚拟数据。我在一个绑定中有一个虚拟转换器,以查看传递的内容,并且在运行时被调用,而不是在设计时被调用(我一直在疯狂地调试一个使用单独的Visual Studio进程的设计时间实例,如根据MSDN建议!)



在设计时,Ninject绑定以及内核实例化。然后viewModel被调用,它返回一个DesignTimeViewModel,以及我所有的硬编码数据。但是实际绑定到viewmodel上的任何属性似乎都没有被调用(虚拟转换器断点从不被击中)。



我真的看不到我做错了任何方向的任何指针都将不胜感激,因为在这个阶段我只是很困惑。谢谢:)

解决方案

这是几件事要尝试:


  1. 当您正在调试设计器时,请检查是否有任何警告/错误。

  2. 尝试使用最新版本的Snoop ,它允许您附加到Visual Studio。

  3. 尝试调试绑定。也许有趣的事情出来。

不要忘了检查DataContexts :)。



希望至少有所帮助。



干杯


Ok, I'm pulling my hair out over this, so any help will be hugely appreciated!

I'm building a WPF application using the MVVM pattern.

In an attempt to get data in at design time I am using the Ninject dependency injection framework in conjunction with a service locator (much like the example in an article at http://jonas.follesoe.no/YouCardRevisitedImplementingDependencyInjectionInSilverlight.aspx

(as I'm using WPF and not Silverlight I check for design time properties slightly differently but otherwise I believe it is applicable).

I can't get it to work at design time, no matter what I do the binding doesn't seem to even get called. It works fine at runtime.

Here's the code for my Ninject module:

public class ViewModelModule : StandardModule
{
    public override void Load()
    {
        bool isRuntime = !ViewModelBase.IsInDesignMode;

        if (isRuntime)
        {
            Bind<IViewModel>().To<MyViewModel>();                
        }
        else
        {
            Bind<IViewModel>().To<MyDesignTimeViewModel>();
        }
    }
}

MyDesignTimeViewModel is a plain CLR object that returns hardcoded data in place of all the same properties on MyViewModel.

The Service Locator is as follows:

 public class ViewModelLocator
 {
    private static IKernel kernel;

    static ViewModelLocator()
    {
        if (kernel == null)
        {
            kernel = new StandardKernel(new ViewModelModule());
        }
    }      

    public IViewModel ViewModel
    {
        get
        {
            var vm = kernel.Get<IViewModel>();
            return vm;
        }
    }
}

And the XAML binds the DataContext of the page as follows (though I've tried a few different ways of declaring it, all with the same result):

<Page.DataContext>
    <Binding Source="{StaticResource viewModelLocator}" Path="ViewModel" />        
</Page.DataContext>

(where the viewModelLocator is declared in a ResourceDictionary that is merged at the top of this file).

As I said this works fine at runtime, even if I switch my Ninject binding to use MyDesignTimeViewModel at runtime it successfully displays the dummy data. I've got a dummy converter in one of my bindings to see what gets passed through and this gets called at runtime but not at design time (I've been frantically debugging a design time instance using a seperate Visual Studio process all day, as per the MSDN recommendations!)

At design time the Ninject binding goes ahead, along with the kernel instantiation. Then the viewModel gets called for and it returns a DesignTimeViewModel, along with all my hardcoded data. But the actual binding to any of the properties on the viewmodel never seem to get called (the dummy converter breakpoint never gets hit).

I really can't see what I'm doing wrong. Any pointers in any direction would be greatly appreciated as at this stage I'm just baffled. Thanks :)

解决方案

Here are couple things to try:

  1. While you are debugging designer, check whether you have any warnings/errors.
  2. Try to snoop your designer with latest version of Snoop, which allows you to attach to the Visual Studio.
  3. Try to debug bindings. Maybe something interesting comes out.

And don't forget to check DataContexts :).

Hope at least something would help.

Cheers

这篇关于在WPF设计时无法使用MVVM进行数据绑定 - ViewModel属性永远不会被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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