【问题标题】:Why do I not have access to App.xaml.cs from other classes in my app?为什么我无法从我的应用程序中的其他类访问 App.xaml.cs?
【发布时间】:2014-11-27 18:28:16
【问题描述】:

我正在尝试以这种方式在 App.xaml.cs 中声明一个全局变量:

sealed partial class App : Application
{
    public static string SessionKey { get; set; }
    . . .

我在 MainPage.xaml.cs 的地图中添加了一个“Loaded”事件:

    <bm:Map x:Name="galaxyMap" Credentials="ImpeccableOrPerhapsALittleBitPeccable" 
Margin="0,0,0,0" MapType="Birdseye" Loaded="galaxyMap_OnLoaded" >

...哪个事件获得会话密钥,将其分配给全局变量:

async private void galaxyMap_OnLoaded(object sender, RoutedEventArgs args)
{
    try
    {
        App.SessionKey = await visitsMap.GetSessionIdAsync();
    }
    catch { }
}

但是,当我在进行 REST 调用时尝试访问该值时:

String httpqry = String.Format(
    "http://dev.virtualearth.net/REST/v1/Locations?addressLine={0}&locality={1}&postalCode=
{2}&adminDistrict={3}&countryRegion={4}&key={5}",
    addrElements[STREET_ADDRESS], addrElements[LOCALITY], addrElements[POSTAL_CODE], 
addrElements[ST8_PROVINCE], addrElements[COUNTRY]), 
    App.SessionKey)

...我得到一个红色的“SessionKey”,在“App”和“SessionKey”之间的点上显示“Invalid expression term '.'

-and: ";expected" 在同一个地方,和 "Invalid expression term ')'" 在调用结束时的 ")" String.Format()。

为什么“应用程序”无法识别?发生这些错误的类位于同一个命名空间中。为什么它看不到应用程序实例?

【问题讨论】:

  • 需要publiclike public sealed partial class App
  • 好主意,但不,这没有区别。此外,“App”是从 MainPage.xaml.cs 中识别出来的—— OnLoaded() 事件可以很好地看到它。

标签: c# windows-store-apps bing-maps sealed app.xaml


【解决方案1】:

看起来像语法错误。一个 ')' 太多了:

addrElements[国家])

这个')'应该被删除。

【讨论】:

  • 比利克鲁德普,你是对的!当这种情况发生时,我讨厌它!
猜你喜欢
  • 2021-08-07
  • 2018-07-05
  • 1970-01-01
  • 1970-01-01
  • 2020-05-11
  • 2020-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多