Skip to content

Jeremy Alles Presentation Foundation: WPF, .Net and modern software development
Syndicate content
Jeremy Alles Presentation Foundation: WPF, .Net and modern software development
Updated: 23 min 53 sec ago

[WP7] Sound effect in a Silverlight Windows Phone 7 application

Thu, 08/12/2010 - 09:18

A coworker and I are currently working on a simple Silverlight game for the Windows Phone 7 platform. In order to give some feedback to our end-user, we decided to add sound effects. Here is a very short post about how we did that.

The first thing is to reference the Microsoft.Xna.Framework assembly in your project. This assembly is needed to access the low-level sound component of XNA right from your Silverlight application. You also need to have your sound effect in a WAVE format file.

Then we created a simple action (from the Blend Behavior toolkit) which is a TargetedTriggerAction:

?View Code CSHARP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class SoundEffectAction : TargetedTriggerAction<FrameworkElement>
{
    public string Source
    {
        get { return (string)GetValue(SourceProperty); }
        set { SetValue(SourceProperty, value); }
    }
 
    public static readonly DependencyProperty SourceProperty = DependencyProperty.Register(
        "Source", 
        typeof(string), 
        typeof(SoundEffectAction), 
        new PropertyMetadata(string.Empty));
 
    protected override void Invoke(object parameter)
    {
        if(!string.IsNullOrEmpty(this.Source)
        {
            var stream = TitleContainer.OpenStream(this.Source);
            if (stream != null)
            {
                var effect = SoundEffect.FromStream(stream);
                FrameworkDispatcher.Update();
                effect.Play();
            }
        }
    }
}

Using this action, we’re able to wire sound effect right in Blend which produces the following XAML code:

?View Code XML
1
2
3
4
5
6
7
8
<Grid Margin="2">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="MouseLeftButtonUp">
            <Actions:SoundEffectAction Source="Resources/Sounds/Click.wav" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
    <!-- rest of the xaml... -->
</Grid>

Simple, isn’t it ?

Categories: Blogs

Attributes-based validation in a WPF MVVM application

Thu, 07/29/2010 - 08:29

Today, I’m proud to share with you my very first article available on CodeProject. This article presents a technique which can be used in order to add validation in a WPF MVVM application based on attribute. Basically, it means that you can write validation logic like that (notice the attribute associated to this property):

?View Code CSHARP
1
2
3
4
5
6
7
8
9
10
11
12
13
[Required(ErrorMessage = "Field 'FirstName' is required.")]
public string FirstName
{
    get
    {
        return this.firstName;
    }
    set
    {
        this.firstName = value;
        this.OnPropertyChanged("FirstName");
    }
}

Of course the article comes with a nice demo application:

You can read the full article here: Attributes-based validation in a WPF MVVM application


Categories: Blogs

MVVM Frameworks Explorer updated

Mon, 07/19/2010 - 19:44

Today I’m releasing a new version of my MVVM Frameworks Explorer application. You can find the updated version here: http://www.japf.fr/silverlight/mvvm/index.html

Here is a list of the changes in this new version:

  • application updated to Silverlight 4
  • support is now detailed for WPF, Silverlight and Windows Phone
  • new frameworks added (MEFedMVVM…)
  • framework’s logo added
  • download count added (based on the information I found on CodePlex website)
  • about window

As always, feel free to give feedback :-)

Categories: Blogs

Where does the default TwoWay binding comes from ?

Thu, 07/15/2010 - 12:50

I got a comment on my post about a very simple MVVM application about the fact that removing the TwoWay mode on a binding did not change the behavior of the application. This is a quick occasion for me to share a quick explanation about this.

Actually and as you already know if you can write XAML like Text={Binding …} it is only because Text is a Dependency Property. Also, dependency properties are defined in a static way (so that if you have 50 textboxes you don’t have to instantiate 50 times the Text property). The default behavior for the mode of the binding (TwoWay, OneWay, etc.) can be found in the static declaration of the dependency property. For example, in the case of the Text property of the TextBox we have:

?View Code CSHARP
1
2
3
4
5
6
7
8
9
10
11
12
TextProperty = DependencyProperty.Register(
	"Text", 
	typeof(string), 
	typeof(TextBox), 
	new FrameworkPropertyMetadata(
		string.Empty, 
		FrameworkPropertyMetadataOptions.Journal 
		| FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
	new PropertyChangedCallback(TextBox.OnTextPropertyChanged), 
	new CoerceValueCallback(TextBox.CoerceText),
	true, 
	UpdateSourceTrigger.LostFocus));

The interesting part here if of course the BindsTwoWayByDefault option. Note that this is the only default option available (we can’t have a OneWayToSource binding by default).

Categories: Blogs

[WP7] Beta build of the Windows Phone 7 tools is available

Mon, 07/12/2010 - 22:40

I guess many Windows Phone 7 developers have been waiting for this news since many weeks. It’s finally official: the Beta build of the Windows Phone 7 tools is out. If you want to download the new version here is the link.

This new version has been released during WPC (Microsoft Worldwide Partner Conference). For more information, please check-out:

  • the post on the Windows Phone Developer Tools blog.
  • this post by Jaime Rodriguez about all breaking change between the April CTP Refresh and the Beta build
  • this post by the Blend team about the new features available in Blend
  • the release note

Here are a summary of the changes in this new version:

  • Despite HW acceleration effects have been removed from the platform (DropShadow and Blur effects are now no-op). This feature might come back later.
  • Compatibility with Blend 4 RTM
  • API near final
  • Various fixes

Note that panorama and pivot controls are coming in the next weeks… Grab you copy now and enjoy Windows Phone 7 development :-)

http://www.microsoft.com/downloads/details.aspx?FamilyID=c8496c2a-54d9-4b11-9491-a1bfaf32f2e3&displaylang=en
Categories: Blogs

MVP in Client Application Development !

Fri, 07/09/2010 - 08:12

A couple of days ago I received an email telling me I got the MVP Award for 2010 ! I’ve been waiting a little bit before I wrote this post because I invited my coworkers at home in order to celebrate this yesterday and I wanted to keep the surprise :D

This is the very first year I got the MVP award. I’m very proud to have this recognition from Microsoft and it is giving me new ideas for the future. I started this blog almost 2 years ago now and I’d never imagine I’d get so much feedback about it. My MVP award is also recognizing my work in the French community (on the developpez.com website) or for conferences I’m giving in my daily job.

The MVP award will be also for me the occasion to have pre-release of Microsoft software (but I’ll not be able to publicly give information about them since I’ve just signed an NDA) and also to enjoy a free MSDN-Premium subscription !

For more information about the MVP Award Program you can check-out:

Thanks to all my readers also who are giving me feedback and support.  I’d also like to thank my co-workers who are helpful in my daily job. I’m very lucky to be part of this very nice team. By reading posts before they go public (Charlotte, Fred !) or discussion about software programming in general (the whole Pythagore group!) I get cool ideas about things I want to share. So thank you every one for supporting me :-)

Categories: Blogs

[WP7] Windows Phone 7 challenge for french readers !

Mon, 06/14/2010 - 14:49

A couple of months ago, the french programming website www.developpez.com organized an event to discover Windows Azure programming (I wrote a blog post about it here).

A similar event has just been launched for Windows Phone 7 development at challenge-windowsphone7.developpez.com

(tr: “Let’s go !” “World cup ?” “No… Windows Phone 7 challenge by developpez.com !”)

The challenge is made of 6 steps:

  1. Tools : download and install the required tools
  2. Quizz : first basic quizz
  3. Silverlight development
  4. Silverlight and push notifications
  5. XNA
  6. Quizz : advanced quizz

Each winner will have the following gifts:

This kind of challenge is really helpful to discover a new technology the funny way ! I hope I’ll get my “I Love Windows Phone” tee-shirt to wear it this summer :-)

Categories: Blogs

[WP7] Using the camera in the emulator

Wed, 05/05/2010 - 23:11

In the very first release of the SDK for Windows Phone 7 development, it was not possible to use the camera in the emulator. The latest version of the SDK fixes this problem.

Windows Phone 7 SDK comes with a set of Task (in the Microsoft.Phone.Tasks namespace). A task can be launched from your application in order to perform some work. Currently available tasks are:

In order to launch a task from your application, all you need to do is to instantiate the associated type and call the Show() method.

Here is a sample code which launchs the StartCameraTask and then gets the capture images in order to use it in a standard Silverlight Image control:

?View Code CSHARP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// launch the camera capture when the user touch the screen
this.MouseLeftButtonUp += (s, e) => new CameraCaptureTask().Show();
 
// this static event is raised when a task completes its job
ChooserListener.ChooserCompleted += (s, e) =>
{
    var taskEventArgs = (TaskEventArgs<PhotoResult>)e;
    var photoStream = taskEventArgs.Result.ChosenPhoto;
 
    var bitmapImage = new BitmapImage();
    bitmapImage.SetSource(photoStream);
 
    this.image.Source = bitmapImage;
};

The image is just a standard Silverlight Image control:

?View Code XML
1
<Image x:Name="image"/>

The emulator while the task is running:

The captured image (shown once the task has completed):

If you haven’t download the tool already, go ahead and grab them ! Everything is available for free in a single download at http://developer.windowsphone.com/windows-phone-7-series/.

Hope this helps :-)

Note: even though the StartCameraTask is now working, this is not yet the case for all the tasks…

Categories: Blogs

[WP7] Bug when using NavigationService in Windows Phone 7

Mon, 05/03/2010 - 21:45

The last couple of days, I’m playing with my favourite tools in order to build a simple WP7 demo application. I just encountered a weird problem which I wanted to share here… I’ll update this article as soon as I’ll get some feedback from Microsoft about this issue.

Note: this problem did not occur if you’re using the first CTP of the WP7 tools

To reproduce the bug:

  • Create a new Windows Phone 7 application in VS2010
  • Add a new page (use the default name: Page1)
  • In the MainPage, add the following XAML code:
?View Code XML
1
2
3
<ListBox>
  <Button MouseLeftButtonDown="Handler"/>
</ListBox>
  • In the code-behind, add the following handler:
?View Code CSHARP
1
2
3
4
private void Handler(object sender, MouseButtonEventArgs mouseButtonEventArgs)
{
    this.NavigationService.Navigate(new Uri("/Page1.xaml", UriKind.Relative));
}
  • Run the application and click on the TextBlock
  • You’ll get an ArgumentException with the following StackTrace:

at MS.Internal.XcpImports.MethodEx(IntPtr ptr, String name, CValue[] cvData)
at MS.Internal.XcpImports.MethodPack(IntPtr objectPtr, String methodName, Object[] rawData)
at MS.Internal.XcpImports.UIElement_TransformToVisual(UIElement element, UIElement visual)
at System.Windows.UIElement.TransformToVisual(UIElement visual)
at System.Windows.Controls.ScrollViewer.OnManipulationStarted(ManipulationStartedEventArgs e)
at System.Windows.Controls.Control.OnManipulationStarted(Control ctrl, EventArgs e)
at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName)

Analysis:

It took me some time to reproduce this problem in a very simple application. At the very beginning, I though it has something do to with the EventToCommand behavior I was using (from the famous MVVM-Light framework of Laurent Bugnion) but after talking with Laurent it was clear it wasn’t the case.

The StackTrace seems to indicate a problem with the ScrollViewer of the ListBox…

Workaround:

Several possibilities seems to be working:

  1. Change the ListBox to an ItemsControl
  2. Or, change the event to ManipulationCompleted

I didn’t find the correct location in order to log this issue on Microsoft Connect. Pleas let me know if you have the URL

Categories: Blogs

R# can create resources for you in XAML

Wed, 04/28/2010 - 13:33

I was aware for some time now that R# offers some support for editing XAML but I didn’t know the following features until recently.When you create a StaticResource in XAML, R# is able to help you by generating some code for you. The famous R# “bubble” shows up offering various options to create the resource:

Then the resource is automatically created for you:

Note that it works with converter too:

R# 5.0 has been released a couple of weeks ago. Go ahead and grab your copy !

Categories: Blogs