例子:Alarm Clock with voice Commands Sample
通过本例子学习:
- 如何使用自定义字体文件(.TTF)
- 如何播放声音
- 动画的使用
- Speech
- 设置闹铃
- 应用 设置 数据存储到IsolatedStorage
- 如何使用自定义字体文件(.TTF)
<TextBlock x:Name="TimeBlock" HorizontalAlignment="Center"
FontFamily="/AlarmClockWithVoice;component/Fonts/Fonts.zip#Quartz MS"
Margin="89,272,87,256" FontSize="60">
字体文件BuildAction为Resource的使用方式:
(FontFamily="/AlarmClockWithVoice;component/Fonts/Fonts.zip#Quartz MS")
字体文件BuildAction为Content的使用方式:
(FontFamily="/Assets/Fonts/Fonts.zip#Quartz MS")
注意: Windows Phone 显示应用指定的 FontFamily 中的文本。如果字符不受 FontFamily 支持,或者应用未指定 FontFamily,Windows Phone 将基于字符和用于显示文本的框架元素的 xml:lang 选择一种字体。
- 如何播放声音
以流的方式读取本地文件,并创建SoundEffect对象。
StreamResourceInfo info = App.GetResourceStream(new Uri("Audio/alarm.wav", UriKind.Relative)); Microsoft.Xna.Framework.Audio.SoundEffect alarm = SoundEffect.FromStream(info.Stream);
播放,停止等操作,当前SoundEffect对象的状态SoundState 可以通过 属性State获取:
this.alarmSound.Play(); } else if (alarmSound.State == SoundState.Playing) { this.alarmSound.Stop(); } } else if (alarmSound.State == SoundState.Playing) { this.alarmSound.Stop(); }
- 动画的使用
小时指针 图片的动画:
<Image x:Name="hourHand" HorizontalAlignment="Left" Height="20" Margin="255,203,0,0" Grid.Row="1" VerticalAlignment="Top" Width="99" Source="/Images/hourHand.jpg" RenderTransformOrigin="0.097,0.5"> <Image.RenderTransform> <CompositeTransform ScaleX="1.5" ScaleY="1.5" TranslateX="-20.15"/> </Image.RenderTransform> </Image>
主要是通过“UIElement.RenderTransform”来实现的: public Transform RenderTransform { get; set; } 要使用多个动画效果,可以使用TransformGroup把多个动画包含其中。
7种变换分别是:
1、TransLateTransForm 移动位置,包含水平移动和垂直移动
2、ScaleTransform 缩放变换 对UI进行放大缩小 包含X轴上的缩放和Y轴上的缩放
3、RotateTransform 旋转 根据定义的旋转点设置角度进行旋转
4、SkewTransform 对UI进行一定角度的倾斜
5、MatrixTransfrom 矩阵变换,一句标准矩阵表示的变换
6、TransformGroup 复合变换按照指定顺序将多个变换复合为一个变换
7、CompositeTransform 组合变换按照固定顺序组合一系列变换
- Speech
语音命令:
1. 右键工程,新 添加 “语音命令项”
<Command Name="alarmTimeSet"> <Example>Set alarm to 7:30 am.</Example> <ListenFor>Set alarm to {hour} {minute} {ampm}</ListenFor> <Feedback>Setting the alarm clock</Feedback> <Navigate Target="MainPage.xaml"/> </Command> <PhraseList Label="hour"> <Item>1</Item> <Item>2</Item> <Item>3</Item> <Item>4</Item> <Item>5</Item> <Item>6</Item> <Item>7</Item> <Item>8</Item> <Item>9</Item> <Item>10</Item> <Item>11</Item> <Item>12</Item> </PhraseList>
2. 初始化语音命令:
Uri uri = new Uri("ms-appx:///voicecommands.xml", UriKind.Absolute); await Windows.Phone.Speech.VoiceCommands.VoiceCommandService.InstallCommandSetsFromFileAsync(uri);
语音识别读取:
1. 初始化语音识别相关属性:
using Windows.Phone.Speech.Recognition; using Windows.Phone.Speech.Synthesis; Speech.recognizer = new SpeechRecognizer(); Speech.synthesizer = new SpeechSynthesizer(); Speech.recognizerUI = new SpeechRecognizerUI(); // Sets the en-US male voice. IEnumerable<VoiceInformation> enUSMaleVoices = from voice in InstalledVoices.All where voice.Gender == VoiceGender.Male && voice.Language == "en-US" select voice; Speech.synthesizer.SetVoice(enUSMaleVoices.ElementAt(0));
2. 朗读文本:
await Speech.synthesizer.SpeakTextAsync("Alarm is now set for " + Settings.alarmTime.Value.Hours + " " + Settings.alarmTime.Value.Minutes);
- 设置闹铃
1. 初始化设置闹钟:
using Microsoft.Phone.Scheduler; public static Alarm alarm; Clock.alarm = new Alarm("alarm"); Clock.alarm.ExpirationTime = DateTime.MaxValue; Clock.alarm.RecurrenceType = RecurrenceInterval.Daily; Clock.alarm.Sound = new Uri("Audio/alarm.wav", UriKind.Relative); Clock.alarm.Content = "Alarm ringing!";
2. 添加到SchduleActionService,一个专门用于管理计划任务的类:
ScheduledActionService.Add(Clock.alarm);
3.根据用户操作输入,设定时间:
参数为 年月日时分秒, 1 表示everyday。
Clock.alarm.BeginTime = new DateTime(1, 1, 1, hours, minutes, 0);
- 应用 设置 数据存储到IsolatedStorage
using System.IO.IsolatedStorage; public class StoredItem<T> { private T value; private T defaultValue; private string name; private bool needRefresh; public StoredItem(string _name, T _defaultValue) { this.name = _name; this.defaultValue = _defaultValue; // If isolated storage doesn't have the value stored yet if (!IsolatedStorageSettings.ApplicationSettings.TryGetValue(this.name, out this.value)) { this.value = _defaultValue; IsolatedStorageSettings.ApplicationSettings[this.name] = _defaultValue; } this.needRefresh = false; } public T Value { get { if (this.needRefresh) { if (!IsolatedStorageSettings.ApplicationSettings.TryGetValue(this.name, out this.value)) { IsolatedStorageSettings.ApplicationSettings[this.name] = this.defaultValue; this.value = this.defaultValue; } this.needRefresh = false; } return this.value; } set { if (this.value.Equals(value)) return; // Store the value in isolated storage. IsolatedStorageSettings.ApplicationSettings[this.name] = value; this.needRefresh = true; } } public T DefaultValue { get { return this.defaultValue; } } public string Name { get { return this.name; } } public override string ToString() { return this.name + " with value: " + this.value.ToString() + ", default value: " + this.defaultValue.ToString(); } public void ForceRefresh() { this.needRefresh = true; } }
posted on 2013-08-13 16:13 Amazing-Ren 阅读(207) 评论(0) 编辑 收藏 举报