开发者

Create Sample Data from Class in Blend throws "Object reference not set to an instance of an object "

开发者 https://www.devze.com 2023-02-10 21:28 出处:网络
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 d

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 Us开发者_运维知识库erListViewModel : 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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消