A sample App on .net maui with MVVM community.toolkit is running on Android simulator but not on windows machine, when i press the add button get this exception

System.AccessViolationException: 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.'

MainViewModel.cs

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Microsoft.Maui.Networking;
using System.Collections.ObjectModel;

namespace ShoppingCart.ViewModel
{
    public partial class MainViewModel : ObservableObject
    {
        IConnectivity connectivity;
        public MainViewModel(IConnectivity connectivity)
        {
            items = new ObservableCollection<string>();
            this.connectivity = connectivity;
        }

        [ObservableProperty]
        ObservableCollection<string> items;

        [ObservableProperty]
        string text;

        [RelayCommand]
        async Task Add()
        {
            if (string.IsNullOrWhiteSpace(Text))
            {
                return;
            }

            if (connectivity.NetworkAccess != NetworkAccess.Internet)
            {
                await Shell.Current.DisplayAlert("Uh oh", "No Internet", "OK");
                return;
            }

            items.Add(Text);
            // add our item
            Text = string.Empty;
        }

        [RelayCommand]
        void Delete(string s)
        {
            if (items.Contains(s))
            {
                items.Remove(s);
            }
        }

        [RelayCommand]
        async Task Tap(string s)
        {
            await Shell.Current.GoToAsync($"{nameof(DetailPage)}?Text={s}");
        }

    }
}

MainPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="ShoppingCart.MainPage"
             xmlns:viewmodel="clr-namespace:ShoppingCart.ViewModel"
             x:DataType="viewmodel:MainViewModel">

    <Grid RowDefinitions="100 , Auto, *"
          ColumnDefinitions=".75* , .25*"
          Padding="10"
          RowSpacing="10"
          ColumnSpacing="10">

        <Image Grid.ColumnSpan="2" 
               Source="shopping_cart2.png" 
               BackgroundColor="Transparent"/>

        <Entry Placeholder="Enter Product"
               Text="{Binding Text}"
               Grid.Row="1" />

        <Button Text="Add" 
                Command="{Binding AddCommand}"
                Grid.Column="1"
                Grid.Row="1" />


        <CollectionView Grid.Row="2" Grid.ColumnSpan="2" 
                        ItemsSource="{Binding Items}"
                        SelectionMode="None">
            <CollectionView.ItemTemplate>
                <DataTemplate x:DataType="{x:Type x:String}">
                    <Frame x:Name="frame" CornerRadius="10" Margin="10" Padding="0" HasShadow="False"> <!--adding round corner to SwipeView-->
                        <SwipeView>
                            <SwipeView.RightItems>
                                <SwipeItems>
                                    <SwipeItem Text="Delete" 
                                           BackgroundColor="Gray" 
                                           Command="{Binding Source={RelativeSource AncestorType={x:Type viewmodel:MainViewModel}}, Path=DeleteCommand}"
                                           CommandParameter="{Binding .}"/>
                                </SwipeItems>
                            </SwipeView.RightItems>
                            <Grid Padding="0.5">
                                <Frame>
                                    <Frame.GestureRecognizers>
                                        <TapGestureRecognizer 
                                           Command="{Binding Source={RelativeSource AncestorType={x:Type viewmodel:MainViewModel}}, Path=TapCommand}"
                                           CommandParameter="{Binding .}"/> 
                                    </Frame.GestureRecognizers>
                                    <Label Text="{Binding .}" 
                                   FontSize="20"/>
                                </Frame>
                            </Grid>
                        </SwipeView>
                    </Frame>
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>

    </Grid>   

</ContentPage>

i try cleaning up the project and rebuild but nothing

0

There are 0 best solutions below