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

c# - Error when trying to place a CollectionView and CarouselView in the same Grid .Net Maui 8.0 LTS on Windows - Stack Overflow

programmeradmin4浏览0评论

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
  • I would wait for the "Loaded" event before binding; or try separate "data sources" for the "different" views. Seems to be some contention when the views are different and they're both binding to the same source. A "view" is a type of query. – Gerry Schmitz Commented Jan 17 at 22:38
  • @Gerry Schmitz I tried the Loaded event to no avail, thank you very much anyways for your help. Regarding the Items, actually for my original code which has more than just a CollectionView and CarouselView I used two different ObservableCollections I just tried to make the sample short enough to focus on the main issue. Since it caused confution I am gonna change the example and add two different ObservableCollections. Thanks again. – Daniel Pinder Commented Jan 17 at 23:15
  • This might not be the solution to your problem, but, typically with layout issues, don't be afraid to use additional Grid, e.g. <Grid><Grid><CollectionView /></Grid><Grid Grid.Row="1"><CarouselView /></Grid></Grid>. Also, with both CollectionView and CarouselView, please set x:DataType on your DataTemplate 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
  • @StephenQuan It didn't work, but I really appreciate your help, and thank you for the tips. – Daniel Pinder Commented Jan 18 at 0:36
  • Have you tried setting BindingContext before calling InitializeComponent() ? – Stephen Quan Commented Jan 19 at 0:28
 |  Show 2 more comments

1 Answer 1

Reset to default 1

I couldn't reproduce the crash in .NET 9 but I did observed some quirky behavior with CarouselView. My workarounds were:

  1. I added more items to OtherItems (if I left it at 3 items, sometimes the CarouselView can get stuck in an infinite scroll)
  2. Put the Label inside a Grid (without this, sometimes the Label failed to render)

Other changes I made:

  1. Move the declaration of BindingContext to XAML (not 100% required)
  2. Declare x:DataType everywhere (enabling compiled Bindings)
  3. Drop the set; on your properties (only get; 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();
    }
}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论