<Window x:Class="Chapter05.ImageBrushExample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Chapter05" Height="300" Width="300">
<StackPanel Margin="5" Grid.Column="0" Grid.Row="1">
<TextBlock Margin="5" Text="Stretch = Uniform" />
<Button Width="135" Height="100">
<Button.Background>
<ImageBrush ImageSource="c:image.jpg" Stretch="Uniform" />
</Button.Background>
</Button>
</StackPanel>
</Window>
Windows Presentation Foundation
Speech Synthesis demo
<Window x:Class="SoundAndVideo.SpeechSynthesis"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="SpeechSynthesis" Height="300" Width="300">
<StackPanel>
<TextBox Name="txtWords" ScrollViewer.HorizontalScrollBarVisibility="Visible" TextWrapping="Wrap">Hello, world</TextBox>
<Button Margin="5" Grid.Row="1" Click="cmdSpeak_Click">Speak</Button>
<Button Margin="5" Grid.Row="2" Click="cmdPromptTest_Click">Prompt Test</Button>
</StackPanel>
</Window>
//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Speech.Synthesis;
namespace SoundAndVideo
{
public partial class SpeechSynthesis : System.Windows.Window
{
public SpeechSynthesis()
{
InitializeComponent();
}
private void cmdSpeak_Click(object sender, RoutedEventArgs e)
{
SpeechSynthesizer synthesizer = new SpeechSynthesizer();
synthesizer.Speak(txtWords.Text);
}
private void cmdPromptTest_Click(object sender, RoutedEventArgs e)
{
PromptBuilder prompt = new PromptBuilder();
prompt.AppendText("A");
prompt.AppendBreak(TimeSpan.FromSeconds(2));
prompt.AppendText("A ", PromptEmphasis.Reduced);
PromptStyle style = new PromptStyle();
style.Rate = PromptRate.ExtraSlow;
style.Emphasis = PromptEmphasis.Strong;
prompt.StartStyle(style);
prompt.AppendText("B ");
prompt.EndStyle();
prompt.AppendText("C?");
SpeechSynthesizer synthesizer = new SpeechSynthesizer();
synthesizer.Speak(prompt);
}
}
}
SpeechSynthesizer demo
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Speech Synthesis" Height="300" Width="300"
>
<Grid>
</Grid>
</Window>
//File:Window.xaml.cs
using System;
using System.Speech.Synthesis;
namespace WpfApplication1
{
public partial class Window1 : System.Windows.Window
{
public Window1()
{
InitializeComponent();
SpeechSynthesizer synthesizer = new SpeechSynthesizer();
PromptBuilder promptBuilder = new PromptBuilder();
promptBuilder.AppendTextWithHint("WPF", SayAs.SpellOut);
promptBuilder.AppendText("sounds better than WPF.");
// Pause for 2 seconds
promptBuilder.AppendBreak(new TimeSpan(0, 0, 2));
promptBuilder.AppendText("The time is");
promptBuilder.AppendTextWithHint(DateTime.Now.ToString("hh:mm"), SayAs.Time);
promptBuilder.StartVoice("this is a test");
promptBuilder.AppendTextWithHint("queue", SayAs.SpellOut);
promptBuilder.EndVoice();
promptBuilder.AppendText("Do it faster!");
promptBuilder.StartVoice("Hi");
promptBuilder.StartStyle(new PromptStyle(PromptRate.ExtraFast));
promptBuilder.AppendTextWithHint("queue", SayAs.SpellOut);
promptBuilder.EndStyle();
promptBuilder.EndVoice();
synthesizer.SpeakAsync(promptBuilder);
}
}
}
Nested Span elements
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock>
<Span FontFamily="Cambria">
This uses <Span FontWeight="Bold">a
<Span FontStyle="Italic">mixture</Span> of</Span> styles.
</Span>
</TextBlock>
</Page>
Play audio through event trigger
<Window x:Class="SoundAndVideo.SoundPlayerTest"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="SoundPlayerTest" Height="300" Width="300">
<StackPanel>
<Button>
<Button.Content>Play Through Wrapper</Button.Content>
<Button.Style>
<Style>
<Style.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<EventTrigger.Actions>
<SoundPlayerAction Source="test.wav"></SoundPlayerAction>
</EventTrigger.Actions>
</EventTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
</StackPanel>
</Window>
Sound Player Action demo
<Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<Button>A Button With Sounds
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<EventTrigger.Actions>
<SoundPlayerAction Source="click.wav"/>
</EventTrigger.Actions>
</EventTrigger>
<EventTrigger RoutedEvent="Button.MouseEnter">
<EventTrigger.Actions>
<SoundPlayerAction Source="hover.wav"/>
</EventTrigger.Actions>
</EventTrigger>
</Button.Triggers>
</Button>
</Canvas>
SolidColorBrush's color is specified using 8-digit hexadecimal notation.
<Window x:Class="WpfApplication1.ShapesWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ShapesWindow" Height="160" Width="400">
<Window.Resources>
<Style TargetType="{x:Type Rectangle}">
<!-- Gives all the rectangles in this panel a white stroke. -->
<Setter Property="Stroke" Value="White"/>
<Setter Property="StrokeThickness" Value="1"/>
</Style>
</Window.Resources>
<Canvas>
<Rectangle Width="50" Height="50">
<Rectangle.Fill>
<SolidColorBrush Color="#FF0000FF" />
</Rectangle.Fill>
</Rectangle>
</Canvas>
</Window>