【问题标题】:setting Bing Maps Silverlight Control Property From Code Behind从后面的代码设置 Bing 地图 Silverlight 控件属性
【发布时间】:2011-09-28 00:50:34
【问题描述】:

在页面上加载控件之前,我需要从后面的代码中设置 CredentialsProvider。我在后面的代码中有“ApiKey”依赖属性并将其绑定到 Bing Maps silverlight Control,但它不起作用。它在运行时给出错误“无效凭据”。

代码背后

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。

【问题讨论】:

  • 您是否确认 ApiKey 属性 get 正在返回您的有效密钥?
  • 我的这个问题太老了。我通过不使用它解决了这个问题。我会在几天内标记答案检查谁获得最高票数。

标签: c# silverlight bing-maps


【解决方案1】:
CredentialsProvider = new ApplicationIdCredentialsProvider("AbcdEfghIjklMNnoP_4rlMTclX8iXiNYUYQnG3GPYoxABCDEmoj3cCBemAAG")

【讨论】:

    【解决方案2】:

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

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

    当然,您仍然需要使用您的 AppID 正确设置凭据。 HTH。

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

      • 是的,我试过了,但 CredentialsProvider 是一个抽象类,我无法创建它的实例。
      • 您需要从两个子类中进行选择,具体取决于您希望使用 Bing 进行身份验证的方式。这就是重点 - 字符串可以转换为这两个类中的任何一个,因此 Bing 不会自动为您转换字符串。阅读文档:msdn.microsoft.com/en-us/library/…
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-28
      • 1970-01-01
      • 2012-09-30
      • 1970-01-01
      • 2020-09-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多