欢迎来到代码驿站!

.NET代码

当前位置:首页 > 软件编程 > .NET代码

关于WPF异步MVVM等待窗体的介绍

时间:2020-12-27 16:08:32|栏目:.NET代码|点击:

需求描述
•在ViewModel中处理Model中的数据需要一定时间的等待
•ViewModel或Model在获取数据或访问同步服务时有一定延迟需要等待
•ViewModel操作View加载数据需要一段时间
解决办法
•显示一个等待UI,当数据处理完毕或服务接口返回后等待UI消失
转动齿轮控件
•参考开源实现SprocketControl :http://wpfspark.codeplex.com/
等待控件

复制代码 代码如下:

<Grid>
     <local:SprocketControl Grid.Row="0"
                            Grid.Column="0"
                            Width="100"
                            Height="100"
                            Margin="0,0,0,0"
                            HorizontalAlignment="Center"
                            VerticalAlignment="Center"
                            Background="Transparent"
                            Interval="60"
                            IsIndeterminate="True"
                            StartAngle="-90"
                            TickColor="{DynamicResource MaskForegroundColor}"
                            TickCount="16"
                            TickWidth="5" />
   </Grid>

等待效果

定义MVVM中的ViewModel的状态

复制代码 代码如下:

/// <summary>
  /// 在MVVM模式中ViewModel的状态
  /// </summary>
  [Flags]
  public enum ViewModelStatus
  {
    /// <summary>
    /// ViewModel无状态
    /// </summary>
    None = 0x0,
    /// <summary>
    /// ViewModel正在初始化
    /// </summary>
    Initializing = 0x1,
    /// <summary>
    /// ViewModel初始化完毕
    /// </summary>
    Initialized = 0x2,
    /// <summary>
    /// ViewModel正在加载
    /// </summary>
    Loading = 0x4,
    /// <summary>
    /// ViewModel加载完毕
    /// </summary>
    Loaded = 0x8,
    /// <summary>
    /// ViewModel正在保存
    /// </summary>
    Saving = 0x16,
    /// <summary>
    /// ViewModel保存完毕
    /// </summary>
    Saved = 0x32
  }

ViewModel状态转变为控件状态
复制代码 代码如下:

public class StatusToAnimationVisibilityConverter : IValueConverter
   {
     #region IValueConverter Members

     public object Convert(
       object value, Type targetType, object parameter, CultureInfo culture)
     {
       try
       {
         string status = value.ToString();

         switch (status)
         {
           case "Initializing":
           case "Loading":
           case "Saving":
             return Visibility.Visible;
           case "Loaded":
           case "Saved":
           default:
             return Visibility.Collapsed;
         }
       }
       catch (Exception)
       {
         return Visibility.Collapsed;
       }
     }

     public object ConvertBack(
       object value, Type targetType, object parameter, CultureInfo culture)
     {
       return DependencyProperty.UnsetValue;
     }

     #endregion
   }

使UserControl支持异步显示
复制代码 代码如下:

<coverters:StatusToAnimationVisibilityConverter x:Key="StatusToAnimationVisibilityConverter" />

   <Style x:Key="AsyncWorkUserControlStyle" TargetType="{x:Type UserControl}">
     <Setter Property="Template">
       <Setter.Value>
         <ControlTemplate TargetType="{x:Type UserControl}">
           <Grid>
             <ContentPresenter Panel.ZIndex="0" />
             <Grid x:Name="animationGrid"
                   Width="Auto"
                   Height="Auto"
                   HorizontalAlignment="Stretch"
                   VerticalAlignment="Stretch"
                   Panel.ZIndex="2000"
                   Visibility="{Binding Path=Status,
                                Converter={StaticResource StatusToAnimationVisibilityConverter}}">
               <Grid Width="Auto"
                     Height="Auto"
                     HorizontalAlignment="Stretch"
                     VerticalAlignment="Stretch"
                     Panel.ZIndex="0"
                     Background="{DynamicResource MaskGridBackgroundBrush}"
                     Opacity="0.2" />
               <ctrl:WaitingControl x:Name="animation" Panel.ZIndex="1" />
             </Grid>
           </Grid>
         </ControlTemplate>
       </Setter.Value>
     </Setter>
   </Style>

应用Style至UserControl
复制代码 代码如下:

<UserControl x:Class="DeviceConfiguration.Views.CameraManagementView"
              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
              xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
              d:DesignHeight="318"
              d:DesignWidth="632"
              Style="{DynamicResource AsyncWorkUserControlStyle}"
              mc:Ignorable="d">
 </UserControl>

定义基础ViewModel
复制代码 代码如下:

/// <summary>
   /// 响应式的ViewModel模型
   /// </summary>
   public abstract class ViewModelResponsive : ViewModelBase, IViewModelResponsive
   {
     #region Fields

     private ViewModelStatus _status = ViewModelStatus.None;

     #endregion

     #region ViewModel Status

     /// <summary>
     /// 刷新UI数据
     /// </summary>
     public virtual void Refresh()
     {

     }

     /// <summary>
     /// ViewModel状态
     /// </summary>
     public ViewModelStatus Status
     {
       get
       {
         return _status;
       }
       protected set
       {
         if (_status != value)
         {
           _status = value;
           RaisePropertyChanged(@"Status");
         }
       }
     }

     #endregion
   }

ViewModel应用
复制代码 代码如下:

public class CameraManagementViewModel : ViewModelResponsive
   {
     protected override void BindCommands()
     {
       RefreshCommand = new RelayCommand(() =>
       {
         Refresh();
       });
     }

     public override void Refresh()
     {
       base.Refresh();

       Status = ViewModelStatus.Initializing;
       CameraCollection.Clear();
       Model.GetCameras(GetCamerasCallback);
     }

     private void GetCamerasCallback(object sender, AsyncWorkerCallbackEventArgs<IList<Camera>> args)
     {
       CameraCollection.Clear();
       Status = ViewModelStatus.Loaded;

       if (result)
       {
         foreach (var item in (args.Data as IList<Camera>))
         {
           CameraCollection.Add(item);
         }
       }
     }
   }

上一篇:学会使用C#异常

栏    目:.NET代码

下一篇:C#判断指定文件是否是只读的方法

本文标题:关于WPF异步MVVM等待窗体的介绍

本文地址:http://www.codeinn.net/misctech/37080.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有