将现有对象实例作为构造函数参数传递给 WPF 中的 ObjectDataProvider [英] Passing existing object instances as constructor parameters to the ObjectDataProvider in WPF

查看:26
本文介绍了将现有对象实例作为构造函数参数传递给 WPF 中的 ObjectDataProvider的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将现有对象实例作为参数传递给使用 ObjectDatatProvider 创建的对象的构造函数.即使我的对象有一个带一个参数的构造函数,这总是会因给定的异常而失败:

I am trying to pass existing object instances as parameters to the constructor of an object being created using the ObjectDatatProvider. This always fails with the given exception, even though my object has a constructor that takes one parameter:

System.Windows.Data 错误:34:ObjectDataProvider 无法创建目的;类型='VegaViewModel';Error='错误的参数构造函数.MissingMethodException:'System.MissingMethodException:类型的构造函数'WpfApplication1.VegaViewModel' 不是成立.在System.RuntimeType.CreateInstanceImpl(BindingFlagsbindingAttr、Binder 绑定器、Object[]args,CultureInfo 文化,对象[]激活属性)在System.Activator.CreateInstance(类型类型,BindingFlags bindingAttr,Binder活页夹、Object[] args、CultureInfo文化,对象[]激活属性)在System.Windows.Data.ObjectDataProvider.CreateObjectInstance(Exception&e)'

System.Windows.Data Error: 34 : ObjectDataProvider cannot create object; Type='VegaViewModel'; Error='Wrong parameters for constructor.' MissingMethodException:'System.MissingMethodException: Constructor on type 'WpfApplication1.VegaViewModel' not found. at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes) at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes) at System.Windows.Data.ObjectDataProvider.CreateObjectInstance(Exception& e)'

这是我的 Xaml:

<ObjectDataProvider ObjectType="{x:Type WpfApplication1:VegaModel}" MethodName="Sample" x:Key="VegaPnlData"/>     
<ObjectDataProvider ObjectType="{x:Type WpfApplication1:VegaViewModel}" x:Key="VegaViewModel">
                <ObjectDataProvider.ConstructorParameters>
                    <ObjectDataProvider ObjectInstance="{StaticResource VegaPnlData}"/>
               </ObjectDataProvider.ConstructorParameters>
</ObjectDataProvider>

如果我使用这个 xaml,我也会得到同样的错误:

If I use this xaml I get the same error as well:

 <ObjectDataProvider ObjectType="{x:Type WpfApplication1:VegaViewModel}" x:Key="VegaViewModel">
            <ObjectDataProvider.ConstructorParameters>
                <StaticResource ResourceKey="VegaPnlData"/>
            </ObjectDataProvider.ConstructorParameters>
        </ObjectDataProvider>

这是 VegaViewModel 类的代码:

Here is the code for the VegaViewModel class:

public class VegaViewModel
{            
    public VegaViewModel(VegaModel vegaPnl)
    {
        VegaPnl = vegaPnl;           
    }

    public VegaModel VegaPnl { get; set; }            
}

推荐答案

您不能通过 ConstructorParameters 传递 ObjectDataProvider.原因为 Dr.WPF 充分说明...

You can't pass an ObjectDataProvider via the ConstructorParameters. The reason as Dr. WPF adequately states...

ObjectDataProvider (ODP) 是旨在与绑定一起使用.如果你在上下文之外使用它绑定的,你最终引用ODP 的一个实例,而不是解除引用的对象,它提供".

An ObjectDataProvider (ODP) is designed to be used with a Binding. If you use it outside of the context of a binding, you end up refering to an instance of the ODP rather than the dereferenced object that it "provides".

您试图做一些框架设计不允许的事情.

Your trying to do something the framework won't allow by design.

这篇关于将现有对象实例作为构造函数参数传递给 WPF 中的 ObjectDataProvider的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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