我正在尝试使用MVVM来尝试一些不同的事情。我们ViewModel中绑定到View的属性是公共的。我以按钮绑定为例进行说明。以下是一个简单的示例。
View.xaml:
<Button Content="Test Button" Command="{Binding TestButtonCommand}" />
ViewModel.cs
private ICommand _testButtonCommand;
public ICommand TestButtonCommand
{
get { return _testButtonCommand?? (_testButtonCommand= new RelayCommand(SomeMethod)); }
}
我的问题是:我们能否将TestButtonCommand
设置为 internal 而不是 public?internal 表示它对当前项目是可访问的,因此这样做应该没有任何问题。但是当我尝试这样做时,它没有起作用。在 getter 中添加断点也没有命中。那么为什么我们不能将其设置为 internal?
以下是来自 MSDN 的链接:
http://msdn.microsoft.com/en-us/library/ms743643.aspx
您用作绑定源属性的属性必须是类的公共属性。显式定义的接口属性不能用于绑定目的,也不能使用没有基本实现的受保护、私有、内部或虚拟属性。
为什么我们不能这样做?
在访问同一项目的情况下,使用 internal 与 public 相同。那么为什么我们不能在这里使用 internal?这些属性必须是公共的,一定有原因,我正在寻找这个原因。
internal ICommand TestButtonCommand { ...... }