2012年3月15日 星期四

Removing the 'Invalid Credentials... ' message from Bing Maps on WP7


How to remove the overlay message you can get when using the Bing Maps control on WP7. Now I've only ever seen this appear for two scenarios - invalid credentials or when in flight mode. It just so happens the control uses the same set of controls to display these messages, example shown below:


Now obviously they don't want you to hide\remove this message :)

The obvious way to remove the 'Invalid credentials...' message is to actual supply valid credentials, but what about if you want to remove the 'Unable to contact server.Please try again' when using the map control in flight mode.

Matt found a previous post that had done this, not sure how this guy was achieving this as the current build for the map control is sealed and access to the RootLayer property is not allowed...

This didn't stop me :) 

So XAML is all about composition - everything is built as layers of UI controls, known as the 'visual tree'. This means you can use extension methods to re-curse the visual tree. I used Colin Eberhardt's linq-to-visual-tree extension methods, available here (LinqToVisualTree.cs).

Here you go...
public partial class MainPage : PhoneApplicationPage
{
    bool removed;
        
    public MainPage()
    {
        InitializeComponent();

        map.ZoomLevel = 8;
        map.Center = new GeoCoordinate(49.109838, -5.976562);
        map.LayoutUpdated += (sender, args) =>
        {
            if (!removed)
            {
                RemoveOverlayTextBlock();
            }
        };
    }

    private void RemoveOverlayTextBlock()
    {
        var textBlock = map.DescendantsAndSelf()
                           .OfType<TextBlock>()
                           .SingleOrDefault(d => d.Text.Contains("Invalid Credentials") ||
                                                 d.Text.Contains("Unable to contact Server"));

        if (textBlock != null)
        {
            var parentBorder = textBlock.Parent as Border;
            if (parentBorder != null)
            {
                parentBorder.Visibility = Visibility.Collapsed;
            }

            removed = true;
        }
    }
}

And you get the following:

2012年3月8日 星期四

Making WPConnect Easier to Use


If you are building a Windows Phone application and you are using your phone to debug AND you are using the Photo Chooser or or the Camera Launcher task you may have found out that it will not let you access the camera or pictures while you are connected to Zune.
But, you have to be connected to Zune to debug on the phone.  Well, kind of.  The Windows Phone Team put out a tool called WPConnect.exe that allows you to dubug on the phone without having Zune open.
Here are the steps.
  1. Connect your phone
  2. Make sure Zune launches and connects to your phone.
  3. Shut down Zune
  4. Using a Dos Prompt, Navigate to
    1. (for 64 bit machines) C:\Program Files (x86)\Microsoft SDKs\Windows Phone\v7.1\Tools\WPConnect\x64
    2. (for 32 Bit machines) C:\Program Files\Microsoft SDKs\Windows Phone\v7.1\Tools\WPConnect\x86
  5. Type WPConnect.exe
It will then tell you that you are good to go.
NOW FOR THE EASY WAY
I got tired of having to navigate using a command prompt (Too may keystrokes) and I use this often enough to matter, so I created a shortcut for the Dos Prompt that takes me right to my designated spot in one click. Here is how you do it.
1. Right-click in the open space of your desktop and click New > Shortcut.
2. For the location, type or copy and paste the following:
%windir%\system32\cmd.exe /k
image
3. Click Next.
4. For the name, type something descriptive, like “Command Prompt for WPConnect” then click Finish.
image
5. Right-click on the new shortcut and choose Properties.
6. Change the “Start In” field to whatever directory you want the command prompt to start in.In my case, I want it to start in the 64 bit folder we talked about above:
"C:\Program Files (x86)\Microsoft SDKs\Windows Phone\v7.1\Tools\WPConnect\x64"

image
Be sure to include the quotation marks, and of course you would need to customize this file path to your own system (32 or 64).
Now when I want to use WPConnect.  I just use the pined shortcut
image
and Type WPConnect.exe
image