简体   繁体   中英

mvvmcross windows store collectionviewsource error

I have one issue with mvvmcross in the Windows Store (or Win RT) app, when I was trying to set ItemsSource for my ListBox (or set Source for my MyCollection , which is needed for filtering, sorting and grouping data).

When I was setting ItemsSource for my ListBox , I immediately got a null reference error in this line of code:

ListBox.ItemsSource = ViewModel.Tasklist;

I checked the ViewModel , and it equalled Null , and I don't know why?

It seems strange, because when I use in XAML this code it works fine, but I need to use collectionviewsource :

XAML (WinRTTasks.Views.FirstView.xaml):

    <ListBox ItemsSource="{Binding Tasklist}" Height="208">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Title }"  FontSize="30"></TextBlock>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

My C# code below (WinRTTasks.Views.FirstView.xaml.cs):

namespace TasksWinRT.Views
{
    public sealed partial class FirstView : MvxStorePage
    {

        public new FirstViewModel ViewModel
        {
            get { return (FirstViewModel) base.ViewModel; }
            set { base.ViewModel = value; }
        }        

        public FirstView()
        {
            //ListBox.ItemsSource = ViewModel.Tasklist; //When I am adding this line, all the error occurred

            this.InitializeComponent();
        }
    }
}

XAML (WinRTTasks.Views.FirstView.xaml):

<ListBox Name="ListBox" Height="208">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Title }"  FontSize="30"></TextBlock>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

Updated1 :

Thank you very much! It's really works for me!

But now, I'm having a new problem with XAML. If I use this code all works good and XAML update my List by RaisePropertyChange any time:

XAML (WinRTTasks.Views.FirstView.xaml):

<ListBox ItemsSource="{Binding Tasklist}" Height="208">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Title }"  FontSize="30"></TextBlock>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

Also I can update my ListBox through button, like this, but I think it's not good solution and it's not what I want recieve from using mvvmcross:

XAML (WinRTTasks.Views.FirstView.xaml.cs):

private void ApplyFilterButton2_Click(object sender, RoutedEventArgs e)
{
    ViewModel.DoApplyFilter();
    ListBox1.ItemsSource = ViewModel.Tasklist;
}

But when I was using this code I have had a problem with updating items in my ListBox, because I even can't see any items in the ListBox after start my application.

public FirstView()
{   
    this.InitializeComponent();

    this.Loaded += FirstView_Loaded;
}

void FirstView_Loaded(object sender, RoutedEventArgs e)
{            
    ListBox1.ItemsSource = ViewModel.Tasklist;            
}

Tasklist is ObservableCollection:

private ObservableCollection<TasklistViewModel> _tasklist = new ObservableCollection<TasklistViewModel>();
public ObservableCollection<TasklistViewModel> Tasklist
{
    get { return _tasklist; }
    set
    {
        _tasklist = value;
        RaisePropertyChanged(() => Tasklist);                
    }
}

Thanks in advance!

It looks like you are accessing "ListBox" before it is initialized (and possibly 'ViewModel') which would give you the null reference error. Usually you want to add a 'loaded' handler and do control initialization in there.

Try this:

public sealed partial class FirstView : MvxStorePage
{
    public new FirstViewModel ViewModel
    {
        get { return (FirstViewModel)base.ViewModel; }
        set { base.ViewModel = value; }
    }

    public FirstView()
    {
        this.InitializeComponent();

        this.Loaded += FirstView_Loaded;
    }

    void FirstView_Loaded(object sender, RoutedEventArgs e)
    {
        ListBox.ItemsSource = ViewModel.Tasklist;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM