开发者

setting Bing Maps Silverlight Control Property From Code Behind

开发者 https://www.devze.com 2023-01-10 18:17 出处:网络
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 do

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.开发者_JS百科

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.


CredentialsProvider = new ApplicationIdCredentialsProvider("AbcdEfghIjklMNnoP_4rlMTclX8iXiNYUYQnG3GPYoxABCDEmoj3cCBemAAG")


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消