穩扎穩打Silverlight(66) - 5.0其它之自定義 XAML 擴展標記, 通過 XNA 處理聲音效果, 支持矢量打印, 統計連擊的次數


[索引頁]
[源碼下載]


穩扎穩打Silverlight(66) - 5.0其它之自定義 XAML 擴展標記, 通過 XNA 處理聲音效果, 支持矢量打印, 連擊的次數



作者:webabcd


介紹
Silverlight 5.0

  • IMarkupExtension - 自定義 XAML 擴展標記
  • 通過 XNA 處理聲音效果
  • 支持矢量打印
  • 統計連擊的次數



在線DEMO
http://www.cnblogs.com/webabcd/archive/2012/03/05/2379862.html


示例
1、IMarkupExtension(自定義 XAML 擴展標記)
XAML/ConcatMarkupExtension.cs

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using System.Xaml;

namespace Silverlight50.XAML
{
/// <summary>
/// 實現自定義 XAML 擴展標記,需要實現 IMarkupExtension<out T> 接口
/// 自定義 XAML 擴展標記以“Extension”結尾,XAML 書寫時可以不必帶“Extension”
///
/// 本例中的 ConcatMarkup 擴展標記,用於合並兩個字符串
/// </summary>
public class ConcatMarkupExtension : IMarkupExtension<string>
{
public object String1 { get; set; }
public object String2 { get; set; }

/// <summary>
/// 需要實現的方法,返回 ConcatMarkup 的計算結果
/// </summary>
public string ProvideValue(IServiceProvider serviceProvider)
{
return String1.ToString() + String2.ToString();
}
}
}

XAML/IMarkupExtension.xaml

<navigation:Page x:Class="Silverlight50.XAML.IMarkupExtension" 
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"
mc:Ignorable
="d"
xmlns:navigation
="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
d:DesignWidth
="640" d:DesignHeight="480"
Title
="IMarkupExtension Page"

xmlns:local
="clr-namespace:Silverlight50.XAML">

<Grid x:Name="LayoutRoot">

<!--
演示自定義的 XAML 擴展標記 ConcatMarkup,本例的結果是 wanglei
-->
<TextBlock Text="{local:ConcatMarkup String1=wang, String2=lei}" />

</Grid>
</navigation:Page>


2、SoundEffectDemo(通過 XNA 處理聲音效果)
Media/SoundEffectDemo.xaml

<navigation:Page x:Class="Silverlight50.Media.SoundEffectDemo" 
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"
mc:Ignorable
="d"
xmlns:navigation
="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
d:DesignWidth
="640" d:DesignHeight="480"
Title
="SoundEffectDemo Page">
<StackPanel HorizontalAlignment="Left">

<StackPanel Orientation="Horizontal">
<TextBlock Name="lblVolume" Width="100" Text="音量" TextAlignment="Center" />
<TextBlock Name="lblPitch" Width="100" Text="高音" TextAlignment="Center" />
<TextBlock Name="lblPan" Width="100" Text="平衡" TextAlignment="Center" />
</StackPanel>

<StackPanel Orientation="Horizontal">
<Slider Name="sliderVolume" Width="100" Height="200" Orientation="Vertical" Minimum="0" Maximum="1" Value="0.5" />
<Slider Name="sliderPitch" Width="100" Height="200" Orientation="Vertical" Maximum="1" Minimum="-1" Value="0" />
<Slider Name="sliderPan" Width="100" Height="200" Orientation="Vertical" Maximum="1" Minimum="-1" Value="0" />
</StackPanel>

<StackPanel Orientation="Horizontal">
<TextBlock Name="txtVolume" Width="100" TextAlignment="Center" />
<TextBlock Name="txtPitch" Width="100" TextAlignment="Center" />
<TextBlock Name="txtPan" Width="100" TextAlignment="Center" />
</StackPanel>

</StackPanel>
</navigation:Page>

Media/SoundEffectDemo.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;

using Microsoft.Xna.Framework.Audio;
using System.Windows.Resources;

namespace Silverlight50.Media
{
/// <summary>
/// Silverlight 5 支持通過 SoundEffect 處理 wav 音效文件
/// 本例演示:如何控制音量,高低音,左右聲道平衡
/// </summary>
public partial class SoundEffectDemo : Page
{
/*
* SoundEffect - 音效對象
* SoundEffect.Play(float volume, float pitch, float pan) - 播放音效
* volume - 音量值,0 到 1 之間
* pitch - 高音值, -1 到 1 之間,-1 是純低音,1 是純高音
* pan - 左右聲道平衡值,-1 到 1 之間,-1 是僅左聲道有聲,1 是僅右聲道有聲
* SoundEffect.Duration - 音效的時長
* SoundEffect.Name - 音效對象的名稱
*/

/*
* SoundEffectInstance - SoundEffect 的對象實例
* SoundEffect.CreateInstance() - 返回 SoundEffect 對象
* SoundEffectInstance.Volume - 音量值,0 到 1 之間
* SoundEffectInstance.Pitch - 高音值, -1 到 1 之間,-1 是純低音,1 是純高音
* SoundEffectInstance.Pan - 左右聲道平衡值,-1 到 1 之間,-1 是僅左聲道有聲,1 是僅右聲道有聲
* SoundEffectInstance.IsLooped - 是否循環播放
* SoundEffectInstance.Play() - 播放
* SoundEffectInstance.Pause() - 暫停
* SoundEffectInstance.Resume() - 繼續播放
* SoundEffectInstance.Stop() - 停止
* SoundEffectInstance.State - 返回音效對象的當前狀態 [Microsoft.Xna.Framework.Audio.SoundState 枚舉]
* SoundState.Playing - 正在播放狀態
* SoundState.Paused - 暫停狀態
* SoundState.Stopped - 停止狀態
*/

/*
SoundEffectInstance soundEffectInstance = soundEffect.CreateInstance();
soundEffectInstance.Volume = 0.5f;
soundEffectInstance.Pitch = -1f;
soundEffectInstance.Pan = 0f;
soundEffectInstance.IsLooped = true;
soundEffectInstance.Play();
soundEffectInstance.Pause();
soundEffectInstance.Resume();
soundEffectInstance.Stop();
SoundState soundState = soundEffectInstance.State;
*/

private SoundEffect _soundEffect;
private SoundEffectInstance _instance;

public SoundEffectDemo()
{
InitializeComponent();

this.Loaded += new RoutedEventHandler(SoundEffectDemo_Loaded);
}

void SoundEffectDemo_Loaded(object sender, RoutedEventArgs e)
{
this.sliderVolume.ValueChanged += new RoutedPropertyChangedEventHandler<double>(sliderVolume_ValueChanged);
this.sliderPitch.ValueChanged += new RoutedPropertyChangedEventHandler<double>(sliderPitch_ValueChanged);
this.sliderPan.ValueChanged += new RoutedPropertyChangedEventHandler<double>(sliderPan_ValueChanged);

// 獲取 wav 文件流
StreamResourceInfo musicStream = Application.GetResourceStream(new Uri("Media/rockyou.wav", UriKind.RelativeOrAbsolute));
_soundEffect = SoundEffect.FromStream(musicStream.Stream);

// 設置音效的初始屬性
_instance = _soundEffect.CreateInstance();
_instance.IsLooped = true;
_instance.Pitch = 0f;
_instance.Pan = 0f;
_instance.Volume = .5f;
_instance.Play();
}

// 控制音量
private void sliderVolume_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
_instance.Volume = (float)e.NewValue;
txtVolume.Text = e.NewValue.ToString("f2");
}

// 控制高低音
private void sliderPitch_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
_instance.Pitch = (float)e.NewValue; ;
txtPitch.Text = e.NewValue.ToString("f2");
}

// 控制左右聲道平衡
private void sliderPan_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
_instance.Pan = (float)e.NewValue; ;
txtPan.Text = e.NewValue.ToString("f2");
}
}
}


3、VectorPrinting(支持矢量打印)

<navigation:Page x:Class="Silverlight50.Other.VectorPrinting" 
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"
mc:Ignorable
="d"
xmlns:navigation
="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
d:DesignWidth
="640" d:DesignHeight="480"
Title
="VectorPrinting Page">
<Grid x:Name="LayoutRoot">

<TextBlock Text="支持矢量打印了" />

</Grid>
</navigation:Page>


4、ClickCount(統計連擊的次數)
Other/ClickCount.xaml

<navigation:Page x:Class="Silverlight50.Other.ClickCount" 
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"
mc:Ignorable
="d"
xmlns:navigation
="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
d:DesignWidth
="640" d:DesignHeight="480"
Title
="ClickCount Page">
<StackPanel x:Name="LayoutRoot">

<Rectangle Name="rectangle" Width="100" Height="30" Fill="Gray" MouseLeftButtonDown="rectangle_MouseLeftButtonDown" />
<TextBlock Name="txt" />

</StackPanel>
</navigation:Page>

Other/ClickCount.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;

namespace Silverlight50.Other
{
public partial class ClickCount : Page
{
public ClickCount()
{
InitializeComponent();
}

protected override void OnNavigatedTo(NavigationEventArgs e)
{

}

private void rectangle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
/*
* MouseButtonEventArgs.ClickCount - 獲取連擊的次數
* 注:連擊的最大間隔時間是由操作系統的"控制面板"-->"鼠標"-->"雙擊速度"設置的
*/

if (e.ClickCount == 1) // Single Click
txt.Text = "Left Mouse Click Counter:" + e.ClickCount.ToString();
else if (e.ClickCount == 2) // Double Click
txt.Text = "Left Mouse Click Counter:" + e.ClickCount.ToString();
else // More Click
txt.Text = "Left Mouse Click Counter:" + e.ClickCount.ToString();
}
}
}



OK
[源碼下載]


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM