win10 uwp 异步进度条
本文主要讲我设计的几个进度条,还有如何使用异步控制进度条,如何使用动画做进度。 进度条可以参见:http://edi.wang/post/2016/2/25/windows⑴0-uwp-modal-progress-dialog 进度条其实异步就是使用后台变化,然后value绑定 我使用1个 “`
绑定到我们的ViewModel,1般如果后台线程操作界面是不能直接,但是我用了
```
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,() =>
{
});
代码参见:https://github.com/lindexi/UWP/tree/master/uwp/control/Progress,项目所有代码都会发出 我们使用Task异步,我们由于没有甚么耗时的,就 ViewModel
public ViewModel()
{
new Task(() =>
{
while (Value < 90)
{
Value += 10;
Task.Delay(1000).Wait();
}
}).Start();
}
public double Value
{
set
{
_value = value;
OnPropertyChanged();
}
get
{
return _value;
}
}
private double _value;
默许进度条设置最大值, 我还自己的控件,1个值从0到100的圆形的,可以看下面 ##圆形进度条 参见:http://www.cnblogs.com/ms-uap/p/4641419.html 先说怎样用我的,首先去我源代码https://github.com/lindexi/UWP,打开我的进度条文件夹,里面有View文件夹 我在View有1个控件 那末我的控件只需要指定Value就好啦,Value实际上是从0到100,如果叫别的应当好,但是我不改,如果你觉得不想要,自己改 xmlns:view="using:lindexi.uwp.control.RountProgress.View"
<view:RountProgress Value="{x:Bind Value,Mode=OneWay}"></view:RountProgress>
我来讲下怎样做 我们要知道StrokeDashArray,这个是1个数组,是循环的,也就是依此读取,知道超太长度。 首先我们需要有Thickness,宽度,StrokeDashArray的每个都是宽度的倍数 首先取第1个元素,把这个元素乘以宽度,作为显示的大小,然后取第2个元素,乘以宽度,作为不显示的大小 然后循环获得第3个……,如果不存在第3个,那末循环拿第1做第3,n=n==max?0:n+1,n就是第n个元素 1个显示1个不显示,循环 记得长度乘以是 那末我们如果有1个 假设我们宽度 3,StrokeDashArray 1,2,0.5,总长度为5,那末 第1个是大小 我们可以用第1个为1个值,然后第2个为1个比总长度还大的值,这样会让宽度显示为我们第1个的值,而其他为空,由于第2个比最大还大 我们要做1个
<Ellipse x:Name="Rount" Stroke="DeepSkyBlue" Height="100" Width="100"
StrokeThickness="3"
RenderTransformOrigin="0.5,0.5"/>
那末我们第1个值 由于我们需要算我们的宽度不是直接总长度,是总长度-宽度 第2个最好是Double.Max 我们想要1个可以用户进度,那末可以绑定1个属性,在我们控件 我们需要这个为 由于我们需要两个值,所以转换 假设我们的转换是固定的总长度,宽度,那末可使用
public object Convert(object value,Type targetType,object parameter,string language)
{
double thine = 3;
double w = 100 - thine;
double n = Math.PI * w/thine * (double)value / 100;
DoubleCollection temp = new DoubleCollection()
{
n,1000
};
return temp;
}
如果觉得固定不好,可以在我们转换写属性,然后在界面把我们的宽度给属性,然后换为我们的宽度算,这个简单 代码在https://github.com/lindexi/UWP/tree/master/uwp/control/Progress/Progress/View/RountProgress.xaml 那末进度条如果不需要进度,那末我有1些好的,例如我之前的博客有说的,还有1个简单,也是上面改,我们1个值是显示1个值是不显示,那末我们可以做
<UserControl
x:Class="lindexi.uwp.control.RountProgress.View.IndeterminateProgress"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:lindexi.uwp.control.RountProgress.View"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<UserControl.Resources>
<Style TargetType="ProgressRing">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Foreground" Value="{ThemeResource SystemControlHighlightAccentBrush}"/>
<Setter Property="IsHitTestVisible" Value="False"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="MinHeight" Value="20"/>
<Setter Property="MinWidth" Value="20"/>
<Setter Property="IsTabStop" Value="False"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ProgressRing">
<Grid x:Name="Ring" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" FlowDirection="LeftToRight" MaxWidth="{Binding TemplateSettings.MaxSideLength,RelativeSource={RelativeSource Mode=TemplatedParent}}" MaxHeight="{Binding TemplateSettings.MaxSideLength,RelativeSource={RelativeSource Mode=TemplatedParent}}" Padding="{TemplateBinding Padding}" RenderTransformOrigin=".5,.5" >
<Grid.Resources>
<Style x:Key="ProgressRingEllipseStyle" TargetType="Ellipse">
<Setter Property="Opacity" Value="0"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="VerticalAlignment" Value="Top"/>
</Style>
</Grid.Resources>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="SizeStates">
<VisualState x:Name="Large">
<Storyboard>
</Storyboard>
</VisualState>
<VisualState x:Name="Small"/>
</VisualStateGroup>
<VisualStateGroup x:Name="ActiveStates">
<VisualState x:Name="Inactive"/>
<VisualState x:Name="Active">
<Storyboard RepeatBehavior="Forever">
<DoubleAnimation Storyboard.TargetName="Rount" Storyboard.TargetProperty="Angle"
BeginTime="0:0:0" Duration="0:0:5" From="0" To="360" >
</DoubleAnimation>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Ellipse Stroke="DeepSkyBlue" Height="100" Width="100"
StrokeThickness="3"
RenderTransformOrigin="0.5,0.5"/>
<Ellipse Stroke="DeepSkyBlue" Height="200" Width="200"
StrokeThickness="3" StrokeDashArray="50 50"
RenderTransformOrigin="0.5,0.5" >
<Ellipse.RenderTransform>
<RotateTransform x:Name="Rount" Angle="0"/>
</Ellipse.RenderTransform>
</Ellipse>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
<Grid>
<ProgressRing Width="200" Height="200"
IsActive="True"></ProgressRing>
</Grid>
</UserControl>
我们使用1个简单的修改,由于我们可使用 我们使用
<VisualState x:Name="Active">
<Storyboard RepeatBehavior="Forever">
<DoubleAnimation Storyboard.TargetName="Rount" Storyboard.TargetProperty="Angle"
Duration="0:0:5" From="0" To="360" >
</DoubleAnimation>
</Storyboard>
</VisualState>
修改我们旋转,时间0:0:5,5秒,从0到360,循环 由于是修改,所以可以放在Resource
<ProgressRing Width="200" Height="200"
IsActive="True"></ProgressRing>
我觉得匀速不好,修改速度
<Storyboard TargetName="Rount">
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Margin"
BeginTime="00:00:00" EnableDependentAnimation="True"
Duration="0:0:2" >
<DiscreteObjectKeyFrame KeyTime="00:00:00" >
<DiscreteObjectKeyFrame.Value >
<Thickness>10,1,10,10</Thickness>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
<DiscreteObjectKeyFrame KeyTime="00:00:02">
<DiscreteObjectKeyFrame.Value >
<Thickness>10,200,10</Thickness>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
Rount就是我们要修改的控件,我们看到这是在2就直接修改,没有从1到200,这样其实其实不是我们直接就想从1然后两秒200 我们定义
<DoubleAnimation Storyboard.TargetName="Rount" Storyboard.TargetProperty="Y"
From="0" To="100" Duration="0:0:2"></DoubleAnimation>
我们要让我们的进度弹起来,如果不知道我说甚么,简单我有图 其实我们要让我们的元素移动,可以看林政大神的书
<local:IndeterminateProgress Margin="0,0" Width="200" Height="200" >
<local:IndeterminateProgress.RenderTransform>
<TranslateTransform x:Name="Rount" Y="10" />
</local:IndeterminateProgress.RenderTransform>
</local:IndeterminateProgress>
在动画
<DoubleAnimation Storyboard.TargetName="Rount"
Storyboard.TargetProperty="Y"
Duration="0:0:2" From="0" To="300">
<DoubleAnimation.EasingFunction>
<BounceEase Bounces="2"></BounceEase>
</DoubleAnimation.EasingFunction>
</DoubleAnimation>
我们使用Rount,x,记得要给名字,然后两秒,从0到300,下面就是弹跳,我上面有说,这个在官方有说比我写还好,但是官方的我没法拿来 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |