作出有约束力的财产,而不是内部的公众视图模型 [英] Making binding property internal instead of public in ViewModel

查看:119
本文介绍了作出有约束力的财产,而不是内部的公众视图模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用MVVM一些不同的东西。在其中绑定到查看我们的视图模型的属性是公开的。我正在一个按钮的例子约束力。下面是一个简单的示例。

View.xaml:

 <按钮内容=测试按钮命令={结合TestButtonCommand}/>

ViewModel.cs

 私人ICommand的_testButtonCommand;
公众的ICommand TestButtonCommand
{
    {返回_testButtonCommand? (_testButtonCommand =新RelayCommand(的someMethod)); }
}

下面我的问题是,我们可以使 TestButtonCommand 内部而不是公开?内部意味着它是当前项目的访问,以便他们不应该这样做,什么问题?但是,当我试图做的,它并没有奏效。添加在吸气断点未被击中。那么,为什么我们不能让它内部的。

下面是从MSDN的链接。

http://msdn.microsoft.com/en-us/library/ ms743643.aspx

您为绑定必须是你的类的公共属性绑定源属性使用属性。明确定义的接口属性不能结合的目的被访问,也不能保障,私人的,内部的,或虚拟的不具有基本实现属性

为什么我们不能做到这一点?在访问的情况下,如果在同一个项目上工作的内部是一样的市民。那么,为什么我们不能用内部在这里。必须有一个原因,这些都应该是公开的,我期待这个原因。

 内部的ICommand TestButtonCommand {......}


解决方案

  

在访问内部的情况下,如果在相同的工作是一样的公共
  项目。那么,为什么我们不能用内部在这里。必须有一个理由
  这些应该是公开的,我期待这个原因。


您有答案你的问题,因为只有内部只能在同一组件内从外部访问,而不是。这是结合内部不起作用,因为绑定由绑定引擎,而不是由您的组装和解决的唯一原因,它在单独的程序集 presentationFramework.dll 是precise,如果你正在寻找。

I am trying some different things using MVVM. In our ViewModel properties which are bind to View are public. I am taking example of a button binding. Here is a simple sample.

View.xaml:

<Button Content="Test Button" Command="{Binding TestButtonCommand}" />

ViewModel.cs

private ICommand _testButtonCommand;
public ICommand TestButtonCommand
{
    get { return _testButtonCommand?? (_testButtonCommand= new RelayCommand(SomeMethod)); }
}

Here my question is that can we make TestButtonCommand internal instead of public? Internal means it is accessible to current project so their should not be any problem doing that? But when I tried to do that it didn't worked. Adding a breakpoint in getter was not hit. So why we cannot make it internal.

Here is the link from msdn.

http://msdn.microsoft.com/en-us/library/ms743643.aspx

The properties you use as binding source properties for a binding must be public properties of your class. Explicitly defined interface properties cannot be accessed for binding purposes, nor can protected, private, internal, or virtual properties that have no base implementation.

Why we cannot do this? In case of access internal is same as public if working in the same project. Then why we cannot use internal here. There must be a reason that these should be public, and I am looking for that reason.

internal ICommand TestButtonCommand { ...... }

解决方案

In case of access internal is same as public if working in the same project. Then why we cannot use internal here. There must be a reason that these should be public, and I am looking for that reason.

You have answer in your question only since internals can only be accessed within the same assembly and not from outside. This is the only reason binding to internals doesn't work because binding is resolved by binding engine and not by your assembly and its in separate assembly PresentationFramework.dll to be precise if you are looking for that.

这篇关于作出有约束力的财产,而不是内部的公众视图模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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