问题
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.
回答1:
CredentialsProvider = new ApplicationIdCredentialsProvider("AbcdEfghIjklMNnoP_4rlMTclX8iXiNYUYQnG3GPYoxABCDEmoj3cCBemAAG")
回答2:
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.
回答3:
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.
来源:https://stackoverflow.com/questions/3424997/setting-bing-maps-silverlight-control-property-from-code-behind