You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET MAUI 8.0 LTS Windows下Grid共存CollectionView与CarouselView报错

.NET MAUI 8.0 LTS Windows:CollectionView与CarouselView同Grid内存访问异常解决方案

问题描述

在Windows平台的.NET MAUI 8.0 LTS项目中,将CollectionView与CarouselView放置在同一个Grid容器内时,触发System.AccessViolationException异常:

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

但同一页面放置两个CollectionView或两个CarouselView时可正常运行。

复现代码

MainPage.xaml

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/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>

MainPage.xaml.cs

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;
    }
}

解决方案

方案1:嵌套额外布局容器隔离视图

给其中一个视图外层嵌套ContentView或ScrollView,隔离两个视图的渲染上下文:

<!--修改CollectionView部分-->
<ContentView Grid.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>
</ContentView>

方案2:调整CollectionView的Windows渲染模式

在代码后台显式设置CollectionView的原生渲染模式,避免内存冲突:

public MainPage()
{
    InitializeComponent();
#if WINDOWS
    // 针对Windows平台设置静态渲染模式
    Microsoft.Maui.Handlers.CollectionViewHandler.Mapper.AppendToMapping("StaticRender", (handler, view) =>
    {
        handler.PlatformView.SetRenderMode(Microsoft.UI.Xaml.Controls.ListViewRenderMode.Static);
    });
#endif
    this.BindingContext = this;
}

方案3:更新到MAUI 8.0最新补丁版本

该异常属于.NET MAUI 8.0的已知bug,官方已在后续补丁版本中修复。通过NuGet包管理器将Microsoft.Maui.Controls及相关依赖更新至8.0.10或更高版本,即可解决问题。

内容的提问来源于stack exchange,提问作者Daniel Pinder

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 22:03:14