从 Blend 中的类创建样本数据会抛出“对象引用未设置为对象的实例”
我正在尝试从 Expression Blend 中的 ViewModel 类创建一些示例数据。 但是,Expression Blend 停止并显示“对象引用未设置到对象的实例”。就我个人而言,我不明白这个异常从何而来。
有谁知道为什么会发生这种情况?
这是我的 UsersListViewModel:
[Export]
public class UserListViewModel : ViewModelBase
{
[ImportingConstructor]
public UserListViewModel(IUserListView view)
: base(view)
{
}
private ObservableCollection<UserItem> _userList;
public ObservableCollection<UserItem> UserList
{
get { return _userList; }
set
{
if (_userList != value)
{
_userList = value;
RaisePropertyChanged("UserList");
}
}
}
private UserItem _selectedUser;
public UserItem SelectedUser
{
get { return _selectedUser; }
set
{
if (_selectedUser != value)
{
_selectedUser = value;
RaisePropertyChanged("SelectedUser");
}
}
}
private string _searchText;
public string SearchText
{
get { return _searchText; }
set
{
if (_searchText != value)
{
_searchText = value;
RaisePropertyChanged("SearchText");
}
}
}
private ICommand _searchCommand;
public ICommand SearchCommand
{
get { return _searchCommand; }
set
{
if (_searchCommand != value)
_searchCommand = value;
}
}
// ... other ICommands
}
提前感谢您的所有帮助,
干杯, G。
I am trying to create some Sample Data from my ViewModel classes in Expression Blend.
However Expression Blend stops and says "Object reference not set to an instance of an object". Personally, I don't understand where this exception comes from.
Does anyone have an idea of why this is happening?
This is my UsersListViewModel:
[Export]
public class UserListViewModel : ViewModelBase
{
[ImportingConstructor]
public UserListViewModel(IUserListView view)
: base(view)
{
}
private ObservableCollection<UserItem> _userList;
public ObservableCollection<UserItem> UserList
{
get { return _userList; }
set
{
if (_userList != value)
{
_userList = value;
RaisePropertyChanged("UserList");
}
}
}
private UserItem _selectedUser;
public UserItem SelectedUser
{
get { return _selectedUser; }
set
{
if (_selectedUser != value)
{
_selectedUser = value;
RaisePropertyChanged("SelectedUser");
}
}
}
private string _searchText;
public string SearchText
{
get { return _searchText; }
set
{
if (_searchText != value)
{
_searchText = value;
RaisePropertyChanged("SearchText");
}
}
}
private ICommand _searchCommand;
public ICommand SearchCommand
{
get { return _searchCommand; }
set
{
if (_searchCommand != value)
_searchCommand = value;
}
}
// ... other ICommands
}
Thank you in advance for all your help,
Cheers,
G.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更新! Laurent(MvvmLight 作者)发布了如何调试设计时数据。 此处发布博客文章。
我在 Blend 中或在 Visual Studio 中打开 .xaml 时找到了此错误的原因和解决方案。
Blend 尝试运行您的设计时代码,如果在某处遇到空指针,这就是您收到的错误。
因此,跟踪创建设计时数据的代码。很可能您忘记初始化某些内容,或者您的类型错误。
如果设计器运行用户代码时可以捕获断点,这将很容易找到,但我认为这是不可能的。
UPDATE! Laurent (MvvmLight author) has posted how to debug design time data. Blog post here.
I found the cause and solution to this error in Blend or when opening a .xaml in Visual Studio.
Blend attempts to run your design time code and it if hits a null pointer somewhere, this is the error you get.
So, track through your code creating the design time data. Most likely you forgot to initialize something or maybe you have the wrong type.
This would be easy to find if you could have breakpoints catch when the designer is running user code, but I don't think this is possible.
当遇到这个问题时,我发现我的属性上的属性导致了此错误消息。
创建示例数据时注释掉
[ImportingConstructor]
和[Export]
(使用 Blend 编译一次项目以确保不使用旧版本)可能会成功这里。When faced with this problem, I found that the Attributes on my properties cause this error message.
Commenting out
[ImportingConstructor]
and[Export]
while creating the sample data (compile the project once with Blend to be sure not to work with the old version) might do the trick here.