将声音添加到TypeOnAction Tigger [英] Adding sound to the TypeOnAction Tigger

查看:64
本文介绍了将声音添加到TypeOnAction Tigger的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

向TypeOnAction触发器添加声音


04-26-2010 12:28 PM |

Adding sound to the TypeOnAction Trigger

04-26-2010 12:28 PM |

 

我希望这是正确的论坛。我是Silverlight的新手。

I hope this is the right forum. I am new to Silverlight.

我找到了TypeOnAction触发器的代码
http://electricbeach.org/?p=339
我成功加入了我的项目。

I found code for a TypeOnAction trigger http://electricbeach.org/?p=339  that I successfully included in my project.

现在我希望每次输入一个字母时都能发出咔嗒声。这是我的尝试(我只在//播放音频评论下添加了代码):

Now I want to the behavior to make a click sound everytime a letter is typed. Here's my attempt (I only added the code under the // Play Audio comment):

 

1  using System;
2  using System.Collections.Generic;
3  using System.Text;
4  using System.Windows;
5  using System.Windows.Controls;
6  using System.Windows.Data;
7  using System.Windows.Documents;
8  using System.Windows.Input;
9  using System.Windows.Media;
10  using System.Windows.Media.Imaging;
11  using System.Windows.Shapes;
12  using System.Windows.Interactivity;
13  using System.Windows.Threading;
14   
15  namespace InCommonScreens
16  {
17    //
18    // If you want your Action to target elements other than its parent, extend your class
19    // from TargetedTriggerAction instead of from TriggerAction
20    //
21    public class TypeOnAction : TriggerAction<TextBlock>
22    {
23      DispatcherTimer timer;
24      int len = 1;
25   
26      public TypeOnAction()
27      {
28        // Insert code required on object creation below this point.
29        timer = new DispatcherTimer();
30  		}
31   
32      protected override void Invoke(object o)
33      {
34        // Insert code that defines what the Action will do when triggered/invoked.
35        if (AssociatedObject == null)
36          return;
37   			
38        AssociatedObject.Text = "";
39        timer.Interval = TimeSpan.FromSeconds(IntervalInSeconds);
40        timer.Tick += new EventHandler(timer_Tick);
41        len = 1;
42        timer.Start();
43      }
44   
45      
46      void timer_Tick(object sender, EventArgs e)
47      {
48        if (len > 0 && len <= TypeOnText.Length)
49        {
50          AssociatedObject.Text = TypeOnText.Substring(0, len);
51  				
52  				// Play Audio
53  				MediaElement media = new MediaElement();
54   				media.Source = new Uri("click.mp3", UriKind.Relative);
55   				media.AutoPlay = false;
56  				media.Position = new TimeSpan(0,0,0,0);
57  				media.Volume = 1.0;
58   				media.Play();
59  				media.Stop();
60   				// Add Remove and Error Handling
61  							
62          len++;
63          timer.Start();
64        }
65        else
66          timer.Stop();
67      }
68   
69      public string TypeOnText
70      {
71        get { return (string)GetValue(TypeOnTextProperty); }
72        set { SetValue(TypeOnTextProperty, value); }
73      }
74   
75      // Using a DependencyProperty as the backing store for TypeOnText. This enables animation, styling, binding, etc…
76      public static readonly DependencyProperty TypeOnTextProperty =
77        DependencyProperty.Register("TypeOnText", typeof(string), typeof(TypeOnAction), new PropertyMetadata(""));
78   
79   
80   
81      public double IntervalInSeconds
82      {
83        get { return (double)GetValue(IntervalInSecondsProperty); }
84        set { SetValue(IntervalInSecondsProperty, value); }
85      }
86   
87      // Using a DependencyProperty as the backing store for IntervalInSeconds. This enables animation, styling, binding, etc…
88      public static readonly DependencyProperty IntervalInSecondsProperty =
89        DependencyProperty.Register("IntervalInSeconds", typeof(double), typeof(TypeOnAction), new PropertyMetadata(0.35));
90   
91   
92   
93    }
94  }

 

我的操作仍然有效,但没有声音。有什么想法吗?

My action still works but no sound. Any ideas?

推荐答案

你调用.Play()然后立即调用.Stop 。()&NBSP;我想您要做的是在xaml中创建media元素,并为其命名,以便可以从代码中引用它。 这样你就不会每次都重新创建它,然后你的代码可以
就像这样(完全未经测试,在我的头顶,公平警告:))

You call .Play() and then immediately .Stop().  I think what you will want to do, is create the media element in your xaml, and give it a name so it can be referenced from code.  That way you are not recreating it every time, then you code could be something like this (completely untested, off the top of my head, fair warning :))

this .MyMediaElement.Stop();

this.MyMediaElement.Stop();

this.MyMediaElement.Position = new TimeSpan(0,0,0);

this.MyMediaElement.Position = new TimeSpan(0,0,0);

this.MyMediaElement.Play ();

this.MyMediaElement.Play();

 

如果已播放,则会停止播放,然后将其播放并播放。 其他属性如volume和autoplay可以在xaml中使用Blend进行设置。

This will stop it if it is already playing, rewind it, and make it play.  The other properties like volume and autoplay can be set in xaml with Blend.


这篇关于将声音添加到TypeOnAction Tigger的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆