【问题标题】:The name 'Console' does not exist in the current context In xamarin forms app当前上下文中不存在名称“控制台”在 xamarin 表单应用程序中
【发布时间】:2016-08-23 15:01:16
【问题描述】:

我正在开发 Xamarin Forms 中的应用程序,该应用程序需要从设备获取地理位置数据,然后将地理位置坐标放入 forecast.io URL 我正在使用 James Montemagno 的 Geolocator 插件,我正在使用代码阅读我的建议,但是我收到以下错误 4 次:

当前上下文中不存在名称“控制台”

这是我的代码:

using AppName.Data;
using Xamarin.Forms;
using Plugin.Geolocator;

namespace AppName.Radar
{    
    public partial class RadarHome : ContentPage
    {   
        public RadarHome()
        {    
            var locator = CrossGeolocator.Current;
            locator.DesiredAccuracy = 50;

            var position = await locator.GetPositionAsync(timeout: 10000);

            Console.WriteLine("Position Status: {0}", position.Timestamp);
            Console.WriteLine("Position Latitude: {0}", position.Latitude);
            Console.WriteLine("Position Longitude: {0}", position.Longitude);
            var LatLong = position.Latitude + "," + position.Longitude;

            var browser = new WebView();
            browser.Source = "https://forecast.io/?mobile=1#/f/" + LatLong;

            Content = browser;   
        }
    }
}

我正在使用 Visual Studio Update 3。关于我做错了什么有什么想法吗?

【问题讨论】:

  • 除此之外,如果您尝试使用System.Console,您将缺少using System; 指令。但我不知道 Xamarin Forms 是否存在 System.Console...
  • 您应该改用Debug.WriteLine,因为PCL 中不存在System.Console。类似于这个答案:stackoverflow.com/a/9675897/1048571 / brianlagunas.com/…
  • 在为 Xamarin 开发时,您能否在 VS 中添加一个模块以使用 Console

标签: c# xamarin xamarin.forms


【解决方案1】:

由于您的代码位于具有特定配置文件的 PCL 中,因此 System.Console 不可用。

请改用Debug.WriteLine("Text here"),别忘了加上using System.Diagnostics;

【讨论】:

  • 应该注意的是,这是他们使用的 PCL 配置文件的一个限制,因为它们不附带System.Console,而是Debug.WriteLineContentPage 只是 Xamarin.Forms 中的一个类
  • @JonDouglas Debug.WriteLine 如何替代 Console.WriteLine?他们是独立的。
  • @JonDouglas 更新了答案,但为了解决问题,我只是保持简单。不过谢谢你的提醒。
  • @Sergey.quixoticaxis.Ivanov 他们是独立的。你在这方面是正确的。这是在使用共享 PCL 项目时将相应信息记录到 IDE (XS/VS) Console Output 的替代方法。您可以在底部看到,Console 类中不支持 PCL:msdn.microsoft.com/en-us/library/system.console(v=vs.110).aspx 但是Debug 类中支持 PCL:msdn.microsoft.com/en-us/library/…
  • 用 Debug 替换 Console 非常感谢!
【解决方案2】:

“控制台”功能仅在您创建控制台应用程序(.NET Core)或(.Net Framework)时可用;因此,例如,如果您创建一个空白应用程序,它将无法正常工作。但是,您可以使用 Debug 功能。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-09
    • 2014-05-13
    • 1970-01-01
    相关资源
    最近更新 更多