To reproduce the error here is the code for MainPage.xaml:
<ContentPage xmlns=";
xmlns:x=";
x:Class="MauiApp1.MainPage">
<Grid Margin="20,10"
RowDefinitions="*,*">
<!--ROW 0-->
<CollectionView ItemsSource="{Binding Items}">
<CollectionView.ItemsLayout>
<LinearItemsLayout ItemSpacing="20" Orientation="Horizontal" />
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
<DataTemplate>
<Border HeightRequest="40" WidthRequest="40">
<Label Text="{Binding .}" HorizontalOptions="Center" VerticalOptions="Center"/>
</Border>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
<!--ROW 1-->
<CarouselView Grid.Row="1"
ItemsSource="{Binding OtherItems}">
<CarouselView.ItemTemplate>
<DataTemplate>
<Border HeightRequest="40" WidthRequest="40">
<Label Text="{Binding .}" HorizontalOptions="Center" VerticalOptions="Center"/>
</Border>
</DataTemplate>
</CarouselView.ItemTemplate>
</CarouselView>
</Grid>
</ContentPage>
Code for MainPage.xaml.cs file
public partial class MainPage : ContentPage
{
public ObservableCollection<string> Items { get; set; } = ["A", "B", "C"];
public ObservableCollection<string> OtherItems { get; set; } = ["D", "E", "F"];
public MainPage()
{
InitializeComponent();
this.BindingContext = this;
}
}
Here is the error when debugging: System.AccessViolationException: 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.'
I tried to place a CollectionView and CarouselView in the same page but it does not work. I don't know why this is happening may be It's just that those views cannot be placed together. Does anyone know if there is any workaround for this.
But what's interesting is that if you put two CarouselViews in the same page or two CollectionViews it works just fine.
To reproduce the error here is the code for MainPage.xaml:
<ContentPage xmlns="http://schemas.microsoft/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft/winfx/2009/xaml"
x:Class="MauiApp1.MainPage">
<Grid Margin="20,10"
RowDefinitions="*,*">
<!--ROW 0-->
<CollectionView ItemsSource="{Binding Items}">
<CollectionView.ItemsLayout>
<LinearItemsLayout ItemSpacing="20" Orientation="Horizontal" />
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
<DataTemplate>
<Border HeightRequest="40" WidthRequest="40">
<Label Text="{Binding .}" HorizontalOptions="Center" VerticalOptions="Center"/>
</Border>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
<!--ROW 1-->
<CarouselView Grid.Row="1"
ItemsSource="{Binding OtherItems}">
<CarouselView.ItemTemplate>
<DataTemplate>
<Border HeightRequest="40" WidthRequest="40">
<Label Text="{Binding .}" HorizontalOptions="Center" VerticalOptions="Center"/>
</Border>
</DataTemplate>
</CarouselView.ItemTemplate>
</CarouselView>
</Grid>
</ContentPage>
Code for MainPage.xaml.cs file
public partial class MainPage : ContentPage
{
public ObservableCollection<string> Items { get; set; } = ["A", "B", "C"];
public ObservableCollection<string> OtherItems { get; set; } = ["D", "E", "F"];
public MainPage()
{
InitializeComponent();
this.BindingContext = this;
}
}
Here is the error when debugging: System.AccessViolationException: 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.'
I tried to place a CollectionView and CarouselView in the same page but it does not work. I don't know why this is happening may be It's just that those views cannot be placed together. Does anyone know if there is any workaround for this.
But what's interesting is that if you put two CarouselViews in the same page or two CollectionViews it works just fine.
Share Improve this question edited Jan 20 at 3:16 Daniel Pinder asked Jan 17 at 21:56 Daniel PinderDaniel Pinder 214 bronze badges 7 | Show 2 more comments1 Answer
Reset to default 1I couldn't reproduce the crash in .NET 9 but I did observed some quirky behavior with CarouselView. My workarounds were:
- I added more items to OtherItems (if I left it at 3 items, sometimes the CarouselView can get stuck in an infinite scroll)
- Put the Label inside a Grid (without this, sometimes the Label failed to render)
Other changes I made:
- Move the declaration of BindingContext to XAML (not 100% required)
- Declare x:DataType everywhere (enabling compiled Bindings)
- Drop the
set;
on your properties (onlyget;
is needed since the collection is defined/initialized once and never changes)
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
x:Class="MauiApp1.MainPage"
xmlns="http://schemas.microsoft/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft/winfx/2009/xaml"
xmlns:local="clr-namespace:MauiApp1"
x:Name="this"
x:DataType="local:MainPage"
BindingContext="{Reference this}">
<Grid Margin="20,10" RowDefinitions="*,300">
<!-- ROW 0 -->
<CollectionView ItemsSource="{Binding Items}">
<CollectionView.ItemsLayout>
<LinearItemsLayout ItemSpacing="20" Orientation="Horizontal" />
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="x:String">
<Border HeightRequest="40" WidthRequest="40">
<Label
HorizontalOptions="Center"
Text="{Binding .}"
VerticalOptions="Center" />
</Border>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
<!-- ROW 1 -->
<CarouselView Grid.Row="1" ItemsSource="{Binding OtherItems}">
<CarouselView.ItemTemplate>
<DataTemplate x:DataType="x:String">
<Border HeightRequest="40" WidthRequest="40">
<Grid>
<Label
HorizontalOptions="Center"
Text="{Binding .}"
VerticalOptions="Center" />
</Grid>
</Border>
</DataTemplate>
</CarouselView.ItemTemplate>
</CarouselView>
</Grid>
</ContentPage>
using System.Collections.ObjectModel;
namespace MauiApp1;
public partial class MainPage : ContentPage
{
public ObservableCollection<string> Items { get; } = ["A", "B", "C"];
public ObservableCollection<string> OtherItems { get; } = ["D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O"];
public MainPage()
{
InitializeComponent();
}
}
<Grid><Grid><CollectionView /></Grid><Grid Grid.Row="1"><CarouselView /></Grid></Grid>
. Also, with both CollectionView and CarouselView, please setx:DataType
on yourDataTemplate
component. We've seen too many issues where if the compiled-bindings aren't being configured it can lead to unexpected runtime Binding behavior particularly on the Release builds. – Stephen Quan Commented Jan 17 at 23:26