2017-05-27 94 views
0

看来的Debug.WriteLine不会在Visual Studio 2017年社区版,如下面的链接讨论工作:Console.WriteLine does not output to Output Window in VS 2017什么是替代的Debug.WriteLine为Xamarin窗体PCL在Visual Studio 2017年社区版

任何人都可以提出一个Xamarin的替代形式PCL? 当我看到Debug对象从PCL我只看到的WriteLine方法,所以我很努力,看看如何连接到不同的监听器。

当我看的Xamarin文档System.Diagnostics命名空间中它列出了很多,我似乎没有必须从我的PCL内访问方法和类。例如,'TraceListener'类缺失。这是正确的行为吗?

回答

1

System.Diagnostic.Debug.WriteLine()工作正常,我在PCL项目,一个.NET标准的项目,并使用Visual Studio 2017

你有你自己试了一下Xamarin.Android/Xamarin.iOS项目?

System.Console.WriteLine()但是并不是在PCL项目工作,虽然有无关,与你正在使用的IDE,有更多的事情要做how PCL projects work,但确实在Xamarin.Android/Xamarin.iOS项目工作。如果你想在PCL项目中使用System.Console.WriteLine(),你可以通过各种方法进入本机代码。其中一个将是DependencyService.Get<>()

创建以下类别:

PCL:

public interface IPlatformHelpers { 
    void WriteLine(string text); 
} 

的iOS &安卓

[assembly: Dependency(typeof(PlatformHelpers))] 

namespace App.<iOS | Android> { 

    public class PlatformHelpers : IPlatformHelpers { 

     public void WriteLine(string text) { 
      System.Console.WriteLine(text); 
     } 
    } 
} 

现在您的PCL你可以这样做:

IPlatformHelpers helper = DependencyService.Get<IPlatformHelpers>(); 

helper.WriteLine("Angry BLAH!"); 

我有趣的是,似乎System.Console.WriteLine()可能在.NET标准项目中可用(至少对于.NET Standard 1.3+),尽管我还没有真正尝试过它。

+0

是的,我确定测试过它。你使用的是什么版本的Visual Studio?我认为这个问题可能是Visual Studio 2017 Community Edition特有的,正如我在上面提到的链接中讨论的那样。 –

+0

您的依赖性服务方法可能是我需要的解决方法,似乎适用于Android项目。 事实上,在Android实现我可以使用“的System.Console.WriteLine”或“Android.Util.Log.Debug”。 它们都在VS Output窗口中产生输出。 但是,我找不到任何工作在UWP项目中。 在UWP中我似乎找不到与“System.Console.WriteLine”等效的内容。 如果在UWP实现中使用“System.Diagnostics.Debug.WriteLine”,我不会得到任何输出。 任何想法? –

+0

@AWWaters我正在运行VS 2017 Professional,并没有UWP的经验。你有没有尝试过使用'Trace.WriteLine()'?另外''Debug.WriteLine()'和'Trace.WriteLine()'由项目属性中配置的内容控制。右键单击您UWP项目 - >属性 - >建设 - >然后找**定义DEBUG常数**(应检查调试配置和未选中的RELEASE配置)和**定义TRACE常数**(这应该是检查DEBUG和RELEASE配置)。你说“这似乎不是等值的”,这是否意味着它不会构建? – hvaughan3

相关问题