Silverlight Journal

December 31, 2009

Static object management with file watchers

Filed under: Uncategorized — Tags: , , — bobbsmooth @ 6:15 pm

I wanted to have some objects remain in memory for use by some WCF services but still be able to refresh them without restarting the service. Basically, when certain files change, I wanted the static objects to be flushed and then re-initialized with the new values in the files.

        private static System.IO.FileSystemWatcher _configWatcher = null;
        private static void StartWatcher()
        {
            if (_configWatcher == null)
            {
//set the path to a directory that I configured in the web.config file.
                string path = HttpContext.Current.Server.MapPath(WebConfigurationManager.AppSettings["CONFIGURATION_DIRECTORY"]);

//initialize the watcher to watch any xml file in the directory identified by the path
                _configWatcher = new System.IO.FileSystemWatcher()
                {
                    Path=path,
                    EnableRaisingEvents=true,
                    NotifyFilter=System.IO.NotifyFilters.LastWrite,
                    Filter="*.xml"
                };

//handle the changed event for when files are modified and the error event
                _configWatcher.Changed += new System.IO.FileSystemEventHandler(_watcher_Changed);
                _configWatcher.Error += new System.IO.ErrorEventHandler(_watcher_Error);
            }
        }
        static void _watcher_Error(object sender, System.IO.ErrorEventArgs e)
        {
                //log error
        }
        public static void _watcher_Changed(object sender, System.IO.FileSystemEventArgs e)
        {
               //reinitialized dependent static objects
        }

December 21, 2009

Prism commanding: KeyUp example

Filed under: Uncategorized — Tags: , , , , — bobbsmooth @ 9:10 pm

Today, I had to build a simple login form using Silverlight 3 and Prism in a MVVM application.  One requirement that I always put on any login form is that the user can hit the enter key to submit her credentials for validation.  This is my solution:

KeyUpBehavior.cs

using System.Windows.Controls;
using System.Windows.Input;
using Microsoft.Practices.Composite.Presentation.Commands;

namespace Infrastructure.Commands.PasswordCommands
{
    public class KeyUpBehavior : CommandBehaviorBase<Control>
    {
        public KeyUpBehavior(Control element)
            : base(element)
        {
            element.KeyUp += new KeyEventHandler(element_KeyUp);
        }

        void element_KeyUp(object sender, KeyEventArgs e)
        {
            //only execute command if the user pressed the enter key
            if (e.Key == Key.Enter)
                base.ExecuteCommand();
        }
    }
}

KeyUp.cs

using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace Infrastructure.Commands.PasswordCommands
{
    public class KeyUp
    {
        #region Command  attached property
        public static ICommand GetCommand(DependencyObject obj)
        {
            return (ICommand)obj.GetValue(CommandProperty);
        }

        public static void SetCommand(DependencyObject obj, ICommand value)
        {
            obj.SetValue(CommandProperty, value);
        }

        // Using a DependencyProperty as the backing store for Command.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CommandProperty =
            DependencyProperty.RegisterAttached("Command", typeof(ICommand), typeof(KeyUp), new PropertyMetadata(OnSetCommandCallback));

        private static void OnSetCommandCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            Control element = dependencyObject as Control;
            if (element != null)
            {
                KeyUpBehavior behavior = GetOrCreateBehavior(element);
                behavior.Command = e.NewValue as ICommand;
            }
        }
        private static KeyUpBehavior GetOrCreateBehavior(Control element)
        {
            KeyUpBehavior behavior = element.GetValue(KeyUpBehaviorProperty) as KeyUpBehavior;
            if (behavior == null)
            {
                behavior = new KeyUpBehavior(element);
                element.SetValue(KeyUpBehaviorProperty, behavior);
            }
            return behavior;
        }
        #endregion

        #region KeyUpBehavior attached property
        public static KeyUpBehavior GetKeyUpBehavior(DependencyObject obj)
        {
            return (KeyUpBehavior)obj.GetValue(KeyUpBehaviorProperty);
        }

        public static void SetKeyUpBehavior(DependencyObject obj, KeyUpBehavior value)
        {
            obj.SetValue(KeyUpBehaviorProperty, value);
        }

        public static readonly DependencyProperty KeyUpBehaviorProperty =
            DependencyProperty.RegisterAttached("KeyUpBehavior", typeof(KeyUpBehavior), typeof(KeyUp), null);
        #endregion

        #region CommandParameter attached property
        public static object GetCommandParameter(DependencyObject obj)
        {
            return (object)obj.GetValue(CommandParameterProperty);
        }

        public static void SetCommandParameter(DependencyObject obj, object value)
        {
            obj.SetValue(CommandParameterProperty, value);
        }

        public static readonly DependencyProperty CommandParameterProperty =
            DependencyProperty.RegisterAttached("CommandParameter", typeof(object), typeof(KeyUp), new PropertyMetadata(OnSetCommandParameterCallback));

        private static void OnSetCommandParameterCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            Control element = dependencyObject as Control;
            if (element != null)
            {
                KeyUpBehavior behavior = GetOrCreateBehavior(element);
                behavior.CommandParameter = e.NewValue;
            }
        }
        #endregion
    }
}

View

        <PasswordBox Grid.Row="2" 
                 Grid.Column="2" 
                 customCommands:KeyUp.Command="{Binding KeyUp_Command}" 
                 customCommands:KeyUp.CommandParameter="{Binding Password, ElementName=txtPassword}" 
                 Password="{Binding Path=User.Password, Mode=TwoWay}" 
                 x:Name="txtPassword" />

ViewModel

        public DelegateCommand<object> KeyUp_Command { get; set; }

        public void KeyUpCommand_Handler(object commandParameter)
        {
            //do authentication, commandParameter is the value stored in PasswordBox.Password.
        }

ViewModel constructor

            KeyUp_Command = new DelegateCommand<object>(KeyUpCommand_Handler);

December 8, 2009

My First Expression Encoder App

Filed under: Uncategorized — Tags: , , , , — bobbsmooth @ 8:59 pm

Problem Background

On my current project, I need to re-encode about 7000 videos into a Smooth Streaming format so that I can take advantage of the functionality in Silverlight’s MediaElement control. The last time I did a big batch of encoding, I simply scripted Windows Media Sever 9. With Expression Encoder 3, however, that’s not possible. Instead you must use the API and create an application to do the work. While not terribly difficult, this project definitely took me outside my comfort zone. There are very few references out there so I hope this helps shed some more light on the solutions that can be built.

Solution

I decided that the best thing to do first was to create a simple application that would encode one video. Then I could expand it to use something like a database or an XML file to script the encoding process.

The first step was to install Expression Encoder 3. I needed the full version because the free version doesn’t support Smooth Streaming.

Next, I started a new WPF solution and added references to all of the Microsoft.Expression.Encoder libraries.

Library References

Project References

In my Window1.asmx file, I created some simple controls for finding a file and starting the process.

<Window x:Class="SingleFileEncoder.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="557">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="25"></RowDefinition>
            <RowDefinition Height="25"></RowDefinition>
            <RowDefinition Height="25"></RowDefinition>

        </Grid.RowDefinitions>
        <StackPanel Orientation="Horizontal" Grid.Row="0">
            <TextBlock Text="Source Video:" VerticalAlignment="Center">
                <TextBlock.Margin>
                    <Thickness>
                        <Thickness.Left>5</Thickness.Left>
                    </Thickness>
                </TextBlock.Margin>
            </TextBlock>
            <TextBox Height="23" Name="txtFileName" VerticalAlignment="Center" Width="300" BorderThickness="1">
                
                <TextBox.BorderBrush>
                    <SolidColorBrush Color="Blue" ></SolidColorBrush>
                </TextBox.BorderBrush>
                <TextBox.Background>
                    <LinearGradientBrush StartPoint=".75,0" EndPoint="0,.5">
                        <GradientStop Color="#CCCCFF" Offset="1" />
                        <GradientStop Color="White" Offset=".25" />
                    </LinearGradientBrush>
                </TextBox.Background>
                <TextBox.Margin>
                    <Thickness>
                        <Thickness.Left>5</Thickness.Left>
                    </Thickness>
                </TextBox.Margin>
            </TextBox>
            <Button Height="23" HorizontalAlignment="Right" Name="btnBrowse" VerticalAlignment="Center" Width="75" Click="btnBrowse_Click">Browse
                <Button.Margin>
                    <Thickness>
                        <Thickness.Left>5</Thickness.Left>
                    </Thickness>
                </Button.Margin>
            </Button>
            <Button Name="btnEncode" Click="btnEncode_Click">
                Encode
                <Button.Margin>
                    <Thickness>
                        <Thickness.Left>5</Thickness.Left>
                    </Thickness>
                </Button.Margin>
            </Button>
        </StackPanel>
        <ProgressBar Grid.Row="1" Name="progressBar" Minimum="0" Maximum="100"></ProgressBar>
        <TextBlock Name="txtMessage" Grid.Row="2"></TextBlock>
    </Grid>
</Window>

In the code behind file, I added using statements for the Encoder namespaces.

 using System;
 using System.Windows;
 using Microsoft.Expression.Encoder;
 using Microsoft.Expression.Encoder.Profiles;
 using Microsoft.Win32;
 

I used the Win32 OpenFileDialog to find a file when the user clicks the Browse button.

private void btnBrowse_Click(object sender, RoutedEventArgs e)
 {
 OpenFileDialog dialog = new OpenFileDialog();
 dialog.ShowDialog();
 txtFileName.Text = dialog.FileName;
 }

In the encode button’s even handler, I assigned that file to a new MediaItem.
MediaItem mediaItem;
mediaItem = new MediaItem(txtFileName.Text);

Next, I created the output profile for my MediaItem. This technique is new to Encoder 3.

 AdvancedVC1VideoProfile videoProfile = new AdvancedVC1VideoProfile
 {
 Bitrate = new VariableConstrainedBitrate(403, 600),
 Complexity = VideoComplexity.Fastest,
 SmoothStreaming = true,
 Size = new System.Drawing.Size(236, 176),
 KeyFrameDistance = new TimeSpan(0, 0, 2),
 InLoopFilter = true
 };
mediaItem.OutputFormat = new WindowsMediaOutputFormat()
 {
 VideoProfile = videoProfile
 };

Next, I created a Job and assigned the MediaItem to it.

 Microsoft.Expression.Encoder.Job job = new Job();
 job.MediaItems.Add(mediaItem);
 job.OutputDirectory = @"C:\EncodedContent\";
 job.CreateSubfolder = false;

I assigned a couple of event handlers that would let me see progress as it was made and notify me when the encoding was complete.

job.EncodeProgress += new EventHandler<EncodeProgressEventArgs>(job_EncodeProgress);
 job.EncodeCompleted += new EventHandler<EncodeCompletedEventArgs>(job_EncodeCompleted);

Finally, I fire the encode process for the Job.

job.Encode();

December 4, 2009

Silverlight Journal

Filed under: Uncategorized — Tags: , , , — bobbsmooth @ 11:42 pm

This will be the home of my musings on Silverlight, MVVM, Prism, Twitter, ArcGIS Server, JavaScript, Agile methodologies and all manner of programming minutia.

You can follow me on twitter at http://twitter.com/jrockers.

Theme: Shocking Blue Green. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.