最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Issue with selecteditem in collectionview's SelectionChangedCommand - Stack Overflow

programmeradmin2浏览0评论

I have added SelectionChangedCommand and SelectedItem commands for collectionview.

<CollectionView
        Margin="0,10,0,0"
        BackgroundColor="Transparent"
        IsVisible="{Binding Visibility}"
        ItemsSource="{Binding ImageItems}"
        ItemsLayout="VerticalGrid, 2"
        SelectionMode="Single"
        SelectionChangedCommand="{Binding ImageItemTappedCommand}"
        SelectedItem="{Binding LastImageTappedItem, Mode=TwoWay}"
        HeightRequest="{Binding ImageHeight}">
    <CollectionView.ItemTemplate>
        <DataTemplate>
            <StackLayout BackgroundColor="{Binding ImageBGColor}">

ImageItemTappedCommand:

public NameMatchViewModel() {
    ImageItemTappedCommand = new Command((obj) = >{
        try {
            //reset the bg color 
            foreach(var item in ImageItems) {
                item.ImageBGColor = Colors.White;
            }
            NameMatchList imageList = obj as NameMatchList;
            if (imageList != null) {
                Debug.WriteLine("**not null**");
            }
            else {
                Debug.WriteLine("**null**");
            }
            int index = ImageItems.IndexOf(imageList);
            imageList.ImageBGColor = Color.FromArgb("#0091da");
            //Storing name and imageurl to local db
            if (Utility.IsInternet()) {
                Preferences.Default.Set("NameMatchImageList_Image", imageList.imageUrl);
            }
            else {
                Preferences.Default.Set("NameMatchImageList_Image", imageList.FullImageUrl);
            }
            Preferences.Default.Set("NameMatchImageList_Name", imageList.name);
            Preferences.Default.Set("ImageItem", imageList);
            isImageSelected = true;
            if (isImageSelected && isNameSelected) {
                //If both image and name selected by player startes checking the matching
                StartNameMatchCheck(imageList);
            }
        }
        catch(Exception imagetapEx) {
            Debug.WriteLine("imagetapEx:>>" + imagetapEx);
        }
    });
}

But when I try to fetch the selecteditem value I am getting null. For the above code I am getting below exception:

Exception is for the below line:

imageList.ImageBGColor = Color.FromArgb("#0091da");
**null**
12:30:36:070    [0:] imagetapEx:>>System.NullReferenceException: Object reference not set to an instance of an object.
12:30:36:070       at MyProjectName.Model.NameMatchViewModel.<.ctor>b__98_0(Object obj) in E:\My Projects\MAUI\MyProjectName-app-maui\MyProjectName\Model\NameMatchViewModel.cs:line 390

How can I fetch the selected item value?

I have added SelectionChangedCommand and SelectedItem commands for collectionview.

<CollectionView
        Margin="0,10,0,0"
        BackgroundColor="Transparent"
        IsVisible="{Binding Visibility}"
        ItemsSource="{Binding ImageItems}"
        ItemsLayout="VerticalGrid, 2"
        SelectionMode="Single"
        SelectionChangedCommand="{Binding ImageItemTappedCommand}"
        SelectedItem="{Binding LastImageTappedItem, Mode=TwoWay}"
        HeightRequest="{Binding ImageHeight}">
    <CollectionView.ItemTemplate>
        <DataTemplate>
            <StackLayout BackgroundColor="{Binding ImageBGColor}">

ImageItemTappedCommand:

public NameMatchViewModel() {
    ImageItemTappedCommand = new Command((obj) = >{
        try {
            //reset the bg color 
            foreach(var item in ImageItems) {
                item.ImageBGColor = Colors.White;
            }
            NameMatchList imageList = obj as NameMatchList;
            if (imageList != null) {
                Debug.WriteLine("**not null**");
            }
            else {
                Debug.WriteLine("**null**");
            }
            int index = ImageItems.IndexOf(imageList);
            imageList.ImageBGColor = Color.FromArgb("#0091da");
            //Storing name and imageurl to local db
            if (Utility.IsInternet()) {
                Preferences.Default.Set("NameMatchImageList_Image", imageList.imageUrl);
            }
            else {
                Preferences.Default.Set("NameMatchImageList_Image", imageList.FullImageUrl);
            }
            Preferences.Default.Set("NameMatchImageList_Name", imageList.name);
            Preferences.Default.Set("ImageItem", imageList);
            isImageSelected = true;
            if (isImageSelected && isNameSelected) {
                //If both image and name selected by player startes checking the matching
                StartNameMatchCheck(imageList);
            }
        }
        catch(Exception imagetapEx) {
            Debug.WriteLine("imagetapEx:>>" + imagetapEx);
        }
    });
}

But when I try to fetch the selecteditem value I am getting null. For the above code I am getting below exception:

Exception is for the below line:

imageList.ImageBGColor = Color.FromArgb("#0091da");
**null**
12:30:36:070    [0:] imagetapEx:>>System.NullReferenceException: Object reference not set to an instance of an object.
12:30:36:070       at MyProjectName.Model.NameMatchViewModel.<.ctor>b__98_0(Object obj) in E:\My Projects\MAUI\MyProjectName-app-maui\MyProjectName\Model\NameMatchViewModel.cs:line 390

How can I fetch the selected item value?

Share Improve this question edited Jan 21 at 4:04 President James K. Polk 42k27 gold badges109 silver badges144 bronze badges asked Jan 20 at 7:06 Matthew PansMatthew Pans 7831 gold badge9 silver badges27 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

The Parameter of SelectionChangedCommand will always be null since, you haven't defined SelectionChangedCommandParameter

Either you can set SelectionChangedCommandParameter to SelectedItem or, Since you already binded your SelectedItem to LastImageTappedItem you can directly use it to set the ImageBGColor color property of selected item

Option 1: Set SelectionChangedCommandParameter

<CollectionView x:Name="myCollectionView"
    Margin="0,10,0,0"
    BackgroundColor="Transparent"
    IsVisible="{Binding Visibility}"
    ItemsSource="{Binding ImageItems}"
    ItemsLayout="VerticalGrid, 2"
    SelectionMode="Single"
    SelectionChangedCommand="{Binding ImageItemTappedCommand}"
    SelectedItem="{Binding LastImageTappedItem, Mode=TwoWay}"
    SelectionChangedCommandParameter="{Binding Source={x:Reference myCollectionView},Path=SelectedItem}"
...

Option 2: Use LastImageTappedItem

LastImageTappedItem.ImageBGColor = Color.FromArgb("#0091da");
发布评论

评论列表(0)

  1. 暂无评论