Currently, most of the laptops have a lot of features (SmartCard reader, etc.) that are not used.
But, with Windows Seven, one of this feature appears more useful: biometric authentication. Indeed, for the moment, this authentication must be done with tiers softwares, developed by constructors.
But now, Windows Seven get a new API allowing the programmation/manipulation of biometric readers. So it's now possible, by playing with some PInvoke, to create a wrapper for the API.
To help you, I've managed to create this wrapper. For now, only the method WinBioIdentifyWithCallback is implemented but it should be quite easy to implement other. This method allows to check, asynchronously, that the user who have swipe his finger on the sensor is effectively a know user:

In case of the user is unknow, the application receive an event which indicate the problem:
To use it, it's quite simple:
It's important to note that this wrapper is composed by 3 DLLs:
- Biometric.Wrapper.dll: It's the managed DLL calling the unmanaged methods
- Interop.WinBio.dll: This DLL contains the definition of the unmanaged methods (PInvoke). Instead of creating this DLL by hand, I've prefered to generate it by using the tool PInvoker. It's quick, workfs fine and, in case of any issue, tool's creator is really active so don't hesitate to give it a try !
- PInvoker.Marshal.dll: DLL generated by the tool PInvoker and which contains the definition of thetypes that are used in Interop.WinBio.dll
To give a try to this, just follow this link : http://morpheus.developpez.com/blog/BiometricWrapper/BiometricWrapper.zip
Have fun !
Bye
When you are working with WPF and MVVM Pattern, you can regret that the designer doesn’t get data, in design time, that allow him to simply modified the graphical interface.
To help my developments, I’ve created a little attached property which will create an instance of the type passed in parameter:
public static class DesignTimeHelper
{
/// <summary>
/// DependencyProperty used to store the DesignTime property.
/// </summary>
public static readonly DependencyProperty DesignTimeDataProperty =
DependencyProperty.RegisterAttached("DesignTimeData", typeof(Type), typeof(DesignTimeHelper), new FrameworkPropertyMetadata(null, new PropertyChangedCallback(OnDesignTimeDataChanged)));
/// <summary>
/// Gets the design time data.
/// </summary>
/// <param name="obj">The obj.</param>
/// <returns></returns>
public static Type GetDesignTimeData(DependencyObject obj)
{
return (Type)obj.GetValue(DesignTimeDataProperty);
}
/// <summary>
/// Sets the design time data.
/// </summary>
/// <param name="obj">The obj.</param>
/// <param name="value">The value.</param>
public static void SetDesignTimeData(DependencyObject obj, Type value)
{
obj.SetValue(DesignTimeDataProperty, value);
}
/// <summary>
/// Called when DesignTimeData changed.
/// </summary>
/// <param name="d">The source object.</param>
/// <param name="e">The <see cref="System.Windows.DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
private static void OnDesignTimeDataChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var isOnDesignMode = DesignerProperties.GetIsInDesignMode(new DependencyObject());
if (isOnDesignMode)
{
var element = d as FrameworkElement;
if (element == null)
throw new NullReferenceException("element must not be null and must be an UIElement.");
var designTimeDataType = e.NewValue as Type;
if (designTimeDataType == null)
throw new NullReferenceException("designTimeDataType must not be null.");
element.DataContext = Activator.CreateInstance(designTimeDataType);
}
}
}
To use this attached property, it’s simple: just create 2 ViewModels (one used for the data in “runtime” and the second one used for the data in “designtime”):
Then, use the property:
At “design time”, we can see that data are really different from “runtime”:
For your information, here is the content of the ViewModel in “design” mode:
namespace TestToolbox.ViewModels.Design
{
class SampleViewModel : ISampleViewModel
{
#region ISampleViewModel Members
public string Text
{
get { return "Design time text"; }
}
public string Title
{
get { return "Design time title"; }
}
#endregion
}
}
And here is the ViewModel for “runtime” mode:
namespace TestToolbox.ViewModels
{
public class SampleViewModel : ISampleViewModel
{
#region ISampleViewModel Members
public string Text
{
get { return "Runtime text"; }
}
public string Title
{
get { return "Runtime title"; }
}
#endregion
}
}
That’s all ! Now, your designer is able to work with (sample) data that allow him to design the graphical interface of your application without having to launch it for testing/viewing the results.
Bye !
PS: Thanks to Simon for the suggestion of using a type instead of a simple string [;)]
When I work on projects, I often need to create business objects (Customer, Country, etc.) and I would like to be able to done this step very quickly.
So I’ve created a little addin to Visual Studio 2008 that allow me to easily create my business objects/entities. This a an old project that I’ve decided to release even if I plan to re-write it because I think it could be an useful in addition to the XAML PowerToys developed by Karl Shifflett. To use it, just right click on a project and select “Add Business Object” under “VS PowerTools”:
Once this is done, a little window is displayed and you can specify the name of your entities, if you want to mark it as serializable (using the DataContract/DataMember attributes) and the number of properties:
Once you have entered the number of properties you want on your entities, just press the TAB key to see an interface that allow you to write the name and the type of the properties:
When you’re done, simply click the “OK” button to launch the create of the entity. Using CodeDOM, the addin performs the following tasks:
- Add the references and “using” necessary
- Create a partial class corresponding to the entity you have entered the name. This class implements INotifyPropertyChanged and INotifyPropertyChanging
- Create the partial methods corresponding to the properties defined in your class
- Define a collection for your class. The collection is simply a ObservableCollection of the type you’ve just created
- Etc..
To install the addin, simply copy the file VSPowerTools.dll and VSPowerTools.AddIn to the Addins directory of Visual Studio 2008 (by default, it’s C:\Users\Username\Documents\Visual Studio 2008\Addins)
Here is a list of the thing that I’m planned to do for this addin:
- Use M-V-VM pattern for the WPF part
- Allow user to select if he want C# or VB.NET generated code
- Manage the “Add Reference” part to support Silverlight (currently, the addin adds, as reference, the assemblies for the .NET Framework only, not Silverlight)
- Maybe use a package instead of a simple addin
Here is the link to download the addin: http://morpheus.developpez.com/wpf/tools/addins/VSPowerTools1.0.zip
If you have any ideas or comments about this addin, feel free to contact me or to post a comment !
Happy coding !
I encountered a bug when I tried to use the control SaveFileDialog, available in Silverlight 3. Indeed, every time I used the method OpenFile, I received the following exception:
IOException: The directory name is invalid
At first, I tought it was an issue with the permissions on the location where I tried to save the file but, after posting a message on the Silverlight’s forums, it’s appears the issue come from the Protected Mode that was activated, in Internet Explorer, for the Local Intranet Zone:
"IE -> Tools -> Internet Options -> Security –> Enable/Disable Protected Mode"
As soon as I deactivate the protected mode, the call to the method OpenFile worked fine ! Of course, it’s a temporary fix because it’s not recommanded to keep the protected mode deactivated.
Happy coding !
Bye
Settings are a very powerful feature of .NET applications that allows developers to store some values in the settings of the application:
If you want to know how to bind a control to a property defined in the settings, it’s pretty simple. First, you need to add a custom XML namespace that will design the namespace where the settings are defined:
xmlns:properties="clr-namespace:TestSettings.Properties"
Then, in your XAML file, access the property using the following syntax:
x:Static properties:Settings.Default
So here is the final result code:
<ListBox x:Name="lb"
ItemsSource="{Binding Source={x:Static properties:Settings.Default}, Path=Names}" />
That’s all :)
Happy coding !
Bye.
Here is another request that a friend of mine asked me recently. To achieve this, I’ve just create a custom control that contains a TextBlock and a TextBox (which is hidden) controls.
When user double clic on the TextBlock, the Textbox appears.
So here is my proposition: in the file generic.xaml, put this:
<Style TargetType="{x:Type local:EditableTextBlock}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:EditableTextBlock}">
<Grid x:Name="PART_GridContainer"
Background="{TemplateBinding Background}"
Width="{TemplateBinding Width}"
Height="{TemplateBinding Height}">
<TextBlock x:Name="PART_TbDisplayText"
Visibility="Visible"
Background="{Binding Mode=TwoWay, Path=TextBlockBackgroundColor, RelativeSource={RelativeSource AncestorType={x:Type local:EditableTextBlock}}}"
Foreground="{Binding Mode=TwoWay, Path=TextBlockForegroundColor, RelativeSource={RelativeSource AncestorType={x:Type local:EditableTextBlock}}}"
Text="{Binding Mode=TwoWay, Path=Text, RelativeSource={RelativeSource AncestorType={x:Type local:EditableTextBlock}}}" />
<TextBox x:Name="PART_TbEditText"
Visibility="Hidden"
Background="{Binding Mode=TwoWay, Path=TextBoxBackgroundColor, RelativeSource={RelativeSource AncestorType={x:Type local:EditableTextBlock}}}"
Foreground="{Binding Mode=TwoWay, Path=TextBoxForegroundColor, RelativeSource={RelativeSource AncestorType={x:Type local:EditableTextBlock}}}"
Text="{Binding Mode=TwoWay, Path=Text, RelativeSource={RelativeSource AncestorType={x:Type local:EditableTextBlock}}}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Then, here is the content that should go in the behind file:
[TemplatePart(Type = typeof(Grid), Name = EditableTextBlock.GRID_NAME)]
[TemplatePart(Type = typeof(TextBlock), Name = EditableTextBlock.TEXTBLOCK_DISPLAYTEXT_NAME)]
[TemplatePart(Type = typeof(TextBox), Name = EditableTextBlock.TEXTBOX_EDITTEXT_NAME)]
public class EditableTextBlock : Control
{
#region Constants
private const string GRID_NAME = "PART_GridContainer";
private const string TEXTBLOCK_DISPLAYTEXT_NAME = "PART_TbDisplayText";
private const string TEXTBOX_EDITTEXT_NAME = "PART_TbEditText";
#endregion
#region Member Fields
private Grid m_GridContainer;
private TextBlock m_TextBlockDisplayText;
private TextBox m_TextBoxEditText;
#endregion
#region Dependency Properties
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(EditableTextBlock), new UIPropertyMetadata(string.Empty));
public Brush TextBlockForegroundColor
{
get { return (Brush)GetValue(TextBlockForegroundColorProperty); }
set { SetValue(TextBlockForegroundColorProperty, value); }
}
public static readonly DependencyProperty TextBlockForegroundColorProperty = DependencyProperty.Register("TextBlockForegroundColor", typeof(Brush), typeof(EditableTextBlock), new UIPropertyMetadata(null));
public Brush TextBlockBackgroundColor
{
get { return (Brush)GetValue(TextBlockBackgroundColorProperty); }
set { SetValue(TextBlockBackgroundColorProperty, value); }
}
public static readonly DependencyProperty TextBlockBackgroundColorProperty = DependencyProperty.Register("TextBlockBackgroundColor", typeof(Brush), typeof(EditableTextBlock), new UIPropertyMetadata(null));
public Brush TextBoxForegroundColor
{
get { return (Brush)GetValue(TextBoxForegroundColorProperty); }
set { SetValue(TextBoxForegroundColorProperty, value); }
}
public static readonly DependencyProperty TextBoxForegroundColorProperty = DependencyProperty.Register("TextBoxForegroundColor", typeof(Brush), typeof(EditableTextBlock), new UIPropertyMetadata(null));
public Brush TextBoxBackgroundColor
{
get { return (Brush)GetValue(TextBoxBackgroundColorProperty); }
set { SetValue(TextBoxBackgroundColorProperty, value); }
}
public static readonly DependencyProperty TextBoxBackgroundColorProperty = DependencyProperty.Register("TextBoxBackgroundColor", typeof(Brush), typeof(EditableTextBlock), new UIPropertyMetadata(null));
#endregion
#region Constructor
static EditableTextBlock()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(EditableTextBlock), new FrameworkPropertyMetadata(typeof(EditableTextBlock)));
}
#endregion
#region Overrides Methods
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
this.m_GridContainer = this.Template.FindName(GRID_NAME, this) as Grid;
if(this.m_GridContainer != null)
{
this.m_TextBlockDisplayText = this.m_GridContainer.Children[0] as TextBlock;
this.m_TextBoxEditText = this.m_GridContainer.Children[1] as TextBox;
this.m_TextBoxEditText.LostFocus += this.OnTextBoxLostFocus;
}
}
protected override void OnMouseDoubleClick(MouseButtonEventArgs e)
{
base.OnMouseDoubleClick(e);
this.m_TextBlockDisplayText.Visibility = Visibility.Hidden;
this.m_TextBoxEditText.Visibility = Visibility.Visible;
}
#endregion
#region Event Handlers
private void OnTextBoxLostFocus(object sender, RoutedEventArgs e)
{
this.m_TextBlockDisplayText.Visibility = Visibility.Visible;
this.m_TextBoxEditText.Visibility = Visibility.Hidden;
}
#endregion
}
As you can see, it’s pretty simple but very useful :)
Happy coding !
Bye.
Version 3 of Expression Blend, which is available since last week, comes with a lot of new features. In this features, we can find Intellisense in XAML files:
So it’s just a real please for people who, like me, modify the XAML code: no need to open Visual Studio to have Intellisense !
But, we can also see another interesting point: the code editor (which was present in the very first Beta of Expression Blend and disappear then) now is again available, with Intellisense on C# or VB.NET files:
For sure, it’s not a good idea to develop an application using only Expression Blend (don’t fordet that you cannot have breakpoints…) but for doing some tests or prototypes, it’s really nice !
Happy (Silverlight & WPF) coding !
Bye.
To help new developers, here is a sample WPF application (that use the Northwind database) developed by using the pattern MVVM (Model View ViewModel).
The goal of this little application is just to give another example that will help to understand this new pattern that is really powerful !
I will try to make some blog posts about this pattern quickly. In the meantime, just enjoy this :)
The application can be downloaded here: http://morpheus.developpez.com/wpf/tools/DemoMVVM.zip
Bye.
When working on WPF projects, it’s mandatory to assign resources to user interface controls. When you work in XAML, it’s pretty simple: you just need to use the MarkupExtension named StaticResource (or DynamicResource if the resource is going to be modified):
<Button Content="Find Position" Click="Button_Click" Background="{DynamicResource brush}" />
But, how to do the same using code-behind ? The key is to use the method SetResourceReference (http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.setresourcereference.aspx):
<Window.Resources>
<SolidColorBrush x:Key="brush" Color="Red" />
</Window.Resources>
<Button x:Name="btn"Content="Find Position" Click="Button_Click" />
this.btn.SetResourceReference(BackgroundProperty, "brush");
As you can see, it’s really simple to use: you define the resource, you define the control and, in code behind, you call the method SetResourceReference and use the following parameters:
- the DependencyProperty on which the resource will be applied
- the name of the resource
Happy coding !
Here is a question that a friend of mine asked me recently. Indeed, as a beginner with WPF, it thought that setting the property IsEnabled = false, on the MenuItem, will disable it. So, the following code:
<MenuItem Header="Edit">
<MenuItem x:Name="miPaste"
Header="Paste" IsEnabled="False" >
<MenuItem.Icon>
<Image Source="pack://application:,,,/Images/Paste.png"
/>
</MenuItem.Icon>
</MenuItem>
</MenuItem>
Does not deactivate the image, as you can see here:
To correct this, you have 2 choices:
- Use a second gray image which will be specified as the source for the control if this one is deactivated
- Use the class FormatConvertedBitmap to create a gray image
So I’ve created a little class, AutoGreyableImage, which allow you to have an image that will be turn in gray automatically when the control is desactivated.
Here is how you can use it:
<MenuItem Header="Edit">
<MenuItem x:Name="miPaste"
Header="Paste">
<MenuItem.Icon>
<local:AutoGreyableImage Source="pack://application:,,,/Images/Paste.png"
/>
</MenuItem.Icon>
</MenuItem>
</MenuItem>
And here is the implementation:
/// <summary>
/// Class used to have an image that is able to be gray when the control is not enabled.
/// Author: Thomas LEBRUN (http://blogs.developpeur.org/tom)
/// </summary>
public class AutoGreyableImage : Image
{
/// <summary>
/// Initializes a new instance of the <see cref="AutoGreyableImage"/> class.
/// </summary>
static AutoGreyableImage()
{
// Override the metadata of the IsEnabled property.
IsEnabledProperty.OverrideMetadata(typeof(AutoGreyableImage), new FrameworkPropertyMetadata(true, new PropertyChangedCallback(OnAutoGreyScaleImageIsEnabledPropertyChanged)));
}
/// <summary>
/// Called when [auto grey scale image is enabled property changed].
/// </summary>
/// <param name="source">The source.</param>
/// <param name="args">The <see cref="System.Windows.DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
private static void OnAutoGreyScaleImageIsEnabledPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs args)
{
var autoGreyScaleImg = source as AutoGreyableImage;
var isEnable = Convert.ToBoolean(args.NewValue);
if (autoGreyScaleImg != null)
{
if (!isEnable)
{
// Get the source bitmap
var bitmapImage = new BitmapImage(new Uri(autoGreyScaleImg.Source.ToString()));
// Convert it to Gray
autoGreyScaleImg.Source = new FormatConvertedBitmap(bitmapImage, PixelFormats.Gray32Float, null, 0);
// Create Opacity Mask for greyscale image as FormatConvertedBitmap does not keep transparency info
autoGreyScaleImg.OpacityMask = new ImageBrush(bitmapImage);
}
else
{
// Set the Source property to the original value.
autoGreyScaleImg.Source = ((FormatConvertedBitmap) autoGreyScaleImg.Source).Source;
// Reset the Opcity Mask
autoGreyScaleImg.OpacityMask = null;
}
}
}
}
Here is the result:
You can download the sources (and the demo) here: http://morpheus.developpez.com/wpf/DisableMenuItemIcon.zip/wpf/DisableMenuItemIcon.zip
Happy coding !
More Posts
Next page »