实例介绍
【实例简介】WPF 模拟wifi和手机信号
【实例截图】
【核心代码】
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;
using Microsoft.Expression.Shapes;
namespace SignalLight
{
/// <summary>
/// 按照步骤 1a 或 1b 操作,然后执行步骤 2 以在 XAML 文件中使用此自定义控件。
///
/// 步骤 1a) 在当前项目中存在的 XAML 文件中使用该自定义控件。
/// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根
/// 元素中:
///
/// xmlns:MyNamespace="clr-namespace:SignalLight.Themes"
///
///
/// 步骤 1b) 在其他项目中存在的 XAML 文件中使用该自定义控件。
/// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根
/// 元素中:
///
/// xmlns:MyNamespace="clr-namespace:SignalLight.Themes;assembly=SignalLight.Themes"
///
/// 您还需要添加一个从 XAML 文件所在的项目到此项目的项目引用,
/// 并重新生成以避免编译错误:
///
/// 在解决方案资源管理器中右击目标项目,然后依次单击
/// “添加引用”->“项目”->[浏览查找并选择此项目]
///
///
/// 步骤 2)
/// 继续操作并在 XAML 文件中使用控件。
///
/// <MyNamespace:ArcSignalLight/>
///
/// </summary>
public class ArcSignalLight : Control
{
private Arc arc_0;
private Arc arc_75;
private Arc arc_50;
private Arc arc_100;
private Storyboard storyboard;
private List<Tuple<double, Arc>> arclist;
private const double opacity = 0.3;
private const int durationMseconds = 500;
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("Value", typeof(double), typeof(ArcSignalLight), new PropertyMetadata(default(double), new PropertyChangedCallback(ValueChangedCallback)));
public static readonly DependencyProperty IsShowStoryboradProperty =
DependencyProperty.Register("IsShowStoryborad", typeof(bool), typeof(ArcSignalLight), new PropertyMetadata(default(bool), new PropertyChangedCallback(ValueChangedCallback)));
public double Value
{
get { return (double)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
public bool IsShowStoryborad
{
get { return (bool)GetValue(IsShowStoryboradProperty); }
set { SetValue(IsShowStoryboradProperty, value); }
}
static ArcSignalLight()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(ArcSignalLight), new FrameworkPropertyMetadata(typeof(ArcSignalLight)));
}
private static void ValueChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
ArcSignalLight arcSignalLight = d as ArcSignalLight;
arcSignalLight.ShowChanged();
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
arc_0 = Template.FindName("PART_0", this) as Arc;
arc_75 = Template.FindName("PART_75", this) as Arc;
arc_50 = Template.FindName("PART_50", this) as Arc;
arc_100 = Template.FindName("PART_100", this) as Arc;
if (arc_0 != null)
arclist = new List<Tuple<double, Arc>>()
{
new Tuple<double, Arc>(0,arc_0),
new Tuple<double, Arc>(25,arc_50),
new Tuple<double, Arc>(50,arc_75),
new Tuple<double, Arc>(75,arc_100),
};
}
private void CreateStoryBoard()
{
storyboard = new Storyboard();
storyboard.Name = "ArcMovingStoryBoard";
for (int i = 0; i < arclist.Count; i )
{
var item = arclist[i];
if (item.Item1 < Value)
{
var defaultAnimation = CreateDefaultAnimation(0, durationMseconds, item.Item2);
var doubleAnimation = CreateAnimation(i 1, durationMseconds, item.Item2);
storyboard.Children.Add(defaultAnimation);
storyboard.Children.Add(doubleAnimation);
}
else
{
break;
}
}
storyboard.RepeatBehavior = RepeatBehavior.Forever;
}
private DoubleAnimation CreateDefaultAnimation(int index, int interval, Arc arc)
{
DoubleAnimation doubleAnimation = new DoubleAnimation();
doubleAnimation.BeginTime = TimeSpan.FromMilliseconds(index * interval);
doubleAnimation.To = 0.3;
doubleAnimation.Duration = TimeSpan.FromMilliseconds(interval);
Storyboard.SetTarget(doubleAnimation, arc);
Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath(UIElement.OpacityProperty));
return doubleAnimation;
}
private DoubleAnimation CreateAnimation(int index, int interval, Arc arc)
{
DoubleAnimation doubleAnimation = new DoubleAnimation();
doubleAnimation.BeginTime = TimeSpan.FromMilliseconds(index * interval);
doubleAnimation.To = 1;
doubleAnimation.Duration = TimeSpan.FromMilliseconds(interval);
Storyboard.SetTarget(doubleAnimation, arc);
Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath(UIElement.OpacityProperty));
return doubleAnimation;
}
private void ShowChanged()
{
if (storyboard != null)
storyboard.Stop(this);
ShowDefault();
if (IsShowStoryborad)
{
CreateStoryBoard();
storyboard.Begin(this, true);
}
else
{
ShowNoStoryboard();
}
}
private void ShowDefault()
{
arc_0.Opacity = opacity;
arc_100.Opacity = opacity;
arc_50.Opacity = opacity;
arc_75.Opacity = opacity;
}
private void ShowNoStoryboard()
{
for (int i = 0; i < arclist.Count; i )
{
var item = arclist[i];
if (item.Item1 < Value)
{
item.Item2.Opacity = 1;
}
else
{
break;
}
}
}
}
}
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论