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

问题:UserDialogs Loading不显示及自定义加载进度未显示求助

解决UserDialogs Loading组件不显示及进度状态问题

我帮你一步步排查这两个加载问题,先从最常见的坑开始梳理:

一、先确认基础配置是否到位

这是最容易被忽略的前提:

  • Android端:在MainActivity的OnCreate方法里必须添加UserDialogs.Init(this);
  • iOS端:在AppDelegate的FinishedLaunching方法里添加UserDialogs.Init();
    如果没做初始化,组件根本没法正常工作。

二、UserDialogs Loading组件不显示的排查方向

1. 线程调用错误

Loading组件必须在主线程触发,要是你在后台线程直接调用,肯定不会显示。比如ViewModel里用Task.Run执行后台任务时,要切回主线程操作:

// 错误示例:后台线程直接调用
Task.Run(() => {
    UserDialogs.Instance.Loading("加载中...");
});

// 正确示例1:利用ConfigureAwait切回主线程
await Task.Run(() => {
    // 你的后台任务逻辑
}).ConfigureAwait(true);
UserDialogs.Instance.Loading("加载中...");

// 正确示例2:用MainThread调度
MainThread.BeginInvokeOnMainThread(() => {
    UserDialogs.Instance.Loading("加载中...");
});

2. 页面生命周期冲突

如果在页面初始化阶段(比如OnAppearing)立刻调用Loading,可能和页面渲染流程冲突。可以尝试延迟一点触发:

protected override async void OnAppearing()
{
    base.OnAppearing();
    await Task.Delay(100); // 给页面渲染留一点时间
    UserDialogs.Instance.Loading("加载中...");
}

三、加载进度状态不显示的正确实现示例

假设你尝试用进度更新但没生效,这里给你一套可运行的View和ViewModel示例:

ViewModel.cs示例

public class YourViewModel : BaseViewModel
{
    public ICommand StartLoadCommand { get; }

    public YourViewModel()
    {
        StartLoadCommand = new Command(async () => await ExecuteLoadWithProgress());
    }

    private async Task ExecuteLoadWithProgress()
    {
        var progressHandler = new Progress<int>(currentPercent =>
        {
            MainThread.BeginInvokeOnMainThread(() =>
            {
                // 实时更新加载进度
                UserDialogs.Instance.UpdateLoadingProgress(currentPercent / 100.0);
            });
        });

        try
        {
            // 开启带进度的Loading弹窗
            UserDialogs.Instance.Loading("正在加载,请稍候...", null, null, true);
            await Task.Run(() =>
            {
                // 模拟耗时任务,逐步上报进度
                for (int i = 0; i <= 100; i++)
                {
                    Thread.Sleep(60);
                    ((IProgress<int>)progressHandler).Report(i);
                }
            });
        }
        finally
        {
            // 不管成功失败,最后都要隐藏Loading
            UserDialogs.Instance.HideLoading();
        }
    }
}

View.cs(XAML)关联示例

确保页面绑定了对应的命令:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:vm="clr-namespace:YourApp.ViewModels"
             x:Class="YourApp.Views.YourView">
    <ContentPage.BindingContext>
        <vm:YourViewModel />
    </ContentPage.BindingContext>
    
    <StackLayout>
        <Button Command="{Binding StartLoadCommand}" Text="启动带进度的加载" />
    </StackLayout>
</ContentPage>

四、其他可能的原因

  • 版本兼容性:检查UserDialogs版本和你的Xamarin.Forms版本是否匹配,版本不兼容会导致组件失效;
  • 组件冲突:如果同时用了其他弹窗组件(比如Prism的DialogService),可能会覆盖UserDialogs的Loading;
  • 提前隐藏:确认代码里有没有在任务完成前就调用HideLoading,导致Loading一闪而过。

要是还是没解决,可以把你的View和ViewModel里的相关代码贴出来,我再帮你精准定位!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 07:53:03