views:

49

answers:

1

Hi,

I need to set the CredentialsProvider from code behind prior to load the control on page. I have "ApiKey" dependency property in code behind and binding it to Bing Maps silverlight Control but it doesn't work. It gives an error "invalid credentials" at run time.

Code Behind

public static readonly DependencyProperty ApiKeyProperty = DependencyProperty.Register("ApiKey", typeof(string), typeof(MainPage), new PropertyMetadata(""));
protected string ApiKey
{
    get { return this.GetValue(ApiKeyProperty) as string; }
    set { this.SetValue(ApiKeyProperty, value); }
}

XAML

<m:Map x:Name="map" Grid.Row="1" Grid.ColumnSpan="5" Margin="0" CredentialsProvider="{Binding ElementName=silverlightMap, Path=ApiKey}" 
               Mode="Road" MouseMove="map_MouseMove" MouseLeftButtonUp="map_MouseLeftButtonUp" MouseLeftButtonDown="map_MouseLeftButtonDown"
               ViewChangeEnd="map_ViewChangeEnd"></m:Map>

The class name is MainPage and is being inherited from UserControl.

A: 

The CredentialsProvider property isn't of type string and doesn't automatically convert strings to a CredentialsProvider instance (how would it choose which sub-class to convert to?)

You'd be best off exposing a CredentialsProvider instance from your code. That way you can return either an API key or client token, perhaps based on your configuration file.

HTH,
Kent

Kent Boogaart
Yes I had tried this but CredentialsProvider is an abstract class and I can't create an instance of it.
Umair Ashraf
There are two subclasses that you need to choose from depending on how you want to authenticate with Bing. That's the point - a string could convert to either of those two classes, so Bing doesn't automatically convert the string for you. Read the docs: http://msdn.microsoft.com/en-us/library/microsoft.maps.mapcontrol.core.credentialsprovider.aspx
Kent Boogaart