时间:2021-05-19
需求描述
•在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);
}
}
}
}
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
在没给大家讲解wpfmwm示例之前先给大家简单说下MVVM理论知识:WPF技术的主要特点是数据驱动UI,所以在使用WPF技术开发的过程中是以数据为核心的,WPF
WPF实现窗体中的悬浮按钮,按钮可拖动,吸附停靠在窗体边缘。控件XAML代码:控件cs代码:usingSystem;usingSystem.Collection
一个好的WPF窗体应用没有一个好看的、炫酷的、背景就是一个失败的程序软件,而小编在这里就教大家一种更换VisualStudio2015WPF窗体程序背景图片的方
1、关于高并发的几个重要概念1.1同步和异步首先这里说的同步和异步是指函数/方法调用方面。很明显,同步调用会等待方法的返回,异步调用会瞬间返回,但是异步调用瞬间
使用WPF创建一个窗体(Window)时,如果设置了固定的高度(Height)和宽度(Width),一旦用户的电脑分辨率过低,就会使得窗体及其中的内容无法完