Friday, December 04, 2009

Five Minute Silverlight 4 Aides-Memoire #4 – Clipboard Access

private void PopButton_Click(object sender, RoutedEventArgs e)
{
    var newPara = new Paragraph();

    newPara.Inlines.Add(new Run { Text = Clipboard.GetText() });

    this.rightRichTextArea.Blocks.Add(newPara);
}

private void PushButton_Click(object sender, RoutedEventArgs e)
{
    Clipboard.SetText(this.leftRichTextArea.Selection.Text);
}

Tuesday, December 01, 2009

Five Minute Silverlight 4 Aides-Memoire #3 – WebCam/Microphone Device Support

<UserControl.Resources>
<
Style x:Key="ListBoxStyle" TargetType="ListBox">
<
Setter Property="ItemTemplate">
<
Setter.Value>
<
DataTemplate>
<
Image Margin="5" Source="{Binding}" Stretch="UniformToFill" Height="80" VerticalAlignment="Center"/>
</
DataTemplate>
</
Setter.Value>
</
Setter>
<
Setter Property="ItemsPanel">
<
Setter.Value>
<
ItemsPanelTemplate>
<
StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</
ItemsPanelTemplate>
</
Setter.Value>
</
Setter>
</
Style>
</
UserControl.Resources>



<StackPanel Grid.Row="2" Grid.Column="0">
<
Button Content="Start WebCam" Click="StartWebCam_Click" Height="30" />
<
Button Content="Snapshot" Click="Snapshot_Click" Height="30" />
</
StackPanel>
<
Rectangle Grid.Row="0" Stretch="Fill" Grid.Column="1" x:Name="CapturedImage" />
<ListBox Grid.Row="1" Margin="10" Grid.RowSpan="2" Grid.Column="1" x:Name="Snapshots" Style="{StaticResource ListBoxStyle}"/>


private void StartWebCam_Click(object sender, RoutedEventArgs e)
{
if (!CaptureDeviceConfiguration.AllowedDeviceAccess & !CaptureDeviceConfiguration.RequestDeviceAccess())
return;

VideoBrush videoBrush = new VideoBrush();
videoBrush.SetSource(captureSource);
CapturedImage.Fill = videoBrush;
this.Snapshots.ItemsSource = snaps;
captureSource.Start();
}


private void Snapshot_Click(object sender, RoutedEventArgs e)
{
captureSource.AsyncCaptureImage((image) => { snaps.Add(image); });
}

Five Minute Silverlight 4 Aides-Memoire #2 – Printing

private void Print_Click(object sender, RoutedEventArgs e)
{
    PrintDocument printDoc = new PrintDocument();

    // Name that will show in the spooler...
    printDoc.DocumentName = "Directory Tree";
    printDoc.PrintPage += new EventHandler<PrintPageEventArgs>(printDoc_PrintPage);
    printDoc.Print();
}

private void printDoc_PrintPage(object sender, PrintPageEventArgs e)
{
    // Needs a UIElement (visual tree)
    e.PageVisual = DirectoriesTreeView;
    // Setting true will ensure PrintPage is called again for subsequent pages
    e.HasMorePages = false;
}

Monday, November 30, 2009

Five Minute Silverlight 4 Aides-Memoire #1 – Drag and Drop

Build a hierarchical treeview of files and directories dropped from a Windows Explorer window. Requires elevated trust running out of browser to avoid security exceptions when accessing the directory information in the FileInfo objects returned from the e.Data.GetData call.

image



<Grid x:Name="LayoutRoot" AllowDrop="True" Drop="LayoutRoot_Drop">
<
my:TreeView x:Name="DirectoriesTreeView" Margin="50" Width="500" />
</
Grid>

private void LayoutRoot_Drop(object sender, DragEventArgs e)
{
if (!Application.Current.HasElevatedPermissions e.Data == null)
return;

FileInfo[] fileInfos = e.Data.GetData(DataFormats.FileDrop) as FileInfo[];
if (fileInfos == null)
return;
List<FileInfo> sortedList = new List<FileInfo>(fileInfos);

sortedList.Sort((x, y) =>
{
if (x.Attributes == FileAttributes.Directory && y.Attributes != FileAttributes.Directory)
return -1;
if (y.Attributes == FileAttributes.Directory && x.Attributes != FileAttributes.Directory)
return 1;
return x.Name.CompareTo(y.Name);
});

foreach (FileInfo fileInfo in sortedList)
{
var newNode = new TreeViewItem { Header = fileInfo.Name };
DirectoriesTreeView.Items.Add(newNode);
if (!fileInfo.Exists)
ProcessDirectory(fileInfo, newNode);
}
}
private static void ProcessDirectory(FileSystemInfo fileSystemInfo, TreeViewItem currentNode)
{
DirectoryInfo directoryInfo = new DirectoryInfo(fileSystemInfo.FullName);

foreach (FileSystemInfo childFileSystemInfo in directoryInfo.EnumerateFileSystemInfos())
{
var newNode = new TreeViewItem { Header = childFileSystemInfo.Name };
currentNode.Items.Add(newNode);
if (childFileSystemInfo.Attributes == FileAttributes.Directory)
ProcessDirectory(childFileSystemInfo, newNode);
}
}

Wednesday, November 18, 2009

Silverlight 4

Quite unexpectedly, at least I wasn’t expecting it, today saw the announcement of the first beta of Silverlight 4 at PDC09 in Los Angeles. Barely has version 3 made it out there, but we have a new beta to play with together with another rev of Blend to support it (and .NET 4 in general).
A quick run through the main features in the beta:
  • Printing – Full access to the regular print facilities of the host machine – the print dialog and print preview features – so long to all the writable bitmaps and HTML bridge hacks
  • Webcam/microphone support – Subject to UAC/user confirmation, you can now programmatically get access to connected devices, capture video, stills and audio input streams
  • A Rich TextBox control – the WPF RTF control makes its appearance in Silverlight – embed other UI Elements, control text Runs and other formatting such as Text Decorations
  • Clipboard Access – Programmatic access to the clipboard – again subject to UAC
  • Right Mouse Context Menus – Full control of a customisable context menu – add whatever you want to it and respond to selection events
  • Drag-Drop Drop-Target support Silverlight apps can now act as drag-drop drop-targets receiving file information for the dropped file object(s)
  • MVVM Commanding – Finally proper implementation of the ICommand interface – no longer a need to kludge it via bindings to helper classes
  • IDataErrorInfo/Validation support – Integration of the support for the IDataErrorInfo interface in Silverlight’s DataBinding giving a validation framework for input controls to leverage in consistently surfacing data input errors with asynchronous data validation
  • String Formatting in Binding Extensions – As with WPF, Silverlight has been retrofitted with improved formatting support in the binding extension to give better control over stuff like date, time and other formatting for locales and long and short formatting
  • Out of Browser improvements: HTML content - Hosting of HTML content within the Silverlight app; Notifications (aka toast) support; Elevated Trust (again via UAC) access to the My Documents, My Videos, My Pictures special folders; Cross domain access and Full keyboard access in Full Screen mode
  • Better tooling and integration of RIA/ADO Services/WCF/MEF
Lots to check out and try out – exciting stuff!

Tuesday, August 11, 2009

Five Minute Silverlight 3 Aides-Memoire #5 – Based on Styles

    <UserControl.Resources>
<
Style x:Key="ColouredContentControlStyle" TargetType="ContentControl">
<
Setter Property="Height" Value="30" />
<
Setter Property="Width" Value="100" />
<
Setter Property="Margin" Value="10" />
<
Setter Property="FontFamily" Value="Verdana" />
<
Setter Property="FontSize" Value="14" />
<
Setter Property="FontStyle" Value="Italic" />
</
Style>

<
Style x:Key="ColouredCheckBoxStyle" BasedOn="{StaticResource ColouredContentControlStyle}" TargetType="CheckBox">
<
Setter Property="Width" Value="154" />
</
Style>

<
Style x:Key="RedButtonStyle" BasedOn="{StaticResource ColouredContentControlStyle}" TargetType="Button">
<
Setter Property="Foreground" Value="Red" />
</
Style>
<
Style x:Key="GreenButtonStyle" BasedOn="{StaticResource ColouredContentControlStyle}" TargetType="Button">
<
Setter Property="Foreground" Value="Green" />
</
Style>
<
Style x:Key="YellowCheckBoxStyle" BasedOn="{StaticResource ColouredCheckBoxStyle}" TargetType="CheckBox">
<
Setter Property="Foreground" Value="Yellow" />
</
Style>
<
Style x:Key="OrangeRadioButtonStyle" BasedOn="{StaticResource ColouredContentControlStyle}" TargetType="RadioButton">
<
Setter Property="Foreground" Value="Orange" />
<
Setter Property="Width" Value="190" />
<
Setter Property="FontWeight" Value="Bold" />
</
Style>
</
UserControl.Resources>
<
Grid x:Name="LayoutRoot">
<
Grid.Background>
<
LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<
GradientStop Color="Black" Offset="0"/>
<
GradientStop Color="White" Offset="1"/>
</
LinearGradientBrush>
</
Grid.Background>
<
StackPanel Orientation="Vertical" Margin="20">
<
Button Style="{StaticResource RedButtonStyle}" Content="Red Button" />
<
CheckBox Style="{StaticResource YellowCheckBoxStyle}" Content="Yellow CheckBox" />
<
RadioButton Style="{StaticResource OrangeRadioButtonStyle}" Content="Orange RadioButton" />
<
Button Style="{StaticResource GreenButtonStyle}" Content="Green Button" />
</
StackPanel>
</
Grid>


FiveMinuteSilverlight3_5

Five Minute Silverlight 3 Aides-Memoire #4 – Merged Resource Dictionaries

ButtonStyles.xaml:

<ResourceDictionary 
x:Name="ButtonsStyles"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
<
Style x:Key="RedButton" TargetType="Button">
<
Setter Property="Foreground" Value="Red" />
<
Setter Property="Height" Value="30" />
<
Setter Property="Width" Value="100" />
<
Setter Property="Margin" Value="10" />
</
Style>
<
Style x:Key="GreenButton" TargetType="Button">
<
Setter Property="Foreground" Value="Green" />
<
Setter Property="Height" Value="30" />
<
Setter Property="Width" Value="100" />
<
Setter Property="Margin" Value="10" />
</
Style>
</
ResourceDictionary>


CheckBoxStyles.xaml:




<ResourceDictionary 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="CheckBoxStyles"
>
<
Style x:Key="YellowCheckBox" TargetType="CheckBox">
<
Setter Property="Foreground" Value="Yellow" />
<
Setter Property="Height" Value="30" />
<
Setter Property="Margin" Value="10" />
</
Style>
<
Style x:Key="OrangeCheckBox" TargetType="CheckBox">
<
Setter Property="Foreground" Value="Orange" />
<
Setter Property="Height" Value="30" />
<
Setter Property="Margin" Value="10" />
</
Style>
</
ResourceDictionary>


MainPage.xaml:




<UserControl.Resources>
<
ResourceDictionary>
<
ResourceDictionary.MergedDictionaries>
<
ResourceDictionary Source="ButtonStyles.xaml" />
<
ResourceDictionary Source="CheckBoxStyles.xaml" />
</
ResourceDictionary.MergedDictionaries>
</
ResourceDictionary>
</
UserControl.Resources>
<
Grid x:Name="LayoutRoot">
<
Grid.Background>
<
LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<
GradientStop Color="Black" Offset="0"/>
<
GradientStop Color="White" Offset="1"/>
</
LinearGradientBrush>
</
Grid.Background>
<
StackPanel Orientation="Horizontal">
<
Button Grid.Row="0" Grid.Column="0" Style="{StaticResource RedButton}" Content="Red Button" />
<
Button Grid.Row="0" Grid.Column="1" Style="{StaticResource GreenButton}" Content="Green Button" />
<
CheckBox Grid.Row="1" Grid.Column="0" Style="{StaticResource YellowCheckBox}" Content="Yellow CheckBox" />
<
CheckBox Grid.Row="1" Grid.Column="1" Style="{StaticResource OrangeCheckBox}" Content="Orange CheckBox" />
</
StackPanel>
</
Grid>

FiveMinuteSilverlight3_4

Thursday, August 06, 2009

Five Minute Silverlight 3 Aides-Memoire #3 – Element-to-element binding

    <Grid x:Name="LayoutRoot">
<
Grid.RowDefinitions>
<
RowDefinition Height="*" />
<
RowDefinition Height="40" />
</
Grid.RowDefinitions>
<
Grid.ColumnDefinitions>
<
ColumnDefinition />
<
ColumnDefinition />
</
Grid.ColumnDefinitions>
<
Grid.Background>
<
LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<
GradientStop Color="Black" Offset="0"/>
<
GradientStop Color="White" Offset="1"/>
</
LinearGradientBrush>
</
Grid.Background>
<
Image Grid.Column="0" HorizontalAlignment="Left" Margin="50,50,0,0" Width="200" Height="200" Source="OpenfeatureLizardSquare.jpg">
<
Image.Projection>
<
PlaneProjection x:Name="ImagePlaneProjection"/>
</
Image.Projection>
</
Image>
<
Slider Value="{Binding RotationX, Mode=TwoWay, ElementName=ImagePlaneProjection}" Grid.Row="1" Grid.Column="0" x:Name="XSlider" Minimum="-360" Maximum="360" Margin="50,10" />
<
Slider Value="{Binding RotationY, Mode=TwoWay, ElementName=ImagePlaneProjection}" Grid.Row="1" Grid.Column="1" x:Name="YSlider" Minimum="-360" Maximum="360" Margin="50,10" />
</
Grid>

FiveMinuteSilverlight3_3

Wednesday, August 05, 2009

Five Minute Silverlight 3 Aides-Memoire #2 – Pixel Effects

    <Grid x:Name="LayoutRoot">
<
Grid.ColumnDefinitions>
<
ColumnDefinition />
<
ColumnDefinition />
</
Grid.ColumnDefinitions>
<
Grid.Background>
<
LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<
GradientStop Color="Black" Offset="0"/>
<
GradientStop Color="White" Offset="1"/>
</
LinearGradientBrush>
</
Grid.Background>
<
Image HorizontalAlignment="Left" Grid.Column="0" Margin="50,50,0,0" Width="200" Height="200" Source="OpenfeatureLizardSquare.jpg">
<
Image.Effect>
<
BlurEffect Radius="15"/>
</
Image.Effect>
</
Image>
<
Image HorizontalAlignment="Left" Grid.Column="1" Margin="50,50,0,0" Width="200" Height="200" Source="OpenfeatureLizardSquare.jpg">
<
Image.Effect>
<
DropShadowEffect ShadowDepth="10"/>
</
Image.Effect>
</
Image>
</
Grid>

FiveMinuteSilverlight3_2

Five Minute Silverlight 3 Aides-Memoire #1 – 3D Projection

    <Grid x:Name="LayoutRoot">
<
Grid.Background>
<
LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<
GradientStop Color="Black" Offset="0"/>
<
GradientStop Color="White" Offset="1"/>
</
LinearGradientBrush>
</
Grid.Background>
<
Image HorizontalAlignment="Left" Margin="50,50,0,0" Width="200" Height="200" Source="OpenfeatureLizardSquare.jpg">
<
Image.Projection>
<
PlaneProjection RotationX="-25" RotationY="45"/>
</
Image.Projection>
</
Image>
</
Grid>

FiveMinuteSilverlight3_1