WPF开发中遇到的新知识 -- 9

加载页面

目的:在打开某个视图的时候,可能需要获取数据,而获取数据的时间一般会慢一点,所以应该提供一些反馈给用户,表示这个视图正在加载,而不是已经加载完成没有数据,重点是需要反馈,让用户知道软件正在运作

方法:在加载数据的开始,弹出对话框,这个对话框就是一个 ProgressBar ,然后开始加载数据,加载数据完成之后,通过事件聚合器发布消息,指出数据已经加载完成,而我们的对话框需要在构建时就注册了这个事件的回调,用来关闭对话框

对话框视图:

<UserControl x:Class="KrDesktop.Views.Dialogs.LoadingDialog"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:prism="http://prismlibrary.com/"
             mc:Ignorable="d" 
             Height="50" Width="100">
    <prism:Dialog.WindowStyle>
        <Style TargetType="Window">
            <Setter Property="prism:Dialog.WindowStartupLocation" Value="CenterScreen" />
            <Setter Property="ResizeMode" Value="NoResize"/>
            <Setter Property="ShowInTaskbar" Value="False"/>
            <Setter Property="SizeToContent" Value="WidthAndHeight"/>
            <Setter Property="WindowStyle" Value="None" />
        </Style>
    </prism:Dialog.WindowStyle>

    <Grid>
        <ProgressBar IsIndeterminate="True" Orientation="Horizontal"/>
    </Grid>
</UserControl>

对话框ViewModle:

public LoadingDialogViewModel(IEventAggregator eventAggregator)
{
    eventAggregator.GetEvent<LoadingEvent>().Subscribe(isClose =>
    {
        if (isClose)
        {
            RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
        }
    });
}

需要加载数据的视图的ViewModel:

async void LoadData()
{
    _dialogService.Show("LoadingDialog");

    var dtos = await Task.Run(async () =>
    {
        return (await _httpClientFactory.GetAllActionRule())?.Data?
            .GroupBy(a => a.RuleId,
                (key, list) =>
                {
                    var first = list.First();
                    var remark = first.Remark;
                    var ruleId = first.RuleId;
                    var ruleDto = new ActionRuleDto { Id = ruleId, Name = remark };
                    ruleDto.ActionRules = list.Select(a => new ActionRule()
                    {
                        Index = a.SIndex ?? 0,
                        ActionArgs = a.Action,
                        Line = new LineModel
                        {
                            LineId = a.LineId,
                            StartPoint = a.StartPoint,
                            EndPoint = a.EndPoint
                        }
                    }).ToList();
                    return ruleDto;
                }).ToList();
    });

    DataList.Clear();
    DataList.AddRange(dtos);
    _eventAggregator.GetEvent<LoadingEvent>().Publish(true);
}

需要注意的是,对话框的弹出,需要是Show方法,而不是ShowDialog,因为ShowDialog方法会导致UI跳到新的对话框中,而不会继续执行ShowDialog后面的代码,也就会导致一直显示加载中,但是数据根本不会获取,因为根本就没执行获取数据的代码

不过实际开发上,感觉每次都需要这样做的话,会有模板代码在,如果在后端中会使用AOP来解决这种问题,不过没试过在WPF中应用AOP,下次试试

posted @   huang1993  阅读(54)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示