PowerShell – Working with JSon

Manipulating JSon with PowerShell

A few tips about working with JSon in PowerShell. (more…)

post image

Windows 8.1 Command Binding in a DataTemplate

DataTemplate and Bindings

In Windows 8 there are a few controls that rely on a DataTemplate to show item collections.

The DataTemplate is used to define visual layout for each item that will be rendered in an ItemsControl, it data binds nicely to collections of objects. It then iterates through the collection and uses each item as context for every new Template that is created.

Binding to Commands in the DataTemplate

When binding Commands to controls we usually bind them to Commands in the ViewModel, set as datacontext.

But if you, for example, have a button defined in the DataTemplate and bind that to a Command defined in your ViewModel, set as the datacontext for the page, it won’t work.

Why doesn’t Binding to Commands work in the DataTemplate?

The reason that the Command isn’t executed is that the Command can’t be found.

Since it’s an item in the collection that’s actually the current DataContext for the item being created, the command can’t be found if it’s defined globally in your ViewModel.

The solution is to refer the Command binding to look further up the object tree to find the Command.

In WPF it was solved using FindAncestor, but that’s removed now in WinRT.

The easiest way to solve it is to actually give the view/page an x:Name. Then refer to the Command in the binding as DataContext.MyCommandName, and set ElementName to the name of the view/page. See code snippets below, how the Command is defined, the x:Name set in Page and the Command defined in the ViewModel.

Code Snippets

ViewModel snippet:

        private RelayCommand headerClickedCommand;
        public RelayCommand HeaderClickedCommand
        {
            get { return headerClickedCommand ?? (headerClickedCommand = new RelayCommand(ExecuteHeaderClickedCommand)); }
        }

        private void ExecuteHeaderClickedCommand()
        {
            NavigateSomewhere();
        }

XAML PageRoot snippet:

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      x:Name="MyView"
      ...
      >

XAML DataTemplate snippet:

<DataTemplate>
  <Grid Margin="0,0,0,2">
    <Button Foreground="{ThemeResource ApplicationHeaderForegroundThemeBrush}"
				    AutomationProperties.Name="Group Title"
                                    Command="{Binding DataContext.HeaderClickedCommand, ElementName=MyView}"
                                    Style="{StaticResource TextBlockButtonStyle}">
     <StackPanel Orientation="Horizontal">
       <TextBlock Text="{Binding Category}" Margin="0,-11,10,10" Style="{StaticResource SubheaderTextBlockStyle}" TextWrapping="NoWrap" />
        <TextBlock Text="{StaticResource ChevronGlyph}" FontFamily="Segoe UI Symbol" Margin="0,-11,0,10" Style="{StaticResource SubheaderTextBlockStyle}" TextWrapping="NoWrap" />
      </StackPanel>
    </Button>
  </Grid>
</DataTemplate>

Happy Coding :)

Tests not running in Visual Studio 2013 with ReSharper 8

Unit tests not executing from ReSharper’s Unit Test Sessions

Pressing Ctrl+U,L resulting in nothing but spinning bars? I had this problem trying to run my unit tests from Visual studio 2013 using ReSharper 8.

The test indicators would keep spinning but nothing happened, setting breakpoints on the first row on the code showed that the unit tests actually never even started executing. No matter how long time that passed (ok I stopped waiting after 3 minutes to be honest) the bars just kept spinning and no error shows up.

After searching the web it turns out this is a known issue, stated to be solved in several ReSharper releases. Not sure if it’s the case and the bug has just resurfaced in the new ReSharper 8 version.

The easy solution

Turns out this problem is avoidable just by starting Visual Studio 2013 as an administrator.

If you got VS2013 pinned ot the taskbar in Windows 8/8.1, this is the easiest way to make it always start in admin mode:

1. Right click the pinned Vs2013 icon and select properties.

Pinned VS2013 Properties2. Click the “Advanced” button in the properties window.

3. Check the “Run as Administrator” checkbox.

Advanced Properties

After this your Visual Studio will always be run as Administrator.

 

Happy Testing :)

The .winmd file ‘Microsoft.Advertising.WinRT.UI.winmd’ contains duplicate type names

Error when using Microsoft Advertising SDK in Windows 8 / Windows 8.1 projects

After updating my Windows 8 app to Windows 8.1 I got a compilation error saying:

"The .winmd file 'Microsoft.Advertising.WinRT.UI.winmd' contains duplicate type names.
Type 'Microsoft.Advertising.WinRT.UI.AdControl' is already registered with the in-process
 server 'CLRHost.dll'.	ListWee_W8_Tests"

For me, this occurred in the test project.

 

After scouring the web I found a few solutions but most of them seemed like hacks, and I feared I would have to revert them later on if I used them now.

The easy solution

The easiest solution for me was to just add a reference to the Microsoft Advertising SDK in the test project.

Adding Microsoft Advertising SDK

After that the compilation works and there’s no need for more hacks :)

post image

Using Azure Mobile Services in Portable Class Libraries now possible!

Using Azure Mobile Services in Portable Class Libraries

It’s now possible using Mobile Services from PCL’s for your data access. Just add the Windows Azure Mobile Services NuGet package to your Portable Class Library and you should be ready to go.

Bcl.Async Error

When I added my package I got some errors however. A prerequisite is having NuGet of version 2.1 or later, but mine was over that.

*(If your’s is of a lower version, make sure to upgrade in Visual Studio via Tools -> Extensions and Updates…)

The error I got is shown in the image below:

BclAsyncError when installing Mobile Services NuGet package

The solution

Seems the Mobile Services package isn’t going to work with Windows Phone 7 and higher selected, so the trick was to raise the version for supported Windows Phone OS to 7.5. See picture below.

Windows Phone 7. 5 and higher selected for PCL

post image

MVVM Light on Windows Phone 8 & Design-Time ViewModels

Using ViewModels to inject Design-Time data

MVVM Light

To make your apps design more tangible when using Blend or Visual Studio we often resort to injecting design-time data in some kind of way.

One of the best ways to do it to actually fake the entire ViewModel and have one that’s used at design-time.
I tried this approach on a new app that I’m also using MVVM Light in. And I couldn’t get it to work at first, I kept getting the error: The name “DesignTimeViewModel” does not exist in the namespace “clr-namespace:MyNamespace.ViewModel”.

How to bind a Design-Time ViewModel to the XAML View

For those that do not know, here’s how you bind a desing-time viewmodel to your views. You need to declare a namespace where your view exists, a reference to http://schemas.microsoft.com/expression/blend/2008 and point out a DesignInstance DataContext. Add the following code in the PhoneApplicationPage-tag:

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:designTimeViewModel="clr-namespace:MyNamespace.ViewModel"
    mc:Ignorable="d"
    d:DataContext="{d:DesignInstance Type=designTimeViewModel:DesignTimeViewModel, IsDesignTimeCreatable=True}"

With MyNamespace.ViewModel being the namespace your viewmodel is located in and DesignTimeViewModel being the name of your viewmodel.

The way I created my design-time viewmodel was by just copying the regular viewmodel, adding a parameterless constructor and adding data to the properties in that constructor.

The solution

With me using MVVM Light all the viewmodels I ha dimplemented was inheriting from GalaSoft.MvvmLight.ViewModelBase, when copying the viewmodels to create my design-time viewmodels I din’t remove the inheritance. And it turns out that this inheritance was the root of the problem.

So if you remove the inheritance from GalaSoft.MvvmLight.ViewModelBase on your design-time viewmodels the error should disappear.

Happy coding!

post image

WACK Issues

WACK not finding your app?

Screenshot of WACK

Fig 1. Windows App Cert Kit

I had some problems when I tried to run WACK 2.2 (Windows App Cert Kit) to test my new App.

After selecting “Validate Windows Store App” – WACK persisted to show an old test project, in doubles,  instead of the name of my Windows App. If I unistalled my app from the start screen, WACK still showed the old test project as installed, but this time only one time.

Installing my App again for testing, just resulted in a copy of the line describing my old test project as an installed App.

It was clear that my old testproject was some how messign things up, problem is it wasn’t visible on the start screen so there was no way of uninstalling it from there.

How to fix it and uninstall a Windows 8 app manually

Screenshot of Debuggable Package Manager

Fig 2. Debuggable Package Manager

To locate all installed packages I ran the “Debuggable Package Manager” tool shipped with Visual Studio 2012.

In DPM you can list all your installed Apps with the “Get-AppxPackage” command. See Fig 2.

Then use the command “Remove-AppxPackage -name” to remove the package.

Observe!  The name parameter required is the full name of the package that you can find listed for each package in the complete package list, see screenshot in fig 2.

Old App removed

After removing the old package, I could deploy my App again from Visual Studio and actually see it listed with the correct package name in WACK.

And my App passed on the first test run :)

 

Happy coding!

post image

Using Portable Class Library for Azure Mobile Services model

Using Portable class Library to hold your Azure Mobile Services model

If you want to use your model classes in other projects (like for example your upcoming Windows Phone 8 app) you can define them in a Portable Class Library.

Normally when using Azure Mobile Services we use the DataTable attribute on classes to denote their table name when stored in the Azure database.

But if you want to define the model classes from PCL using the DataTable attribute isn’t possible, as the namespace Microsoft.WindowsAzure.MobileServices isn’t available in a PCL.

 
using System.Runtime.Serialization;
using Microsoft.WindowsAzure.MobileServices;

namespace Demo
{
    [DataTable(Name = "users")]
    public class User
    {
        [DataMember(Name = "id")]
        public int Id { get; set; }

        [DataMember(Name = "user_name")]
        public string UserName { get; set; }

        [DataMember(Name = "password")]
        public string Password { get; set; }

        [DataMember(Name = "user_level")]
        public int UserLevel { get; set; }
    }
}

The solution here is to use the DataContract attribute from System.Runtime.Serialization. Make sure though that you got the latest version of the Azure Mobile Services SDK or might not work!

using System.Runtime.Serialization;

namespace Demo
{
    [DataContract(Name = "users")]
    public class User
    {
        [DataMember(Name = "id")]
        public int Id { get; set; }

        [DataMember(Name = "user_name")]
        public string UserName { get; set; }

        [DataMember(Name = "password")]
        public string Password { get; set; }

        [DataMember(Name = "user_level")]
        public int UserLevel { get; set; }

    }
}

For a download of the Azure Mobile Services SDK as a source, visit the GitHub page.

Or use this link for a straight msi download.

post image

VS2010 and TFS2012 not playing nice?

Issues when connecting to TFS2012 from VS2010

I recently upgraded my TFS2010 installation to the new TFS2012, and it has certainly not been all that easy.

Creating a new Team Project

TFS2012 with new Team Project created

The first issue was trying to create new Team Project in VS2012 form VS2010. That turned out to be impossible, and I don’t mean impossible as in hard but as in “it’s not possible yet”.

The only solution to that issue at the moment is to have the Team Project created from a VS2012 installation. There is a recommendation to download Visual Studio 2010 SP1 Team Foundation Server 2012 Compatibility GDR to solve the issue, but that did not work for me (and apparently neither for a lot of other people out there).

Luckily for me I just finished my new Windows 8 machine that’s also running VS2012 for Windows 8 app development. So I could just use that to create a new Team Project in the TFS.

Can’t add new items to a project from VS2010 when working against TFS2012

When the new Team Project finally was created in TFS2012 I added my project and that wen’t smooth, but after going online with the version control, I could not add any items to my code.

I could check out and check in files, but not add new items, existing items or folders, that entire context menu was greyed out for me. After looking at the security settings in my TFS and concluding that everythign should work I started looking on the net, but could not find any information.

In a desperate attempt I installed the  Visual Studio 2010 SP1 Team Foundation Server 2012 Compatibility GDR again, after previously uninstalling in frustration. And that did the trick! After installing the GDR I could finally add new classes and items to my TFS2012 project.

Why use VS2010? (Or: The big Fluster Cluck…)

Windows 8 Start screenAfter building a new great dev machine, installed with Windows 8 and VS2012, why use VS2010 at all?

Because it turns out (and hey, maybe my bad not researching this before) that the development SDK for Windows phone 7.x can’t be installed on Windows 8!!

So, to continue developing my game for Windows Phone 7.x, I needed to keep my old computer running Windows 7 and VS2010 on it.

I don’t know if I should be pissed off here or not really, but one reason why I have stayed with MS tech during most of my career (and hobby projects) is the fact that most often stuff just works nicely together. Not a lot of config, modding or dabbling needed. That plus the fact that MS have produced the best dev tools out there in my opinion, I suspect I’ll get flogged on the internet for saying this, but Eclipse that so many swear by isn’t exactly something I look forward to opening.

I was so totally expecting the Windows Phone 7.x SDK to work on Windows 8 with VS2012 that I didn’t even bother researching it :(

The new Windows Phone SDK for Apollo (WP8) is of course in the making, but so far only a limited amount of app developers are being let in to the testing program. That being the devs with released apps that rank highly, and those belonging to companies that MS is paying to produce apps for the upcoming WP8. This is also something that’s been bothering me as of lately. The Microsoft I like didn’t do stuff like this before.

I miss Brandon Watson, maybe it was just me but it felt like he cared about the entire developer community around Windows Phone, and that meant every single one of us…

Edit: You CAN get the Windows Phone 7 SDK to work on Windows 8

What’s needed is to install the Windows Phone 7.1.1 SDK Update. And if you are doing any kind of XNA development, you’re going to need to install the Games for Windows Marketplace Client.

The recommended install order is:

1. Games for Windows Marketplace Client

2. Windows Phone 7.1 SDK

3. Windows Phone 7.1.1 SDK Update

 

Good luck with the Windows Phone projects :)

 

post image

Error code 0x8007007B when activating Windows 8

Error code 0x8007007B when activating Windows 8

If you want to personalize your copy of Windows 8, you need to activate it first. But if you have a version from  MSDN or TechNet, it might not be as simple as it should.

System centre

When you go to System Centre to activate your copy of Win 8 and see a pre-entered product key that you have not entered yourself

 

 

 

 

 

activation fail

You´ll probably run into Error code 0x8007007B

 

 

 

 

 

 

How to fix it

If you try to activate Windows 8 and get Error code 0x8007007B, this is how to fix it.

right click bottom left in desktop mode

1. In desktop mode, right click the lower left corner and select “Run” (or just press windows+r from either the start screen or the desktop).

 

 

 

 

 

Run slui 3

2. Enter “slui 3″ and press ok.

 

 

 

 

 

Enter new key

3. Enter your correct product key and hit Activate

 

 

 

 

 

 

 

Success

4. And it’s fixed!

 

 

 

 

 

 

 

Want a short power user primer to Windows 8? Check out Scott Hanselman’s post on Windows 8 productivity.

 

Oh btw, firt post from Windows 8! :)