`
jinvasshole
  • 浏览: 770979 次
文章分类
社区版块
存档分类
最新评论

稳扎稳打Silverlight

 
阅读更多

示例
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();
        }
    }
}
1
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics