Nick's .NET Travels

Continually looking for the yellow brick road so I can catch me a wizard....

Vista, and the Device and Cellular Emulators

Today I have been doing some work with the Windows Mobile notification broker, which, for those who haven't used it is basically the event bus that Windows Mobile has for notifying applications when system properties have changed. For example in my application I can register an event handler for when the system state PhoneIncomingCall changes.  This is useful as it means that my application can respond to device based events, instead of just the typical user interaction. 

Unfortunately one of the issues with doing this type of development is how do you test your code.  I could simply call my real device but that might get expense.  Alternatively the Windows Mobile SDK now includes a cellular emulator which you can use in conjunction with the device emulators to simulate phone calls, messages and other connectivity.

Now I don't want to provide another How To article on the cellular emulator as there are a couple out there including a particularly useful one by Wei Meng Lee on getting started and a similar one by fellow Aussie, Dave Glover. However what I want to briefly point out is one of the issues I faced today in getting it to work.  These two articles are both great starting points, however when I attempted to restart the emulator (after adding the peripheral COM port) all I would get is the black screen of death (equivalent of the blue screen for desktop machines - basically the device emulator fails to restart properly).

The fix to this issue, as indicated in the first of the common problems discussed by Wei, is to disable the bluetooth services.  Luckily you don't actually need to go to the great pain of reinstalling the Windows Mobile SDK.  Here's what you need to do:

  • Stop the Bluetooth Support Service - As I don't need the Bluetooth service at all I've elected to stop it and set the Startup Type to Manual.  Apparently once you have the cellular emulator working you can restart this service without causing any issues.

image

  • Reinstall the  XPVCom device driver - From an administrator command line issue the following commands:
  • C:\Program Files\Windows Mobile 6 SDK\Tools\Cellular Emulator>InstallXPVCom.exe uninstall

    C:\Program Files\Windows Mobile 6 SDK\Tools\Cellular Emulator>InstallXPVCom.exe install

  • Do a soft reset of your emulator - File -> Reset -> Soft

Once you have done these steps (in addition to those steps provided by Wei) you should be able to initiate a call from the cellular emulator and send sms messages to the device emulator.  Note that you don't need to have the emulator cradled for this to work since the cellular emulator talks directly to the device emulator.  This is particularly useful in light of the ongoing issues with both ActiveSync and the Windows Mobile Device Center...... still waiting for a reliable tool Microsoft!!!

Comments are closed
Adding WPF Client with Azure Active Directory Authentication and Azure Mobile Service

Nick's .NET Travels

Continually looking for the yellow brick road so I can catch me a wizard....

Adding WPF Client with Azure Active Directory Authentication and Azure Mobile Service

In today’s post I was going to cover adding Azure AD authentication to my iOS project but I don’t have my Mac build machine handy so there’s a bit of a change of plans. Today I’m going to add a WPF desktop application to my solution and catch it up to where the other client applications are up to. I’ll start by creating the WPF Application.

image

At this point I realised that I missed setting the framework in the Add New Project dialog, so I opened the Properties pane of the application and on the Application tab I set the Target framework to .Net Framework 4.5.

image

Next we need to add references to the following solution projects:

- RealEstateInspector.Core

- RealEstateInspector.Shared.Client

And then add reference to both the ADAL and Mobile Service SqliteStore packages. From the downloads page on sqlite.org you’ll also need to download the “32-bit DLL (x86) for SQLite” which is under the Precompiled Binaries for Windows heading. Unblock and Extract the zip file and add the sqlite3.dll file to the new WPF project, setting the Build Action to Content and setting the Copy to Output Directory to Copy always.

I also need to check the Prefer 32-bit checkbox and define the DESKTOP compilation symbol for All Configurations.

image

I’ll add the following XAML markup to the MainWindow.xaml

<Window x:Class="RealEstateInspector.Desktop.MainWindow"
        xmlns="
http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.Resources>
            <DataTemplate
                x:Key="PropertyItemTemplate">
                <TextBlock
                    Text="{Binding Address}"
                    FontSize="30"
                    Foreground="WhiteSmoke" />
            </DataTemplate>
        </Grid.Resources>
        <Grid.RowDefinitions>
            <RowDefinition
                Height="Auto" />
            <RowDefinition />
        </Grid.RowDefinitions>
        <Button
            Content="Authenticate"
            Click="AuthenticateClick" />
        <ListView
            ItemTemplate="{StaticResource PropertyItemTemplate}"
            Grid.Row="1"
            ItemsSource="{Binding Properties}" />
    </Grid>
</Window>

And the following code to the MainWindow.xaml.cs

public partial class MainWindow : IWin32Window
{
    public IntPtr Handle
    {
        get
        {
            var interopHelper = new WindowInteropHelper(this);
            return interopHelper.Handle;
        }
    }
    public MainWindow()
    {
        InitializeComponent();

        Loaded += MainWindow_Loaded;
    }
    public MainViewModel CurrentViewModel
    {
        get { return DataContext as MainViewModel; }
    }
    void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        var vm = new MainViewModel();
        DataContext = vm;
    }

    private async void AuthenticateClick(object sender, RoutedEventArgs e)
    {
        var token = await AuthenticationHelper.Authenticate(Handle);
        await CurrentViewModel.LoadPropertyData(token);
    }
}

I also had to make some minor changes to the AuthenticationHelper

public static class AuthenticationHelper
{

    public static async Task<string> Authenticate(
#if DROID
    Android.App.Activity callerActivity
#elif DESKTOP
IntPtr callerHandle
#endif
        )
    {
        try
        {
            var authContext = new AuthenticationContext(Constants.ADAuthority);
#if !SILVERLIGHT
            if (authContext.TokenCache.ReadItems().Count() > 0)
                authContext = new AuthenticationContext(authContext.TokenCache.ReadItems().First().Authority);
#endif
            var authResult =
                await
                    authContext.AcquireTokenAsync(Constants.MobileServiceAppIdUri,
                    Constants.ADNativeClientApplicationClientId,
                    new Uri(Constants.ADRedirectUri),
#if WINDOWS_PHONE_APP || SILVERLIGHT
                    new AuthorizationParameters()
#elif DROID
                    new AuthorizationParameters(callerActivity)
#elif DESKTOP
                        new AuthorizationParameters(PromptBehavior.Auto, callerHandle)
#else
                        new AuthorizationParameters(PromptBehavior.Auto, false)
#endif
                    );
            Debug.WriteLine(authResult != null);

            return authResult.AccessToken;

        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex.Message);
            return null;
        }
    }
}

And that’s it, the WPF desktop application can be run up, the user can sign in and properties are synchronized before being displayed.

Pingbacks and trackbacks (1)+

Comments are closed