Image Background in a TextBlock

Here is the scenario: On my MainPage I have three TextBlocks:

In TextBlock 2 I want to have an Image Background. The problem is that a TextBlock does not have a Background property. What I have to do is to add the TextBlock to a layout control and set its Background property. There are different ways to do this:

1. Add the TextBlock to a Grid and set the Background property of the Grid:

2. In this case you might need to apply additional Margins:

3. The (in my opinion) easiest approach is to put a Border around the TextBlock:

Which approach you take is a matter of personal taste. The result is the same:

 

To be continued…

 

MarketplaceReviewTask & MarketplaceSearchTask

In Windows Phone applications you can not directly access other applications like phone calls or messaging. Also Windows Phone applications can’t directly access information stores like contacts or photos. To access those you need to make use of Launchers and Choosers that allow you to have indirect access to some phone features.

The isolated and sandboxed model of Windows Phone protects the user because the developer can’t access personal information or send text messages without the users permission. When you use Launchers or Choosers you invoke a built-in application that will perform the task and replace the currently running application.

Here you can find more information about Launchers and Choosers.

In my applications I wanted the user to be able to rate and/or review it from within the application, and also to check out more of my apps on Marketplace.

So in my application I created two buttons:

Now I have to navigate to my event handlers and add the following code:

Clicking on those buttons will navigate me to Marketplace. At the same time my own application will move to the background where it is not active anymore. Using the Back Key     I can come back to my application at a later time.

 

To be continued…

 

Too quiet for too long…

I have been quiet on this blog for a long time now. The reason is that I was busy writing apps. I now have four apps out and another one waiting for certification. It is a lot of fun and excitement to go through that process of developing, testing, submitting and finally getting certification. But that is not where it stops. Actually I find it very exciting to watch the downloads.

My apps are not very complex. As a matter of fact, all I needed to know in order to develop them, was what I have been writing about on this blog. It is amazing what possibilities you have with that little bit of knowledge.

I am planning on continuing writing blog entries on a more regular basis though. The thing is, I have a lot more ideas for apps, but so far my knowledge is too limited.

So, expect more blog posts about Windows Phone in the near future.

If you are curious about what apps a beginner has out on Marketplace, check them out:

MyDiary , Tagebuch , Dagboek and PeriodicTable.

The one waiting for certification is going to be called LittleDictionary.

 

To be continued…

Localized or Locale?

It’s been about three weeks now, that my first application “MyDiary” has been published on Marketplace and I am quite thrilled that there are some downloads. It’s not going to make me rich, but that was never the goal anyway. I am just happy to see that I created something that people seem to like.

When I saw the downloads, and the market those downloads came from, I started thinking. So far, my application was just supporting English. But the German market seems to be interested in “MyDiary”. Being German myself, I know that only roughly 60% of the German population speak English. That was the reason to think about a localized or a local version of “MyDiary”.

Frankly, I don’t really know much (yet) about localized applications, but here you can find some information about how to build and test a localized application for Windows Phone.

I went for a different approach though. I just created a German version of “MyDiary”, called “Tagebuch” and submitted it not only to the German market, but to all markets. The advantage in my opinion is, that the German application name now could attract more users.

Basically I had to create the whole thing from scratch again and along with some German texts of course do some minor changes inside the code. For example, “MyDiary” stores diary entries together with the location where you created and the date when you created those entries. By default the date is being displayed like this:

In a German version I want the date being displayed in German, so obviously I have to add something in my MainPage’s constructor:

The date now looks like this:

Take a look at this to learn more about Culture Info.

My applications don’t support multiple languages, but at the same time users choose the language they want in the moment they download the application. I deliberately chose this approach, because I wanted users to be able to use the language they want to, independent from where they download the app. If they live in the USA they can choose to download the German version or the English version. Like I live in the Netherlands, but I like to have my applications in English.

Oh, and while I was at it, I also created a Dutch version, called “Dagboek” that is ready to submit. While I basically developed only one app, with the German and the Dutch version, I will have three applications on Marketplace.

If you are interested in German versions as well, and you have questions about translations, don’t hesitate to contact me either on this blog via a comment or via Twitter @andidrea.

 

To be continued…

Just another App?

I have been silent on this blog for a while now. The reason is that I have been working on an application. And I have to tell you, it is quite exciting to go through that whole process of developing your application, testing it thoroughly and finally submitting it to Marketplace.

If you think that is where the excitement ends, let me tell you, you are wrong. I am waiting for certification for my application right now, and it is as exciting.

Anyway, to get it submitted in the end, I had to solve a few issues.

In my application I am dealing with Location and Tombstoning, I am using Isolated Storage, I am making use of the application bar and I am navigating between Xaml Pages.

When I did that blog entry about Tombstoning, everything was working fine. When I needed that in my application, I was running into problems with my back key.

When I was returning to my application from a Tombstoning situation, the state where I left the application before was saved and restored. Just how it was supposed to be. But when I then wanted to terminate the application by using the back key, I was just looping through several pages in my application, but I could not exit it.

Now, I have to admit that I still really don’t have a clue why that happened. But at least I eventually I found a solution. I needed to override that initial behavior of the back key:

Another thing I want to talk about now is a great way of testing GPS readings. In another previous blog post I was writing about how to call out to a Web Service to retrieve the current location. In that post I did set a fixed latitude and longitude so I could test it on my emulator.

In the meantime I have updated my development environment from “Visual Studio 2010 Express for Windows Phone” to “Windows Phone SDK 7.1 RC” (you can download the tools here)

Anyway, with the newest tools, I can test GPS readings on my emulator with actual locations:

I just took an existing project and ran it in the emulator. In the emulator’s toolbar on the side I clicked the arrow:

That opened up another dialog:

There I can just pin different locations:

I randomly pinned one location, and my emulator indeed retrieved that latitude and longitude and showed me the right location:

Pretty neat, I think.

However, coming back to my application, that hopefully will be published later this week, there is one more thing I want to point out. In order to submit your application, there are some “Application Submission Requirements” that you need to be aware of.

Here you can find the “App Hub Application Submission Walkthrough”.

Having said that, I want to encourage everybody out there to write apps. Even if you are an absolute beginner, like I am. It’s really not that hard.

Windows Phone is a cool platform and it is a lot of fun to work on an application.

That I have submitted my first application was the end goal when I started this blog. But that doesn’t mean the end of this blog. After all, there is so much more to learn, so much more to be enthusiastic about, so much more to write about.

To be continued…

Image Background, Orientation Changes, Control Visibility

I have been thinking about getting an app out into Marketplace myself. I have come up with a few ideas. I don’t know, yet, if I can make them work, but I am going to give it a shot for sure.

Of course, with that idea in the background, there is still so much to learn about developing for Windows Phone 7. But, with what I have learned so far, I think, I could actually give it a try.

I will, of course, report every step here on my blog.

Anyway, before I can start that, I need to have a look at a few little things. Like how to style the background of my app using an image. After all, I want to give it an unique look. Otherwise it’s going to be another application with just a black background.

Another thing I want to look at is what happens when I turn the emulator. Will my application be displayed right? And if not, how can I make it right? And how does that change the layout of my application?

So, there still are a few things, that I need to know before I really can get started working on a real application.

So, I am just going to create another project in Visual Studio.

First thing I want to do is running the app and rotating the emulator:

Well, that’s not going to work. Everything is locked in places. The way to fix that is a simple property on the Application page:

I am changing that property in the property window:

This is going to change the Orientation property in XAML, and I can turn the emulator either way when running the application:

Now everything flips into place, depending on how I turn the emulator, or later the device.

Let’s move on and set a background. Now, the easy thing to do, if I want to move away from that black background is, of course, to change the background color. But I want an image background.

Here is how I do that: I put my mouse cursor in the LayoutRoot Grid and then I can go to the property window where I can change the background property from Transparency to the image I choose as my background:

So far, so good. Let’s run it:

It is re-orienting itself when I turn the emulator. The only thing left here is to set the proportions right. I do that by changing the Stretch property of that image from Fill to UniformToFill.

Now I drag and drop a Textblock control into my main form:

The problem now is that I have to make sure that my text is readable with the image in the background. A useful control for that is the Rectangle control. Let’s drag and drop one right on the Textblock control. I am going to edit that a little in my XAML. What I want is the Rectangle control behind the Textblock control but in front of the image itself. First thing I do here is to modify the opacity of the Rectangle control.

When I run it now and turn the emulator I am yet running into another problem:

Both my Textblock and my Rectangle do not stretch to the entire width of my emulator when it is in landscape mode. In order to fix this I am setting the Horizontal Alignment of both controls to “Stretch” and set the left Margin setting of both controls to “0”. Finally I am going to remove the Width property for both controls completely:

When I run it now my Textblock and my Rectangle nicely fill the width:

Let’s move on.In one of my ideas for an application I need to allow the user to edit values that at this point are read-only. I want the Textblock to flip into a Textbox. There is a little trick. Let’s drag and drop a Textbox onto the designer:

I am going to set the Textbox properties equal to the Textblock properties in the XAML. I am also clearing out the text property of the Textbox. The text property of the Textblock I am changing to “Hello World”.

Now I need one more control, a button:

Now I have to write some code inside my event handler. When somebody clicks the “Edit” button I want the Textblock to flip to the Textbox and back to Textblock when the button is clicked a second time:

What I am doing here, is first checking if the Textbox is visible. If so, then the first codeblock (“if”) is executed. If not, then the second codeblock (“else”) is executed. So basically I am toggling between the Visibility of the Textbox and the Textblock. When somebody clicks the Edit button then I want to retrieve the value of the Textbox and put them back in the Textblock. One more thing to do. In my XAML I am going to set the initial Visibility of the Textblock to “Visible” and the initial Visibility of the Textbox to “Collapsed”:

Let’s see this in action. When I click the Edit button the application hides the Textblock, opens up the Textbox and puts in the value so that I can edit it:

When I click the “Save” button it collapses the Textbox, opens up the Textblock again and puts the value from the Textbox back in the Textblock.

To sum it up, I have set an image background, I made sure that the proportions are right both in portrait mode and in landscape mode, I made sure that my text stays readable with an image in the background and I allow the user to edit values.

With that said I think it is time to start working on an application.

 

To be continued…

 

 

GPS, Location API and Calling Web Services

The Windows Phone 7 is equipped with a GPS interface and couples that with the Windows Phone Location Service API within the .NET Framework class library. It can be used to determine the Latitude and Longitude for the application run on a Windows Phone device. It can be used for some pretty creative purposes.

As a matter of fact there are some companies that use Geo Location, e.g. that big coffee chain from Seattle (not mentioning the name here), allowing “check-ins”.

I am going to have a look at how this works. Unfortunately, since the Windows Phone Location Service API relies on a phone’s physical hardware for the GPS, and I, in this example, will only work with the emulator, I might not get a completely satisfying result, But I am going to try it anyway.

I am going to try and retrieve the current location of the phone, or in this case the emulator, through the Windows Phone Location Service API. At least I am going to show the approach how it can be done. I am going to use the Latitude and Longitude of a specific location, if indeed the application is running in an emulator as opposed to a physical device someday.

Then I am going to call a Web Service that will allocate the Latitude and the Longitude to a city, state and country.

A Web Service is just like a method that runs over the internet. Somebody hosts a Web Service and exposes the code block’s name (in a sense), what parameters are accepted and what it will return back. I can call that over the internet and then retrieve the result back and do something meaningful with it.

I create a new project with just a textblock, where I clear out the content, and a button where I change the content to “Find Me”:

I am just going to double click the “Find Me” button to get to the click event in my C# code.

The first thing I need to do is to get access to the Windows Phone Location Service API, which I currently don’t have. I don’t, because the way that .NET Framework class library is built, it is split up into a number of different dll files or assembly files. The reason for that is that I don’t have to load more of the library than I actually need. With so many classes available it would make it so overweight to keep it all in one place and make it all available at the same time. So, all I need to do is just to pick and choose all the pieces that I need, and include that particular dll file into my project.

If I open up the References folder, I can see all the dll files that are there already:

Those dll files have been automatically added when I created the new project. The template would add these. It kind of knew I would need some of those features in almost every application I build.

This is the first time that I’ve come across a feature that doesn’t fit inside one of these dll files. I am going to have to add a reference to that:

I now can see it in the list of dll files. Now I have to add a little using statement:

Now I should be able to add some code inside my button click event:

So, I either retrieve the latitude and longitude on the phone device. If I am not running the application on a physical device, but on the emulator, then I have defaulted it to a specific value, so that I get some satisfaction by running this example.

Now, that I have that location information I need to send it off to a Web Service in order to determine the city, state and country of either the retrieved latitude and longitude or the default value.

Again, a Web Service is like a method that communicates over the internet, usually over port 80 or rather over http, just like the rest of the internet.

The conversation between my client application and the web server that hosts the Web Service is in an XML format called SOAP (Simple Object Access Protocol). Fortunately I don’t  directly have to deal with the XML, rather the SOAP message. Visual Studio and the .NET Framework will hide those from me, so that I can work with the Web Service as if it were any other class or method in the .NET Framework class library. This is called a proxy class.

The proxy class is created, based on the information that Visual Studio initially retrieves from the Web Service’s definition the first time I try to connect to the Web Service using Visual Studio. The proxy class then handles all of the little details of working with that Web Service, so that I don’t have to.

Here you can find some Information about the Web Service I am going to use. I am choosing MSR MapsService WSDL, then there opens up a page with a list of all the methods that belong to that class. I need that Url : http://msrmaps.com/TerraService2.asmx.

Ok, now I right click on my project name in the properties window and add a Service Reference:

A window is going to pop up where I have to paste in that copied Url and then click on “Go”:

Now it is looking over the internet to find that Web Service and everything that belongs to it:

Eventually I rename that namespace to something friendlier. When I then click the OK button, Visual Studio is building that proxy class that I can access from that point on.

Now, I have to admit, at this point I ran into some trouble. I just kept getting some error messages, so I repeated the step of adding the Service Reference a few times. Nothing worked. I even started the whole project again from scratch. That didn’t change anything. I still kept getting those errors. Then I closed down Visual Studio, opened it up again, and added the Service Reference again. That did the trick. I am wondering if it was a bug in Visual Studio, or if it was my pc. Or was it my lack of knowledge? Anyway, if any of you want to try my example and you run into some trouble, then try to close and reopen Visual Studio.

Ok, let’s move on:

I am creating a reference to a “client” object. I am going to need to make what is called an asynchronous call. When I make a call over the internet it might take a long time, depending on the time of the day or if the user is going to make that call with the phone and has a bad connection. So, what happens while the application is waiting?

The phone is basically locked out of taking additional actions. Therefore in Silverlight, I can call some Web Services asynchronously, meaning that I listen for the event that fires once the application receives back the response from the Web Service, if it ever receives it back. Meanwhile then the application is free, and the phone is free to continue on working until that event happens. So, once I’ve told Silverlight that I am listening for the response in an event that I am going to create, then I am free to call the Web Service. It’s a two step process. I am going to send out the request, and while I go on doing what I want to do, at some point the response will be retrieved. In other words, the phone will not lock down while waiting for the response, it will continue to operate.

Fortunately again, Visual Studio makes this very easy. It creates an event that will handle the asynchronous call.

There is an event handler called “ConvertLonLatPtToNearestPlaceCompleted”. This is what I do:

When I type in “+=”, there pops up a bubble that’s going to create that new event handler by hitting the tab key twice:

Visual Studio is generating that event handler that is fired off whenever that event happens. It also threw in an exception. I don’t think I am going to need that exception, so I comment that out and add add this line of code there:

So far, so good. I have set everything up, but I have not really called the Web Service, yet.

Let’s do that. I am going to add this code inside the button click event handler:

What’s going to happen is that I am going to pass in the default Latitude and Longitude values in the creation of a new object of type LonLatPt. It’s what is reqired by this Web Service “ConvertLonLatPtToNearestPlaceAsync” in order to begin the asynchronous call to the Web Service.

Ok, let’s run it:

Well, the only issue seems to be that my default Latitude and Longitude values seem to be incorrect. I expected it to be Amsterdam, but it is close enough. This will do.

Of course, to run this application on a physical device, I need to make sure to have a data connection.

One more thing: I am looking forward to the release of the new Mango tools. I just read that with them I can test this application even better in the emulator.

So, I am pretty sure that there will be other blog entries about this topic.

 

To be continued…

 

 

 

 

 

%d bloggers like this: