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

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.

Visual Studio SP1 and upgrade (Oh.. what the heck is that ?!@#)

I’m a go getter when any of the new upgrades available to the products I use, and I feel it is a very good way to keep ourselves updated and it makes our life easier. (But not always….)

I installed VS 2010 SP1 immediately after they have launched it. It was fine. Last week I installed the WP7 developer tools for VS 2010 SP1 from the link provided in the MSDN. After installing the WP7, when I opened the VS 2010 it threw a message saying that some of the features of the VS 2010 has been upgraded to the SP1 and some are not, so please install the SP1 again. Until that I cannot use VS.

OK, then I downloaded the SP1 online installer again, and executed that. When installing it was asking me awkward questions like specify the ADO.NET RIA Services .msi and Silverlight 4 .msi and all that.

I don’t know where are they ? I know I have installed them and no longer have the access to the .msi files as I don’t keep them in my junk box. I specified the path where they have been installed and it says that, the path is incorrect.

Oh.. God it was a huge time killer, the bad side is, I have to submit a  project that day. But I don’t have the access to the VS.

Then I decided to repair the VS, and I did it. But no use. Even after the repairing process the same thing happened. It’s like I got stuck in an infinite loop.

Then I tried removing the SP1 one completely and tried to open the VS. Any guesses what happened ?

Again the same problem. I feel like kicking off the VS team. They couldn’t provide a support file that detects the installed SP and downloads the correct version of WP7 (or any sort of plugin or tool we add to VS).

(Here I didn’t remove the WP7 which can fix the problem, as conceptually because the I need WP7 to continue my work)

I think the problem might be this…

When I install the WP7 development kit, it has no information about the SP I use in VS. It installs the available version of the WP7 to the VS.

Then when I start the VS, it detects that one (newly added) component does not have the required features (at least few lines meta configurations) to run in the VS SP1.

That’s why it was throwing a message saying, that some components of the VS are not upgraded to SP1 while some being upgraded. And asks me to run the SP1 setup again.

When run the SP1 setup, it starts the fresh new installation of SP1 upgrade (because it asks whether remove the SP1 or to reapply that). When we select the reapply the SP1, it keeps on asking the files for SP1 upgrades (like Silverlight .msi and RIA Services .msi). Which I cannot provide because we installed them from various setup files and we don’t keep them. (at least me..)

Actually MS should have provided a neat installation pack that detect the current VS version of the machine (whether VS or VS SP1) and installs accordingly.

Because it will be a nightmare for the users who are using VS SP1 and whenever they try to install a new feature it asks for SP1 setup and it is a time consuming work to do.