Umair A.
Umair A.

Reputation: 6863

setting Bing Maps Silverlight Control Property From Code Behind

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.

Upvotes: 0

Views: 2863

Answers (3)

Kent Boogaart
Kent Boogaart

Reputation: 178630

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.

Upvotes: 0

Keith Robertson
Keith Robertson

Reputation: 841

After much travail, I finally discovered that this occurs when the Thread.CurrentUICulture is set to the invariant culture. Be sure it is set to a specific culture (consider also setting Thread.CurrentCulture) in the App.Startup event handler, e.g.

System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en-US");

You do still need the credentials set properly using your AppID, of course. HTH.

Upvotes: 0

David
David

Reputation: 581

CredentialsProvider = new ApplicationIdCredentialsProvider("AbcdEfghIjklMNnoP_4rlMTclX8iXiNYUYQnG3GPYoxABCDEmoj3cCBemAAG")

Upvotes: 6

Related Questions