从 Blend 中的类创建样本数据会抛出“对象引用未设置为对象的实例”

发布于 2024-10-17 02:18:44 字数 1628 浏览 6 评论 0原文

我正在尝试从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笨笨の傻瓜 2024-10-24 02:18:44

更新! 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.

Object reference not set to an instance of an object.

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.

江湖彼岸 2024-10-24 02:18:44

当遇到这个问题时,我发现我的属性上的属性导致了此错误消息。

创建示例数据时注释掉 [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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文