【发布时间】:2023-03-12 01:01:01
【问题描述】:
我遇到了一个有趣的问题,但我还没有找到任何解释......
鉴于下面非常简单的 MVVM WPF 应用程序,为什么只有在 ViewModel 中的可见性设置为 public 时,列表才会绑定到组合框?
将TestList 可见性更改为internal 在编译时不会引发错误或警告,但在运行时组合框会留空。
引用the official documentation:internal 类型或成员只能在同一程序集中的文件中访问。
尽管 View 和 ViewModel 是在同一个程序集中定义的,但这个问题仍然存在。
代码如下所示:
型号:
class TestModel
{
internal List<string> Musketeers { get; private set; }
public TestModel()
{
Musketeers = new List<string> { "Athos", "Porthos", "Aramis" };
}
}
查看:
<Window x:Class="TestWpfApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<ComboBox Width="250" Height="25" ItemsSource="{Binding TestList}" />
</Grid>
</Window>
视图模型:
class TestViewModel : INotifyPropertyChanged
{
TestModel myModel = new TestModel();
public List<string> TestList
{
get
{
return myModel.Musketeers;
}
}
// INotifyPropertyChanged members are below ...
}
【问题讨论】:
-
View 和 ViewModel 不是在同一个程序集中吗? 我不能回答这个问题。你在哪里定义的?
-
@ChrisDunaway 哎呀,你是对的!我现在已经修正了我的措辞......