C#

WCF Completed Event firing more than once

0

The problem situation is stated in the title. =P

The “fix” to the problem is something real simple. I didn’t notice it when it happened to me the first time. This post is also posted to remind myself about it.

This is an example of a code with the problem.

public class WorkingArea{
Server.ServiceClient client = new Server.ServiceClient();

public WorkingArea()
{
InitializeComponent();
}

private void btnCheck_Click(object sender, RoutedEventArgs e)
{
client.CheckCompleted += new EventHandler(Client_UpdateCompleted);
client.CheckAsync(Value);
}
}

This code seems to be working fine. I have a Service which I declare it as client and everytime I click on the button Check, i attach the event handler and call the async method to run it. But wait, what went wrong is this.
“…..everytime I click on the button Check, i attach the event handler and call…….”

What happen is there will be alot handlers being attached to the client variable as times go by ( with the user clicking the button in our example ). The callback/comepleted event will increasingly happen with the button click in this example. With abit of moving of that line where you attach the handler. This will solve the problem.
“client.CheckCompleted += new EventHandler(Client_UpdateCompleted);”

I will recommend putting this in the constructor, loaded event handler or onNaviagtedTo. This will depends on what is the architecture of the application you are building.

If you have any question, please feel free to contact me at guohong@limguohong.com or use the contact page. Please visit the tutorials page for more tutorials from Microsoft Office to Windows Phone!

Windows Phone 7 – Removing Battery Indicator

0

Many a times, when we do a XNA game, we will want it to be full screen and hide the battery indicator. We will not want to show the battery indicator like this.

With Battery Indicator

With Battery Indicator

You can change it to remove the battery indicator by adding this line of code at the constructor where the game is initialized.
graphics.IsFullScreen = true;

Without Battery Indicator

Without Battery Indicator

You can download and view the sample here.
http://cid-29f099c37b76ca59.office.live.com/self.aspx/Blog/Code%20Guide/Windows%20Phone%207/RemoveBatteryIcon.zip
If you have any feedback, you can email me at guohong@limguohong.com

Error reporting for Windows Phone 7 : Little Watson (XNA version)

5

Recently I blogged about Error reporting for Windows Phone 7 : Little Watson but when I was using it for the Slime Sweeper V2 , i realised its not as simple as copying and paste the framework over to XNA and use it. We have to introduce some extra stuffs.

I built the XNA version for it. Appreciated the help given by Konaju Games on MSDN forum here and Andy Pennell who have written the original version of Little Watson.

We have to add Microsoft.Phone and System.Windows references into the project. The codes are similar but we do not have native MessageBox.Show and Application.Current.UnhandledException in the project like what we have on Silverlight project and hence we have to add them differently to the project.

In the constructor of the game, we have to add this code.

Application.Current.UnhandledException += (s, e) =>
{
if (!System.Diagnostics.Debugger.IsAttached)
{
try
{
LittleWatson.ReportException(e.ExceptionObject, GetType().Assembly.FullName);
}
catch
{
// We do not want to throw exceptions in our exception handler
}
}
};

and as for MessageBox.Show, I am using Guide.BeginShowMessageBox method.
http://msdn.microsoft.com/en-us/library/dd940233.aspx

Guide.BeginShowMessageBox

You can download the sample code which I have built here, the button will crash the application and when you try to go into the application again from the emulator, a messagebox will appear :
http://cid-29f099c37b76ca59.office.live.com/self.aspx/Blog/Code%20Guide/Windows%20Phone%207/LittleWatsonXNA.zip

If you have any feedback or question, feel free to contact me via the contact me form or email me at guohong@limguohong.com

Windows Phone 7 – Putting application in game hub

0

One of the things that i tried to do is trying to put the application which we have created into the game hub. I cant seem to find this resource easily and hence I have decided to post it and how we can go about doing it.

According to http://msdn.microsoft.com/en-us/library/ff769509(v=vs.92).aspx
We will have to go to WMAppManifest.xml and edit the Genre

You can fill in either Apps.Normal or Apps.Games.

AppManifest - Game

AppManifest - Game

By doing this, you will place your application into the game hub.

You can download the sample code here.
http://cid-29f099c37b76ca59.office.live.com/self.aspx/Blog/Code%20Guide/Windows%20Phone%207/XNAGameHubSample.zip

If you have any question or query or feedback, you can contact me at guohong@limguohong.com

Remove Title Bar from your WPF application

0

Recently, one of my friends was asking me how can him remove the title bar from his WPF Application. He dont know what the keyword he can use, I thought its interesting so I am posting here so in case if any one needs it in the future.

There are a few ways to do it.
1. Do through coding ( Code Behind ).

this.WindowStyle = WindowStyle.None;

2. Do through XAML. Add in the WindowStyle attribute to the Window Tag
Window WindowStyle="None">

3. Editing through the properties ( Ultimately, this changes the XAML )

Properties

Properties

You can check out the sample application here.
http://cid-29f099c37b76ca59.office.live.com/self.aspx/Blog/Code%20Guide/Windows%20Presentation%20Foundation/RemoveTitleBarWPF.zip

If you have any questions, please feel free to contact me at guohong@limguohong.com

Error reporting for Windows Phone 7 : Little Watson

0

I was looking through some resources and found this interesting bit written by a Microsoft developer working for mobile. He written it for his personal use and he is kind enough to release it to the community.

Link : http://blogs.msdn.com/b/andypennell/archive/2010/11/01/error-reporting-on-windows-phone-7.aspx

What he really did was actually to call the methods he had written in Application_UnhandledException and RootFrame_NavigationFailed in App.xaml.cs and this code bit will actually send an email to the email you specific with the stack trace and the error.

Simple and useful!

Thanks Andy.

Detecting theme on Windows Phone 7

0

This blog post is going to teach you how do we detect if we are on dark or light theme on Windows Phone 7. I realised that this will be useful because I have quite some friends who are asking me about it because they didn’t get the right resources online. If you happened to come across a resource that asks you to get from App.xaml, that doesnt work anymore as its removed after the RC build.

Based on Theme Resources for Windows Phone, under Theme Visibility and Opacity.
By checking Resources, I am able to check if its Visible or Collapsed for the dark or White theme by doing this

Visibility isLight = (Visibility)Resources["PhoneLightThemeVisibility"]; // for light theme
Visibility isDark = (Visibility)Resources["PhoneDarkThemeVisibility"]; // for dark theme

Then we use if else to check,

if (isLight == System.Windows.Visibility.Visible)
{
//We are on light theme
}

Light Theme

or

if (isDark == System.Windows.Visibility.Visible)
{
//We are on dark theme
}

Dark Theme

Attached is a sample code to show how do I detect Windows Phone 7 theme, before the Mango buuild ( I am unsure if there is a change after Mango ).
Detect Theme on Windows Phone 7 Sample

Slime Sweeper

0

In March, the Windows Phone 7 game created by Juan Kristoffer Mancuyas Sanio, Delconi Quek Si Qi, Lim Guo Hong and Steven Li Yan, interns of Microsoft School Technology Innovation Center is finally released to the Windows Phone 7 Marketplace! Its one of our mini-project which we decided to do during our free time.

Slime Sweeper Title Page

Slime Sweeper Title Page

Slime Sweeper Game Play

Slime Sweeper Game Play

Slime Sweeper Game Over Page

Slime Sweeper Game Over Page

Slime Sweeper How to Play Screen

Slime Sweeper How to Play Screen

I understand from reviews that this application seems to be crashing on some of the phone, I am looking into it and hope to release another version to fix that bug. :)

We have looking into creating more slime related games! Stay tune! Contact me if you are interested. :)

Download Link to Slime Sweeper on Zune

Connecting to SQL Azure with ASP.NET

0

Recently, I have been working with Windows Azure and SQL Azure for my new project and because of this, a good amount of developer friends came to me and wanted me to share more about it hence I decided to write it down as a blog post for anyone who is interested to know about it.

What is SQL Azure?
SQL Azure is Microsoft’s Relational Database offering on the Cloud. It is build from the foundation of SQL Server It helps to actually ease the provisioning and deployment of many databases. It is easily scalable per se.

This blog post is going to show you how easy is it for you to connect to SQL Azure, it actually works the same way as a normal SQL Server Connection, the main difference is the connection string. The username is your username setup at SQL Azure @ Server name. You can download the full sample project with CRUD ( Create, Retrieve, Update, Delete ) at the end of this post.

///
/// Connection to SQL Azure Database
///
public void ConnectToDatabase()
{

//Connection String
//data source=SERVER URL;
//initial catalog= Database name;
//User ID=User ID @ Server Name;
//Password=Password;
string connectionString = “data source=quxm9ku7nz.database.windows.net;initial catalog=fiveaboutme;User ID=guohong@quxm9ku7nz;Password=*****;”;
if (conn.State != ConnectionState.Open)
{
conn.ConnectionString = connectionString;
conn.Open();
comm.Connection = conn;
}
comm.Parameters.Clear();
}

You can download the project file from here.
http://cid-29f099c37b76ca59.office.live.com/self.aspx/Blog/Code%20Guide/SQL%20Azure/SqlAzureConnection.zip

Please take note that you have to change the Server Name, Username, Password at the connection string to make this work. You should run the following script to the table first.
http://cid-29f099c37b76ca59.office.live.com/self.aspx/Blog/Code%20Guide/SQL%20Azure/SQLAzureConnection.sql

How to use Back Button on Windows Phone 7

1

Credit to Steven, http://www.sticapac.com for allowing me to post this up on my blog.

Recently, I worked on an app for the Windows Phone 7 platform and submitted it to the marketplace for certification. I’ve learnt many things and gained many insights after going through this process and would like to share one of these with you, developers and potential developers of the Windows Phone 7 platform.

Throughout this post, I will be making references to the official Windows Phone 7 Application Certification Requirements document (WP7ACR) which can be downloaded athttp://go.microsoft.com/?linkid=9730558.

Under Section 5.2.4 of the WP7ACR, clause a, it is stated that:

Pressing the Back button from the first screen of an application must exit the application.

Developers who are creating apps with multiple pages must give special attention to this clause because pressing the back button automatically brings the user to the previous page, regardless of whether the user is at the first screen or not. Consider the following example:

In this app, there are 2 pages. Page1 goes to Page2 via a button with the following logic:

NavigationService.Navigate(new Uri(“/Page2.xaml”, UriKind.Relative));

Page2 goes back to Page1 via a button with the following logic:

NavigationService.Navigate(new Uri(“/Page1.xaml”, UriKind.Relative));

Each time that line of code is executed, it is actually creating a new instance of the page defined under the new Uri. Therefore if the user navigates in this manner:

  1. User starts the app.
  2. User navigates to Page2 via button.
  3. User navigates to Page1 via button.

At this point in time, the user is at the first page of the application. If the user presses the back button, he will be brought back to Page2 instead of exiting the app, which is wrong and will result in a failure of the certification process.

There is no API in the Windows Phone 7 SDK similar to the Application.Exit() function found in the average C#.NET Windows Forms application. For this reason, we must overcome this problem by navigation by page hierarchy. That is to say, when the user navigates from Page2 to Page1 via the button, instead of treating Page1 as a new page, the developer should instead use this line of code for the navigation purpose from Page2 back to Page1:

NavigationService.GoBack();

By doing this, the developer is essentially coding the button to function exactly like what the back button does on the Windows Phone. In this way, the app will always exit when the back button is pressed on the first page because the first page is always the only record under the action history when the user is on the first page. The diagram will then look like this:

Therefore we have successfully adhered to Section 5.2.4 of the WP7ACR, clause a. This is how we can effectively use the Back button and pass certification for functions relating to the back button.

Go to Top