Cortana – what she thinks about Microsoft, Apple and Google

Cortana the new power booster of the Windows phone 8.1 works really well. Cortana is still in beta but seems matured. I asked questions from Cortana, plenty of them. But I wanted to know what she thinks about her Microsoft, Apple and Google. The answers are impressive and funny.

image

What do you think about Microsoft ? There’s no place like home.

What do you think about Apple ? Their new headquarters looks kind of like a Halo. I’m into it.

What do you think about Google ? Impressive achievement. Still get everything I know from Bing

Advertisement

Detecting the Windows Phone Theme Background Color

We often need to detect the WP background theme color to switch the color schemes of our apps.

This is very useful when we utilize the application bar and have some metro icons in our app. In built WP apps have this feature and switch between different icons. For example when you use the Messaging app in dark background mode the icons are white and when the background is in light mode. A very simple feature but how to detect the background theme color of the WP.

Here’s the code snippet for this. PhoneBackgroundBrush is a property of the Application.Current.Resources Dictionary object.

private readonly Color _lightThemeBackground = Color.FromArgb(255, 255, 255, 255); private readonly Color _darkThemeBackground = Color.FromArgb(255, 0, 0, 0); private void DetectPhoneTheme() { SolidColorBrush theme = Application.Current.Resources["PhoneBackgroundBrush"] as SolidColorBrush; if (theme.Color == _lightThemeBackground) { btnBack.IconUri = new Uri("Images/backB.png", UriKind.Relative); BtnNext.IconUri = new Uri("Images/nextB.png", UriKind.Relative); BtnShare.IconUri = new Uri("Images/shareB.png", UriKind.Relative); } else { btnBack.IconUri = new Uri("/Images/backW.png", UriKind.Relative); BtnNext.IconUri = new Uri("/Images/nextW.png", UriKind.Relative); BtnShare.IconUri = new Uri("/Images/shareW.png", UriKind.Relative); } } private readonly Color _lightThemeBackground = Color.FromArgb(255, 255, 255, 255); private readonly Color _darkThemeBackground = Color.FromArgb(255, 0, 0, 0); private void DetectPhoneTheme() { SolidColorBrush theme = Application.Current.Resources["PhoneBackgroundBrush"] as SolidColorBrush; if (theme.Color == _lightThemeBackground) { btnBack.IconUri = new Uri("Images/backB.png", UriKind.Relative); BtnNext.IconUri = new Uri("Images/nextB.png", UriKind.Relative); BtnShare.IconUri = new Uri("Images/shareB.png", UriKind.Relative); } else { btnBack.IconUri = new Uri("/Images/backW.png", UriKind.Relative); BtnNext.IconUri = new Uri("/Images/nextW.png", UriKind.Relative); BtnShare.IconUri = new Uri("/Images/shareW.png", UriKind.Relative); } }

Styles and Templates in Silverlight

Styles

This post has very short code snippets to explain the styles and templates in Silverlight.

Styles are used to set the properties of a UI control. For example this shows the inline style setting of a Button control.

<Button Content="Direct Styling" Height="30" Margin="12,73,298,197" Width="90"> <Button.Style> <Style TargetType="Button"> <Setter Property="Background" Value="DarkOrange"/> <Setter Property="BorderBrush" Value="Blue"/> </Style> </Button.Style> </Button>

This is not interesting, because you can do this without using a Style tag by directly setting the Background and BorderBrush properties of a Button.

It is very useful when you declare the style definition as a resource. Then you name the style and can use across different Button controls as shown below.

Here you can notice for the Background property I have used the attached properties. It is possible since Value is an attached property of Style.

<UserControl.Resources> <Style TargetType="Button" x:Name="colorfulButtonStyle2"> <Setter Property="BorderBrush" Value="Red"></Setter> <Setter Property="BorderThickness" Value="5"></Setter> <Setter Property="Background"> <Setter.Value> <LinearGradientBrush StartPoint="0,0" EndPoint="1,0"> <GradientStop Color="DarkRed"></GradientStop> <GradientStop Color="Chocolate" Offset="1"></GradientStop> </LinearGradientBrush> </Setter.Value> </Setter> </Style> </UserControl.Resources>

Usage

<Button Style="{StaticResource colorfulButtonStyle2}" Content="I'm Colorful" Width="90" Margin="12,12,298,257" Click="Button_Click" Height="30" />

In the above Style declaration I have named the style as ‘colorfulButtonStyle2’. If you do not use a name then all the Buttons in the page will use the style automatically without the need of explicitly setting the Style. (In some applications it is useful mainly if you want all your buttons look same)

Styles also support inheritance. Though it is not recommended you can inherit a Style who’s TargetType is different from the one who inherits.

If you have a style defined for TextBox where it sets the BorderBrush, you can inherit this from a different style who’s TargetType is Button. This is possible because both the TextBox and the Button have the BorderBrush property.

Inheritance will raise errors only if the controls do not have the matching properties. But as a matter of a convention / good practice inheritance of Styles between two different types is not recommended.

Inheritance is done via BasedOn tag.

<Style TargetType="Button" x:Name="anotherStyle" BasedOn="{StaticResource colorfulButtonStyle2}"> <Setter Property="Background" Value="Green"></Setter> </Style>

Here the child will override the styles of the parent.

Templates

Templates allow you to change the visual “face” of any common control. In other words, if
you can’t get the custom appearance you want by tweaking properties alone (and often you
can’t), you can almost certainly get it by applying a new template.

And although creating custom templates is more work than just setting control properties, it’s still far simpler and more flexible than developing an entirely new custom control, which many other programming frameworks force you to do

In Silverlight every control has a way to be rendered. Controls are consist of other basic controls. Example Button is a complex control which consists of Rectangale, Border and other basic controls. This is known as Control Template.

<ControlTemplate x:Key="RawButtonTemplate" TargetType="Button"> <Border BorderBrush="BlueViolet" BorderThickness="3" CornerRadius="4" Background="Red"> <TextBlock Text="Custom Button Template"></TextBlock> </Border> </ControlTemplate>

Above we have defined the Control Template for the Button. You can see in Template we have the flexibility to go beyond the control’s properties and customize them.

<Style x:Key="ButtonStyle" TargetType="Button"> <Setter Property="Background" Value="Green"/> </Style>

 

We have a defined a style as well. Let’s see what happens when we apply these two to a Button. Because both the template and the style set the Background property.

<Button Content="Button" Height="46" HorizontalAlignment="Left" Margin="20,26,0,0" Name="button1" VerticalAlignment="Top" Width="166" Style="{StaticResource ButtonStyle}" Template="{StaticResource RawButtonTemplate}" Click="button1_Click" />

Output image

 

 

Few things to notice here, first it is obvious Style is rejected. Next we have set the Content property of the Button as ‘Button’. And it is overridden by the Control Template’s TextBlock control. And if you run the code you can notice that, Button would have lost it’s total Buttonness Open-mouthed smile ( I invented this word).

You can notice that Button would do its work and fires a click event, but you no longer see the visual transition and the hover effect of a typical button.

This is the way template is defined. It overrides all the properties.

But there’s a way to handle that, using the Content Presenter and Template Binding. Template Binding takes the property values from the control and pass it to the Template (Control Template)

<ControlTemplate x:Key="ContentPresenterButtonTemplate" TargetType="Button"> <Border BorderBrush="BlueViolet" BorderThickness="3" CornerRadius="4" Background="{TemplateBinding Background}"> <ContentPresenter Margin="{TemplateBinding Padding}"></ContentPresenter> </Border> </ControlTemplate>

It is used in a Button like this

<Button Content="Button" Height="41" HorizontalAlignment="Left" Margin="20,101,0,0" Name="button2" Style="{StaticResource ButtonStyle}" Template="{StaticResource ContentPresenterButtonTemplate}" VerticalAlignment="Top" Width="166" Padding="10" /

Output

image

You can see the value set on the Style is applied for Background. And Content Presenter wraps the content of a control which also used Template Binding, so the Content value in the Button is used.

Developing a simple Data bound Windows Phone Application

I was asked to develop a Windows Phone Application development tutorial for beginners. This is not an advance coding stuff, but a simple basic tutorial.

Setting the Dev Environment

Windows Phone (WP) SDK latest version is 7.1 (very recent one is 7.1.1 targeted for 512 Mb mobile devices for multimedia rich apps. but SDK 7.1 is more than enough if you are developing generic WP apps)

Download the WP SDK free from

http://go.microsoft.com/fwlink/?LinkID=226694

The above link has the ISO which includes Visual Studio (VS) Express for WP development, Emulator and all the features needed for the development in a raw machine. If you already have VS this will install the missing module only.

Optional: If you want to have 7.1.1 update apply the update from this link after installing the 7.1 SDK http://www.microsoft.com/en-us/download/details.aspx?id=29233

Developing a simple List Data Bound Application

· Open VS

· New -> Project -> Silverlight for Windows Phone -> Windows Phone Application

clip_image002

Select the version as 7.1 to target Mango (latest version) [ Mango phone version is 7.5 and the SDK version is 7.1]

clip_image003

· Initial Development Interface

clip_image006

· Find the following Grid where we place our user elements

clip_image008

· Create a List box by XAML or drag and drop from Toolbox inside the Grid

· Write the ListBox template.

<!--ContentPanel - place additional content here--> <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <ListBox x:Name="ListCourses"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Margin="0,15,0,0"> <TextBlock Text="{Binding CourseName}" FontSize="36"/> <TextBlock Text="{Binding Description}"/> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Grid>

· Go to the code behind C# file of the MainPage.xaml

· Double click and open to write the code.

clip_image011 

· Create a class Course. And populate some data in a List.

public partial class MainPage : PhoneApplicationPage { private List<Course> _courses = new List<Course>() { new Course() { CourseName = "Mobile Computing", Description = "Good course ! 🙂 " }, new Course() { CourseName = "Wireless Communication", Description = "Good course ! 🙂 " }, new Course() { CourseName = "Artificial Intelligent", Description = "Good course ! 🙂 " }, new Course() { CourseName = "Database Design", Description = "Good course ! 🙂 " }, new Course() { CourseName = "Marketing", Description = "Good course ! 🙂 " }, new Course() { CourseName = "Communication Skills", Description = "Good course ! 🙂 " }, new Course() { CourseName = "Presentation Skills", Description = "Good course ! 🙂 " }, new Course() { CourseName = "Windows Phone Development", Description = "Good course ! 🙂 " }, }; // Constructor public MainPage() { InitializeComponent(); this.Loaded += new RoutedEventHandler(MainPage_Loaded); } void MainPage_Loaded(object sender, RoutedEventArgs e) { ListCourses.DataContext = typeof(Course); ListCourses.ItemsSource = _courses; } } public class Course { public string CourseName { get; set; } public string Description { get; set; } }

· In the Loaded event we set the DataContext and ItemSource of the ListBox.

· Run the App

clip_image014

Windows Phone SDK in Windows Server 2008 R2

I have been using WS2K R2 in main primary laptop for last 5 months. I wanted to install the Windows Phone SDK on my server. Then I got the error that WP SDK is not compatible with the Server OS. I googled and finally found this useful link.

http://blogs.msdn.com/b/astebner/archive/2010/05/02/10005980.aspx

If you have an offine WP 7 installer like an ISO file extract them using any file compressing tool and follow the steps mentioned in the above link.

But another pain point is Zune does not support on the server OS. Don’t worry .. I did the search again found this following link.

http://robmensching.com/blog/posts/2009/9/12/How-to-install-Zune-software-on-Windows-2008-R2

Personally I didn’t check the Zune install on Windows Server 2008. So if it is not working don’t blame me, I just believe the Google 🙂

 

Windows Phone–Mango Version 7.1 or 7.5

The next version of the Mango is there.

 

 

But there is a small controversy about  the version of the Mango. In some places it is declared as 7.5 where in some other places it is mentioned as version 7.1.

In the SDK it says 7.1.
Read this article for the more info.

Windows Phone All in One Kick Start => Meet Jack Sparrow

Hi this is a quick and simple WP7 application, which demonstrates some of the advanced features within very few steps.

If you are already familiar wit h WCF and Silverlight then WP7 development is very easy for you.

This application explains the following features.

  • Accessing the WCF service from the WP7 application.
  • Handling Cross reference threads in Wp7 and create responsive UI.

Application

The application asks you simple question, and if you answer correct then you will be taken to meet Jack Sparrow. If you do not answer correctly, then the application asks another question and this process repeats.

WCF service is responsible for generating the questions and evaluating the answer. Thread is used to load the web browser.  Browser object is in the Collapsed Visibility mode, and the thread used to navigate the page, and bind the delegate of Navigated event.

Since the web browser is created in the main thread and accessed in another thread, this creates the cross thread reference problem. It is solved using the traditional way of the BeginInvoke() call. But the BeginInvoke() call is made from the Dispatcher object of the current thread.

Code Implementation

In my WCF service I have 2 operational contracts. One is to generate the question and the other one is for evaluate the answer. (Read the comments within the code blocks)

Code for my IService1.cs

image

Code implementation of those methods are mentioned here, You can download the complete solution here

As like Silverlight we have to call the WCF service through asynchronous method calls and binding a delegate to that. But we do not have to worry much since VS generates the Async methods for us.

Build the service, and the reference of the Service to the WP7 application.

The below image shows my WP7 application interface in the design mode.

image

 

In the MainPage.xaml.cs file we have code logic of the application.

Here is the code for calling a service in WP7. It is 100% similar to Silverlight applications. (Read the comments ..)

image

Once the Method call is returned from the service, the following method will be fired. (Note that GetQuestion() is the method we created. GetQuestionAsync() is generated by VS. We have to simply call that. (If you are familiar with Silverlight then this is not a big deal for you)).

image

The following method handles the cross thread reference problem, and provides a responsive UI. (Note that I didn’t code any explicit Threads. In .NET you can do this with simple delegates, .NET handles the thread for you).

image

Notice that in the BeginInvoke() I’m again making a call to my main thread for the Navigated event.

After the application completes it happily displays Jack Sparrow Winking smile

 

image