Prism 4.1 February – 2012 for Windows Phone 7.1 Mango


The Prism toolkit 4.1 February -2012 release is available for Windows Phone Mango . On installing Prism_Source.exe  it will integrate WPF 4.0 for Desktop , Silverlight 5 , Windows Phone 7.1 Mango apps library side by side. Prism 4.1 has greater extensibility support over RIA on WPF 4.0, Silverlight 5 toolkit & Windows Phone.

  • To download the Latest Prism library 4.1 for WPF 4.0, Silverlight 5 & WP7  

             Click Here.

Now extract the Prism Source Library & open the folder named PrismLibrary & open the Phone profile to get the Prism 4.1 interactivity      modules for Windows Phone.

  • Run the project in VS 2010 with .NET framework 4.0 & check out latest prism namespaces module for WP7 named as
  1. Microsoft.Practices.Prism Namepace
  2. Microsoft.Practices.Prism.Commands Namespace
  3. Microsoft.Practices.Prism.Events Namespace
  4. Microsoft.Practices.Prism.Interactivity Namepace
  5. Microsoft.Practices.Prism.Interactivity.InteractionRequest Namespace
  6. Microsoft.Practices.Prism.ViewModel Namespace.

  • Set Prism.Interactivity & Prism.Interactivity.Tests as a Startup Project & debug the app in Windows Phone Mango emulator.

  • Move into details screen of event handler of prism library for Windows Phone Mango.

  • Check the Prism.Interactivity Modules for Windows Phone.

  • The Prism library 4.1 also includes Unit Testing framework support for Windows Phone Mango.

Scheduling Background Agents in Windows Phone 7.1 Mango


Scheduled Tasks & Background Agents allows apps to run in background in Windows Phone while the app is not running in foreground. There are two types of Scheduled Tasks referenced called Periodic Tasks & ResourceIntensive Tasks which helps to implement background agent scaling.

  • Periodic Tasks: Periodic agents run for a small amount of time on a regular recurring interval. Typical scenarios for this type of task include uploading the device’s location and performing small amounts of data synchronization.
  • Resource-Intensive Tasks: This tasks works for long period of time when the phone meets a set of requirements relating to processor activity, power source, and network connection. A typical scenario for this type of task is synchronizing large amounts of data to the phone while it is not being actively used by the user.
  • The Background  Agent LifeCycle for Windows Phone:  The app can have at least of one type of Scheduled tasks either Periodic Tasks or Scheduled Tasks or even both. So the schedule on which the agent runs depends on which type of task it is registered as.

There are some constraints features on both of Periodic Tasks & Resource-Intensive Tasks . For details Click here.

  • Implement Background Tasks for Windows Phone 7.1 Mango : In order to implement it you need to a Windows Phone application project in VS 2010 SP1 from Silverlight for Windows Phone template.

  • Add a new Schedule Task agent Project to add resources for Schedule agent tasks in Windows Phone app.

  • In order to access the Schedule Task apps in Foreground app , we need to add reference of ScheduleTaskAgent1 Project in ScheduledTask project by clicking on add reference-> Project dialog.

  • Now, add the following code in ScheduleAgent.cs file in the ScheduleTaskAgent1 project.

#define

DEBUG_AGENT

using  System.Windows;

using Microsoft.Phone.Scheduler;

using  Microsoft.Phone.Scheduler;

using  Microsoft.Phone.Shell;

using  System;

namespace ScheduledTaskAgent1

{

public class ScheduledAgent : ScheduledTaskAgent

{

private static volatile bool _classInitialized;

///<remarks>

/// ScheduledAgent constructor, initializes the UnhandledException handler

///</remarks>

public ScheduledAgent()

{

if (!_classInitialized)

{

_classInitialized = true;

// Subscribe to the managed exception handler

Deployment.Current.Dispatcher.BeginInvoke(delegate

{

Application.Current.UnhandledException += ScheduledAgent_UnhandledException;

});

}

}

/// Code to execute on Unhandled Exceptions

private void ScheduledAgent_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)

{

if (System.Diagnostics.Debugger.IsAttached)

{

// An unhandled exception has occurred; break into the debugger

System.Diagnostics.

Debugger.Break();

}

}

///<summary>

/// Agent that runs a scheduled task

///</summary>

///<param name=”task”>

/// The invoked task

///</param>

///<remarks>

/// This method is called when a periodic or resource intensive task is invoked

///</remarks>

protected overridevoid OnInvoke(ScheduledTask task)

{

//TODO: Add code to perform your task in background

string toastMessage = “Hello WP7”;

if (task isPeriodicTask)

{

toastMessage = “Periodic task running.”;

}

else

{

toastMessage = “Resource-intensive task running.”;

}

// The toast will not be shown if the foreground application is running.

ShellToast toast = newShellToast();

toast.Title = “Background Agent Sample”;

toast.Content = toastMessage;

toast.Show();

#if

DEBUG_AGENT

ScheduledActionService.LaunchForTest(task.Name, TimeSpan.FromSeconds(60));

#endif

// Call NotifyComplete to let the system know is done working .

NotifyComplete();

}

}

}

  • So, Next to build the Forgeound App Project , so move to MainPage.xaml in ScheduleTask Project of Windows Phone.

MainPage.Xaml:  Modify the code inside the Grid name called ContentPanel.

<Grid x:Name=”ContentPanel” Grid.Row=”1″ Margin=”12,0,12,0″>

<StackPanel>

<StackPanel Orientation=”Vertical” Name=”PeriodicStackPanel” Margin=”0,0,0,40″>

<TextBlock Text=”Periodic Agent” Style=”{StaticResource PhoneTextTitle2Style}” />

<StackPanel Orientation=”Horizontal”>

<TextBlock Text=”name:” Style=”{StaticResource PhoneTextAccentStyle}” />

<TextBlock Text=”{Binding Name}” />

</StackPanel>

<StackPanel Orientation=”Horizontal”>

<TextBlock Text=”is enabled” VerticalAlignment=”Center” Style=”{StaticResource PhoneTextAccentStyle}” />

<CheckBox Name=”PeriodicCheckBox” IsChecked=”{Binding IsEnabled}” Checked=”PeriodicCheckBox_Checked” Unchecked=”PeriodicCheckBox_Unchecked” />

</StackPanel>

<StackPanel Orientation=”Horizontal”>

<TextBlock Text=”is scheduled:” Style=”{StaticResource PhoneTextAccentStyle}” />

<TextBlock Text=”{Binding IsScheduled}” />

</StackPanel>

<StackPanel Orientation=”Horizontal”>

<TextBlock Text=”last scheduled time:” Style=”{StaticResource PhoneTextAccentStyle}” />

<TextBlock Text=”{Binding LastScheduledTime}” />

</StackPanel>

<StackPanel Orientation=”Horizontal”>

<TextBlock Text=”expiration time:” Style=”{StaticResource PhoneTextAccentStyle}” />

<TextBlock Text=”{Binding ExpirationTime}” />

</StackPanel>

<StackPanel Orientation=”Horizontal”>

<TextBlock Text=”last exit reason:” Style=”{StaticResource PhoneTextAccentStyle}” />

<TextBlock Text=”{Binding LastExitReason}” />

</StackPanel>

</StackPanel>

<StackPanel Orientation=”Vertical” Name=”ResourceIntensiveStackPanel” Margin=”0,0,0,40″>

<TextBlock Text=”Resource-intensive Agent” Style=”{StaticResource PhoneTextTitle2Style}” />

<StackPanel Orientation=”Horizontal”>

<TextBlock Text=”name:” Style=”{StaticResource PhoneTextAccentStyle}” />

<TextBlock Text=”{Binding Name}” />

</StackPanel>

<StackPanel Orientation=”Horizontal”>

<TextBlock Text=”is enabled” VerticalAlignment=”Center” Style=”{StaticResource PhoneTextAccentStyle}” />

<CheckBox Name=”ResourceIntensiveCheckBox” IsChecked=”{Binding IsEnabled}” Checked=”ResourceIntensiveCheckBox_Checked” Unchecked=”ResourceIntensiveCheckBox_Unchecked” />

</StackPanel>

<StackPanel Orientation=”Horizontal”>

<TextBlock Text=”is scheduled:” Style=”{StaticResource PhoneTextAccentStyle}” />

<TextBlock Text=”{Binding IsScheduled}” />

</StackPanel>

<StackPanel Orientation=”Horizontal”>

<TextBlock Text=”last scheduled time:” Style=”{StaticResource PhoneTextAccentStyle}” />

<TextBlock Text=”{Binding LastScheduledTime}” />

</StackPanel>

<StackPanel Orientation=”Horizontal”>

<TextBlock Text=”expiration time:” Style=”{StaticResource PhoneTextAccentStyle}” />

<TextBlock Text=”{Binding ExpirationTime}” />

</StackPanel>

<StackPanel Orientation=”Horizontal”>

<TextBlock Text=”last exit reason:” Style=”{StaticResource PhoneTextAccentStyle}” />

<TextBlock Text=”{Binding LastExitReason}” />

</StackPanel>

</StackPanel>

</StackPanel>

</Grid>

</Grid>

  • Add the following code in MainPage.xaml.cs:

MainPage.xaml.cs:

#define DEBUG_AGENT

using System;

using  System.Collections.Generic;

using System.Linq;

using  System.Net;

using  System.Windows;

using  System.Windows.Controls;

using  System.Windows.Documents;

using  System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using  System.Windows.Shapes;

using  Microsoft.Phone.Controls;

using  Microsoft.Phone.Scheduler;

namespace ScheduledTask

{

public partial class MainPage : PhoneApplicationPage

{

PeriodicTask periodicTask;

ResourceIntensiveTask resourceIntensiveTask;

string periodicTaskName = “PeriodicAgent”;

string resourceIntensiveTaskName = “ResourceIntensiveAgent”;

public bool agentsAreEnabled = true;

// Constructor

public MainPage()

{

InitializeComponent();

}

private void StartPeriodicAgent()

{

agentsAreEnabled = true;

periodicTask = ScheduledActionService.Find(periodicTaskName) asPeriodicTask;

if (periodicTask != null)

{

RemoveAgent(periodicTaskName);

}

periodicTask = newPeriodicTask(periodicTaskName);

periodicTask = newPeriodicTask(periodicTaskName);

periodicTask.Description = “This demonstrates a periodic task.”;

// Place the call to add in a try block in case the user has disabled agents.

try

{

ScheduledActionService.Add(periodicTask);

PeriodicStackPanel.DataContext = periodicTask;

// If debugging is enabled , use LaunchForTest to launch the agent in one minutes

#if

(DEBUG_AGENT)

ScheduledActionService.LaunchForTest(periodicTaskName, TimeSpan.FromSeconds(60));

#endif

}

catch (InvalidOperationException exception)

{

if (exception.Message.Contains(“BNS Error: The action is disabled”))

{

MessageBox.Show(“Bakcground agents for this application have been disabled by the user.”);

agentsAreEnabled = false;

PeriodicCheckBox.IsChecked = false;

}

if (exception.Message.Contains(“BNS Error: The maximum number of ScheduledActions of this type have already been added.”))

{

// No user action required.

}

PeriodicCheckBox.IsChecked = false;

}

catch (SchedulerServiceException)

{

PeriodicCheckBox.IsChecked =false;

}

}

private void StartResourceIntensiveAgent()

{

agentsAreEnabled = true;

resourceIntensiveTask =  ScheduledActionService.Find(resourceIntensiveTaskName) asResourceIntensiveTask;

if (resourceIntensiveTask != null)

{

RemoveAgent(resourceIntensiveTaskName);

}

resourceIntensiveTask = newResourceIntensiveTask(resourceIntensiveTaskName);

try

{

ScheduledActionService.Add(resourceIntensiveTask);

ResourceIntensiveStackPanel.DataContext = resourceIntensiveTask;

#if

(DEBUG_AGENT)

ScheduledActionService.LaunchForTest(resourceIntensiveTaskName, TimeSpan.FromSeconds(60));

#endif

}

catch (InvalidOperationException exception)

{

if (exception.Message.Contains(“BNS Error: The action is disabled”))

{

MessageBox.Show(“Background agents for this application have been disabled by the user.”);

agentsAreEnabled = false;

}

ResourceIntensiveCheckBox.IsChecked = false;

}

catch (SchedulerServiceException)

{

ResourceIntensiveCheckBox.IsChecked = false;

}

}

bool ignoreCheckBoxEvents = false;

private void PeriodicCheckBox_Checked(object sender, RoutedEventArgs e)

{

if (ignoreCheckBoxEvents)

return;

StartPeriodicAgent();

}

private void PeriodicCheckBox_Unchecked(object sender, RoutedEventArgs e)

{

if (ignoreCheckBoxEvents)

     return;

RemoveAgent(periodicTaskName);

}

private void ResourceIntensiveCheckBox_Checked(object sender, RoutedEventArgs e)

{

if (ignoreCheckBoxEvents)

return;

RemoveAgent(resourceIntensiveTaskName);

}

private void ResourceIntensiveCheckBox_Unchecked(object sender, RoutedEventArgs e)

{

if (ignoreCheckBoxEvents)

return;

RemoveAgent(resourceIntensiveTaskName);

}

private void RemoveAgent(string name)

{

    try

{

ScheduledActionService.Remove(name);

}

catch (Exception)

{

}

}

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)

{

ignoreCheckBoxEvents = true;

periodicTask = ScheduledActionService.Find(periodicTaskName) as PeriodicTask;

     if (periodicTask != null)

{

PeriodicStackPanel.DataContext = periodicTask;

}

resourceIntensiveTask = ScheduledActionService.Find(resourceIntensiveTaskName) as ResourceIntensiveTask;

   if (resourceIntensiveTask != null)

{

ResourceIntensiveStackPanel.DataContext = resourceIntensiveTask;

}

ignoreCheckBoxEvents = false;

}

  • Now the Debug the app in Windows Phone 7.1 Mango emulator & check the background app agent running as Schedule Tasks.

  •    The Background apps schedule time with Periodic Agent on Windows Phone.

  • Enable Background schedule to receive toast notification as foreground process from background activity apps. Check to see the Toast notification tile on Mango screen as foreground process.

SSMS 2012 Launching Exception on Windows 8 Developer Preview with VS 11 Developer Preview


Recently I came across an unusual exception during launching SSMS 2012 on Windows 8 Developer Preview where VS 11 Developer Preview was already installed. On launching the SSMS 2012 RC0 , an exception window pops up indicating “Exception has been thrown by the target of an invocation“.

  • Now visiting log files did not provide proper troubleshooting info henceforth , I searched up the registry at  HKEY CURRENT USER\ Software\Microsoft\SQL Server Management Studio  , under which you can see two nodes 11.0 & 11.0_Config.
  • Expand the node of 11.0_Config & delete the entire node.

  • Now launch SSMS 2012 again in Windows 8 developer preview. It will launch successfully.

 

ComponentOne Controls(Charts, Excel,FlexGrid,Gauge) for Windows Phone 7.5 Mango


Component One Studio for Windows Phone 7.5 Mango is quite essential for enterprise apps with Windows Phone. It has more than 20 rich UI controls (FlexGrids, PdfViewer, Maps, Charts, DateTimePicker, Ribbon etc..). So , to develop rich UI set with Windows Phone 7.5 apps , lets start with ComponentOne Controls for Windows Phone. Start code in VS 2010 SP1 with Controls designing & get support for ComponentOne Controls in toolbox.

More information: http://www.componentone.com/SuperProducts/StudioWindowsPhone/

  • The ComponentOne Studio for WP7 contains massive support for enterprise Charts (Bar Charts, Pie Charts, Line Graphs..), Sortable Flexgrids, Interactive Gauges..
  • Lets check the Line Chart Sample:

  • View of Interactive Gauges of ComponentOne Controls for WP7:

  • Lets check a sample of DateTime Picker Control of ComponentOne Studio:

  • Similarly , it has direct navigation controls to access Windows SkyDrive folders from your WP7 apps.
  • A sortable FlexGrid control sample of WP7 app with ComponentOne Controls for WP7.

Integration of Windows LIVE SDK Connect & Push Notifications in Windows 8 Metro Style Apps using Windows Live SDK


Windows Live SDK is quite essential for integrating Windows LIVE CONNECT with Metro Style Apps as well as with LIVE Push Notifications. In this article we will see how to integrate Windows LIVE CONNECT & Push Notification services from Windows Azure (Cloud) to Windows 8 Metro Style Apps.  First of all , you need to Download & install Windows Live SDK from

http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=28195

Now, Install the Live SDK & Check out the Extension on VS 11 Developer Preview in Add Reference section.

  • Now create a new Metro Style project in VS 11 Developer Preview .

  • Now write some simple code in MainPage.xaml:

<Grid x:Name=”LayoutRoot” Background=”#FF0C0C0C”>

<Grid.ColumnDefinitions>

<ColumnDefinition Width=”251*” />

<ColumnDefinition Width=”1116*” />

</Grid.ColumnDefinitions>

<StackPanel Grid.ColumnSpan=”2″>

<live:SignInButton Name=”btnLogIn” Scopes=”wl.signin wl.basic” />

<TextBlock Name=”tbName” Width=”600″ Height=”150″ FontSize=”32″ TextWrapping=”Wrap” />

<TextBlock Name=”tbGender” Width=”600″ Height=”150″ FontSize=”32″ TextWrapping=”Wrap” />

<TextBlock Name=”tbLiveProfile” Width=”600″ Height=”150″ FontSize=”32″ TextWrapping=”Wrap” />

<TextBlock Name=”tbError” Text=”Error Message” Width=”600″ Height=”150″ FontSize=”32″ />

</StackPanel>

</Grid>

  • Same way modify MainPage.xaml.cs :

using System;

using System.Collections.Generic;

using System.Linq;

using System.Threading.Tasks;

using Windows.Foundation;

using Windows.UI.Xaml;

using Windows.UI.Xaml.Controls;

using Windows.UI.Xaml.Data;

using Microsoft.Live;

using  Microsoft.Live.Controls;

namespace LiveSDKDemo

{

partial class MainPage

{

private LiveConnectClient liveClient;

private LiveConnectSession session;

 public MainPage()

{

InitializeComponent();

      this.btnLogIn.SessionChanged += btnLogIn_OnSessionChanged;

}

private void btnLogIn_OnSessionChanged(object sender, LiveConnectSessionChangedEventArgs e)

{

if(e.Session != null && e.Status == LiveConnectSessionStatus.Connected)

{

this.liveClient = newLiveConnectClient(e.Session);

session = e.Session;

this.liveClient.GetCompleted += OnGetCompleted;

this.liveClient.GetAsync(“me”,null);

}

   else

{

this.liveClient = null;

}

}

private  void OnGetCompleted(object sender, LiveOperationCompletedEventArgs e)

{

if (e.Error == null)

{

dynamic result = e.Result;

his.tbName.Text = “Hello,” + result.first_name + ” “ + result.last_name;

this.tbGender.Text = “You are “ + result.gender + “that lives in “ + result.locale + “.”;

this.tbLiveProfile.Text = “Your Live Profile can be found:” + result.link;

}

else

{

this.tbError.Text = e.Error.ToString();

}

}

}

}

  • Now , after pressing F5 , you will be stuck with the following message:

  • You need to login to https:manage.dev.live.com  with your Windows Live ID & add the Metro Style Apps Package Name & Publisher Name in Windows Live Connect Portal to integrate LIVE SDK CONNECT & Push notifications with your Metro Style Apps.

  • Accept the  Terms & Condition to get the Live Application Package Name ID & paste it in your package.appxmanifest file.

  • Now Press F5 & Test it in Windows 8 Simulator & check out the following output.

  • Click on Yes & you will be signed in with Windows LIVE SDK Connect & enable to receive Push notifications in your Metro style apps.

Integration of Bing Maps in Metro Style Apps with XAML


Previously we have integrated Bing Maps API with Windows Phone Controls , Silverlight WebView in SharePoint 2010 running on Windows Azure. Now, integration of Bing Maps in Windows 8 Metro Style Apps with XAML is also quite easy. You need to insert WebView controls inside the XAML code. To implement this , simply create a new Metro Style apps in VS 11 in XAML C#/VB.

  • Now drag a WebView Control from Toolbox & insert proper height & width values in it on MainPage.xaml.

  • Now add the following code in MainPage.xaml.cs:

public MainPage()

{

InitializeComponent();

Uri uri = newUri(http://www.bing.com/maps&#8221;);

WebView1.Navigate(uri);

}

MainPage.xaml:

<Grid x:Name=”LayoutRoot” Background=”#FF0C0C0C”>

<WebView x:Name=”WebView1″ HorizontalAlignment=”Left” Height=”768″ Margin=”8,8,0,-8″ VerticalAlignment=”Top” Width=”1350″ ToolTipService.ToolTip=”Maps for India”/>

</Grid>

  • Now check the preview in Simulator & local Machine mode:

  • Switch to Aerial Mode :