Författare arkiv

Fixing the assembly hopping between WP7 and WP8

If you want to develop an Windows Phone app that can run on both WP7 and WP8 you can simply submit one built for Windows Phone 7. But if you want to take advantage of some of the new feature of Windows Phone 8 you will need to submit two versions of it (one for 7 and one for 8). This means that you will have to maintain two copies of the project file -- but you obviously don't want two copies of each source code file.

This can be solved by putting the code in a Portable Class Library and then reference it by the two projects. There are however some limitations using a Portable Class Library, and you might find yourself stuck between a rock and a hard [coded] place trying to use it.

Each time I was facing the dual project problem, I ended up with using linked source code files instead, i.e. making one of the projects the "main" project, and then simply linking all the source code files into the other project (i.e. "Add As Link" rather than "Add" when adding "Existing Item..."). An annoying problem with this solution is however that each time you rename a source file you will need to remove and add the linked file again.  Aaarrgghh!  :[  But for me this actually was less painful than using the Portable Class Library.

Anyway, this way you will only have to maintain one copy of the code files that are identical on both platforms. For the ones that differs a bit, you either use compiler switches (e.g. "#if WP7 [...] #endif") -- or totally different classes (i.e. non-linked code, if there are "too much" differences to make use of #if's).

But when it comes to XAML you are in more trouble. For instance, if you are sharing the same implementation of a page in both WP7 and WP8, you will notice that the home of the Pivot (and Panorama) has changed between the two platforms. Ouch.

In WP7 your page declaration looks like this:

xmlns:xxx="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls"

but your WP8 projects want this line:

xmlns:xxx="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"

As you can see above, the assembly name has changed.

This makes you yearn for the following (illegal) construction:

<phone:PhoneApplicationPage 
    x:Class="Wp7App.PivotPage1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
#if WP7
    xmlns:xxx="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls"
#elif WP8
    xmlns:xxx="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
#endif
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

But unfortunately compiler switches are not allowed in XAML... So the above will NOT compile. :(

 

There is however a pretty simple solution to this. Create the following two classes:

namespace MyShared
{
	using Microsoft.Phone.Controls;

	public class MyPivot : Pivot
	{
	}

	public class MyPivotItem : PivotItem
	{
	}
}

This file can be linked across the two platforms intact without any need of #if's (since it wasn't the namespace that was changed, "just" the assembly).

Now you simply use the MyPivot wrapper classes (instead of the Pivot directly) in your XAML, and you're good to go:

<Phone:PhoneApplicationPage 
    x:Class="MyShared.PivotPage1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	xmlns:MyShared="clr-namespace:MyShared"
    xmlns:Phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone">

    <Grid>
        <MyShared:MyPivot Title="MY APPLICATION">
            <MyShared:MyPivotItem Header="item1">
                <Grid/>
            </MyShared:MyPivotItem>
            <MyShared:MyPivotItem Header="item2">
                <Grid/>
            </MyShared:MyPivotItem>
        </MyShared:MyPivot>
    </Grid>
    
</Phone:PhoneApplicationPage>

 

Broadcast on Bambuser from an IP-cam

I have mounted a D-Link DCS-923L IP-cam in a nesting box to watch birds nest and their home life. The IP-cam is a simple surveillance camera with night vision that allows it to capture video in complete darkness, and it is equipped with a built in web server that serves the live video feed. I [...]

Less is more

Jag har ett projekt hemma där jag samlar in data från vårt värmesystem som sedan kan visas i diverse grafer via en websajt. Jag har tidigare använt en gammal laptop som webserver men tänkte att jag skulle se om det … Läs mer

web-ROTI

Jag brukar mäta nyttan med de möten jag håller med hjälp av ROTI (Return On Time Invested). Det är en enkel metod för att förstå om ett möte är bra, om det behöver förbättras eller rent av inte borde hållits … Läs mer

web-ROTI

We have started a small project to build a web-ROTI, that is an app that can be used to measure how good your meetings are. After one day of hacking we have a MVP. Web-ROTI (Return On Time Invested) is a tool to gauge the time spent on meetings and improve them. ROTI can easily [...]

Running a "Hello World" written in C# on a Raspberry Pi for Linux n00bs like me

A while ago I found myself in the possession of a Raspberry Pi.

O the joy of a new toy! But what to do with it?

Well, being a C# developer of course I would like to be able to install and run my own applications on it — and I wanted to do that remotely from my regular laptop.

The biggest problem was that I have no Linux skills what so ever. I've been living a comfortable (well, hm... you know...) Windows life ever since the days of the Amiga and Commodore 64. But how hard could it be?

The following are the steps I had to go through to make it work. It's a hodgepodge of wisdoms found all over the internet. So; disclaimer: It worked for me, but I can't promise it will work for you!

 

Here we go...

 

Step 1. Formatting the SD card

To do this properly you'll need to use the SD Formatter 3.1 application.

Install it and launch it having your SD card inserted. Simply press the Format button. It's enough to just do a quick format.

Important note!

If you are repeating this step later on, your SD card might appear to have lost a few gigabytes, only showing something like 56MB! This is due to that Windows cannot see the Linux partitions, and the 56MB is just some left-overs that Linux didn't claim.

You have at least a couple of options to reclaim the full space in Windows:

  1. Find yourself a [video]camera or any other non-windows device and try to format it in there. I first tried my age-old compact camera, but it just said that the card was bad. Then I tried my somewhat newer video camera, and it formatted it nicely and I got back my gigabytes. Phew...
     
  2. Use the dreaded (?) Flashnul (blog post) tool. I haven't tried this one since my first option worked for me. Be careful...

 

Step 2. Installing a Linux distribution on the freshly formatted SD card

I used the Raspbian “wheezy” image. (I was somehow drawn to its tag-line "If you’re just starting out, this is the image we recommend you use".)

To write the image to the card you'll need the Image Writer for Windows application. There is no installation required; just download and launch the Win32DiskImager.exe. Launch it, click the blue little folder-button and point out the image file. Then press the Write button to start writing. This will take a few moments, depending on the size and speed of your SD card.

 

Step 3. First launch

After preparing the SD-card it's time for the first launch.

Insert the SD card into the Pi and plug in an ethernet cable (with internet access), a USB keyboard and a monitor/tv (hdmi). Finally plug in the micro USB power adapter to boot it up. You will see a whole lot of text burst out on the screen. You do not need to read it all. :)

After a while a classic ASCII-artish gray popup appears, entitled Raspi-config.

Here you can for instance set your keyboard layout. When you're done, use the tab key to move to the Finish button, then press enter to access the console.

This popup is shown only the very first launch time. The following times you start the Pi you will need to enter a user name and password to access the console. The default user name is "pi" and the password is "raspberry".

 

Step 4. Optional: Setting the keyboard locale

If you didn't change the keyboard layout in that first popup (which only will show up upon the first launch), you can do it using the following command:

sudo nano /etc/default/keyboard

Where 'sudo' means 'run this command as super user', 'nano' is a text editor and '/etc/default/keyboard' is the file to edit.

Now the text editor appears, and you may change the 'XKBLAYOUT' to whatever you like. I chose 'se' for Sweden.

Save with Ctrl + O (!) and exit with Ctrl + X.

Reboot. This can be done by pulling the power cord or by issuing the command

sudo reboot

 

Step 5. Set a fixed IP-address

Since you want to remotely connect to your Raspberry Pi you might want to give it a fixed IP-address. This step is of course optional.

If you want to know the current network settings on your Pi, execute the following command:

ifconfig

(No, that is not a typo. It should be an 'f', not a 'p'...) You will see the current IP-address under the section 'eth0', on the line starting with 'inet'. (The MAC address is found after the word 'HWaddr'.)

Edit the network settings by executing:

sudo nano /etc/network/interfaces

When the editor appears, replace the line

iface eth0 inet dhcp

with these lines (the #-mark means that the line is commented out.)

#iface eth0 inet dhcp
iface eth0 inet static
address 192.168.1.200
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255
gateway 192.168.1.1

Obviously you will need to fill in your preferred numbers here...

After saving the file you'll need to reboot. The best thing is of course to check your new settings by executing the ifconfig-command again, after rebooting.

 

Step 6. Ensure the SSH service is started at boot time

This is necessary for connecting remotely. It was however already active on the image I used, but I'll keep this step here anyway.

The command to execute is

sudo mv /boot/boot_enable_ssh.rc /boot/boot.rc

which means to rename/move the file/directory specified. I got the following error message:

mv: cannot stat `/boot/boot_enable_ssh.rc': No such file or directory

which I interpreted that the SSH service was already active for me.

 

Step 7. Connect remotely from your favorite Windows PC

Download PuTTY.

There's no installation here, just an exe-file. Launch it and fill in the IP-address to your Pi (and the port, which defaults to 22) and connect with the Open button.

You will get a warning of a potential security breach the first time you connect, because your PuTTY instance (obviously) does not recognize your Pi.

I guess it is quite harmless to press the Yes button here.

Now the Pi console window greets you by asking for your username (pi) and password (raspberry). You're in!

 

Step 8. FTP

In addition to execute command on your Pi you will also need to transfer files to and from it. Use your favorite FTP client, for instance FileZilla. Simply connect to the correct IP address, port 22, user "pi" and password "raspberry".

 

Step 9. Install the C# compiler and .net exe-executer

To be able to compile and execute C#-code on the Raspberry Pi, you will need to install Mono.

This turns out to be pretty simple. Just execute the following command to install the dmcs (4.0 mscorlib) C# compiler:

sudo apt-get install mono-dmcs

This will take a while to process, the files requested will be downloaded from the internet and installed by some magic Linux fairy. Please note that in the beginning you will get a "Do you want to continue"-question. After saying Y + enter you might want to get a quick cup of coffee (or a beer since this is most probably done off-business hours). It will take a couple of minutes.

 

Step 10. Compile and run your program.

Now, I wanted the full monty and even compile the C# code on the raspberry. So, in order to make it as simple as possible, I put all the code into one file (HelloPi.cs):

namespace Test
{
	using System;

	public class HelloPi
	{
		public static void Main(string[] args)
		{
			Console.WriteLine("Hello World!");
			Console.ReadKey();
			Console.WriteLine("End.");
		}
	}
}

 

Using the FTP client, transfer your 'HelloPi.cs' source file to a suitable folder. Compile the source file with the dmcs-command:

dmcs HelloPi.cs

Since it's not a very large application, it will be compiled pretty much instantly. Launch it with the mono-command:

mono MyApp.exe

Joy to the world, my app has launched! Laughing

Important note!

You might as well do the compilation on your PC (which is Much. More. Convenient.) and just transfer the exe-file to the Pi instead.

 

Some useful links

  

Good luck! Wink

 

The Øredev 2012 Diversify App is now available!

The Øredev 2012 conference that starts 5th of November announced a Mobile App contest. Fredrik Mörk noticed this announcement and thought it would be a great opportunity for Diversify to shine. We choose the WP7 platform since we are mainly .NET developers. The team consisted of  Micael Carlstedt, Niclas Carlstedt,Fredrik Mörk, Markus Wallén, Mattias Larsson and myself.  We used Mercurial [...]

Announcing the Diversify app for Øredev 2012

A collegue of mine, Fredrik Mörk, noticed the announcement of the Øredev 2012 mobile app contest, and soon a team was formed that happily started hacking away (hosting the code in a Mercurial repository on BitBucket, and also using Trello to have some sort of control of who was what, and what needed to be done). The team consisted of Micael Carlstedt, Niclas Carlstedt, Fredrik Mörk, Markus Wallén, Sebastian Johansson and myself.

The app is supposed to function as a companion before and during the conference. The main functionality is that it offers you to easily navigate and explore in the conference program, and build your own personalized conference program by “favoriting” sessions. It also features a twitter feed, listening to stuff related to the conference and the app. One focus of the design has been to allow you to explore. From almost every view, there is a way to move on and get information about related stuff. One example is when you are looking at a session, from this view you can in one touch navigate to

  • a page highlighting the room in which the session is on a map
  • a page showing all sessions in that room during the conference
  • a page showing details about the speaker (OK, this typically requires two interactions; scroll to the bottom of the page and then tap the speaker)
  • a page showing details about each other session running in other rooms at the same time
  • a page showing all sessions for a topic that this session also has

This may be the most extreme example, but it’s not unique in its concept.

One other thing that the app does is that it invites to sharing information about the conference. You can (again with very few interactions) share info about a session or speaker to social networks.

There is an update submitted to the market place which will add a couple of features (tell the app what days you will attend and it will filter the data throughout the app accordingly and, as a personal touch, the app authors’ suggestions for some sessions we find extra interesting). If you happen to have an unlocked developer phone, you can grab the XAP file for that update here. Otherwise, go ahead and get the app from the Windows Phone Marketplace.

 

(Thank you Fredrik, for allowing me to steal your words from your blog postSmile)

 

Some screen shots (click for full size):
Splash screen My Øredev List of sessions Session details

Speaker details Twitter feed Tweet detail About us

Ordning och reda

Den här artikeln handlar om poängen med att ha ordning och reda. Min tanke är att ju mer symmetri och ju färre överraskningar det är desto bättre. Det minskar belastningen på vårt arbetsminne och minskar risken för misstag, speciellt under … Läs mer

Att underlätta kommunikation

Hos den kund där jag för tillfället sitter har man skrivbord med en skiva i bakkant av skrivbordet, dvs så det blir som en liten vägg bakom bildskärmen. Skivan är så hög att man inte ser över den utan att … Läs mer
Tillbaka