如何在MAUI安卓应用中禁用自动ActivityIndicator
解决MAUI中Android平台IsBusy自动生成ActivityIndicator重复问题
要解决Android上默认指示器和自定义指示器重复的问题,核心思路是禁用Android平台MAUI自动生成的IsBusy指示器,同时保留自定义的ActivityIndicator在双平台生效,具体有两种实现方式:
方式一:单页面禁用默认指示器
在需要自定义指示器的XAML页面中,添加Android平台专属属性,直接关闭当前页面的默认IsBusy指示器:
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:android="clr-namespace:Microsoft.Maui.Controls.PlatformConfiguration.AndroidSpecific;assembly=Microsoft.Maui.Controls" <!-- 禁用Android默认的IsBusy指示器 --> android:Page.IsBusyIndicatorVisible="False" x:Class="YourAppNamespace.YourPage"> <!-- 页面原有布局 --> <Grid> <!-- 你的自定义ActivityIndicator,绑定IsBusy --> <ActivityIndicator IsRunning="{Binding IsBusy}" IsVisible="{Binding IsBusy}" HorizontalOptions="Center" VerticalOptions="Center" Color="#2196F3" /> <!-- 其他页面元素 --> </Grid> </ContentPage>
方式二:全局禁用默认指示器
如果你的应用所有页面都要使用自定义指示器,可以在App启动时全局关闭Android的默认IsBusy指示器:
using Microsoft.Maui.Controls.PlatformConfiguration; using Microsoft.Maui.Controls.PlatformConfiguration.AndroidSpecific; namespace YourAppNamespace; public partial class App : Application { public App() { InitializeComponent(); MainPage = new AppShell(); // 全局关闭Android平台所有页面的默认IsBusy指示器 Current.On<Android>().SetIsBusyIndicatorVisible(false); } }
效果验证
设置完成后:
- Android平台:当IsBusy为true时,只会显示你手动添加的自定义指示器,不会出现默认的顶部加载条
- iOS平台:正常显示自定义的ActivityIndicator,解决了原本iOS不显示的问题
内容的提问来源于stack exchange,提问作者David López
相关产品推荐
相关产品推荐

