从代码隐藏设置 Bing 地图 Silverlight 控件属性

发布于 2024-09-12 20:15:16 字数 884 浏览 2 评论 0原文

在页面上加载控件之前,我需要从代码后面设置 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

今天小雨转甜 2024-09-19 20:15:16
CredentialsProvider = new ApplicationIdCredentialsProvider("AbcdEfghIjklMNnoP_4rlMTclX8iXiNYUYQnG3GPYoxABCDEmoj3cCBemAAG")
CredentialsProvider = new ApplicationIdCredentialsProvider("AbcdEfghIjklMNnoP_4rlMTclX8iXiNYUYQnG3GPYoxABCDEmoj3cCBemAAG")
沉默的熊 2024-09-19 20:15:16

经过一番努力,我终于发现当 Thread.CurrentUICulture 设置为不变区域性时会发生这种情况。
确保在 App.Startup 事件处理程序中将其设置为特定区域性(另请考虑设置 Thread.CurrentCulture),例如,

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

当然,您仍然需要使用 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.

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.

梦醒灬来后我 2024-09-19 20:15:16

CredentialsProvider 属性不是字符串类型,并且不会自动将字符串转换为 CredentialsProvider 实例(它如何选择要转换为哪个子类?

)最好从代码中公开 CredentialsProvider 实例。这样,您就可以返回 API 密钥或客户端令牌(可能基于您的配置文件)。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文