需求描述
•在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"
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);
}
}
}
}