从代码隐藏设置 Bing 地图 Silverlight 控件属性
在页面上加载控件之前,我需要从代码后面设置 CredentialsProvider。我在代码后面有“ApiKey”依赖属性并将其绑定到 Bing 地图 silverlight 控件,但它不起作用。它在运行时给出错误“无效凭据”。
背后的代码
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>
类名称为 MainPage,继承自 UserControl。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
经过一番努力,我终于发现当 Thread.CurrentUICulture 设置为不变区域性时会发生这种情况。
确保在 App.Startup 事件处理程序中将其设置为特定区域性(另请考虑设置 Thread.CurrentCulture),例如,
当然,您仍然需要使用 AppID 正确设置凭据。 HTH。
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.
You do still need the credentials set properly using your AppID, of course. HTH.
CredentialsProvider
属性不是字符串类型,并且不会自动将字符串转换为CredentialsProvider
实例(它如何选择要转换为哪个子类?)最好从代码中公开
CredentialsProvider
实例。这样,您就可以返回 API 密钥或客户端令牌(可能基于您的配置文件)。The
CredentialsProvider
property isn't of type string and doesn't automatically convert strings to aCredentialsProvider
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.