Mvvm跨Mvx.Trace的用法 [英] MvvmCross Mvx.Trace usage

查看:56
本文介绍了Mvvm跨Mvx.Trace的用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MvvmCross. 在库中,我看到了Mvx.Trace方法的用法,但没有输出到控制台/输出窗口. 如何使用?

I'm using MvvmCross. In the library I see usage of Mvx.Trace method, but no output to the console/output window. How to use it?

p.s.我已经设置了编译器常量Trace = true

p.s. I have set the compiler constant Trace = true

谢谢.

推荐答案

MvvmCross DebugTrace均通过IMvxTrace

MvvmCross DebugTrace is all routed via a singleton implementation of IMvxTrace

每个平台都提供了不同的实现方式:

Each platform provides different implementations of this:

  • Windows平台使用Debug跟踪(因为Trace本身并非在所有Windows平台上都可用)
  • Android使用Debug和Android log
  • 的双重日志
  • iOS使用Debug和iOS Console
  • 的双重日志
  • the Windows platforms use Debug trace (because Trace itself is not available on all the Windows platforms)
  • Android uses a dual log of both Debug and the Android log
  • iOS uses a dual log of both Debug and the iOS Console

这在MvvmCross的早期版本中效果很好,尤其是在人们直接链接到MvvmCross源代码的地方,因此可以直接绑定到调试或发布MvvmCross二进制文件.

This worked well in earlier versions of MvvmCross, especially where people linked directly to the MvvmCross source code and so could bind directly to either the debug or release MvvmCross binaries.

但是...由于人们现在越来越多地使用Nuget发行的Binaries-当然已经编译了Debug,因此人们常常会问如何使用它"?

However... because people are now increasingly using the release Binaries from Nuget - in which Debug is of course compiled out, then this often leaves people asking "How to use it?"

最简单的方法是让您在Setup类中覆盖IMvxTrace的初始化.

The easiest way is for you to override the initialisation of IMvxTrace in your Setup class.

这很容易做到-例如:

   protected override IMvxTrace CreateDebugTrace() { return new MyDebugTrace(); }

其中MyDebugTrace类似于:

public class MyDebugTrace : IMvxTrace
{
    public void Trace(MvxTraceLevel level, string tag, Func<string> message)
    {
        Debug.WriteLine(tag + ":" + level + ":" + message());
    }

    public void Trace(MvxTraceLevel level, string tag, string message)
    {
        Debug.WriteLine(tag + ":" + level + ":" + message);
    }

    public void Trace(MvxTraceLevel level, string tag, string message, params object[] args)
    {
        try
        {
            Debug.WriteLine(string.Format(tag + ":" + level + ":" + message, args));
        }
        catch (FormatException)
        {
            Trace(MvxTraceLevel.Error, tag, "Exception during trace of {0} {1} {2}", level, message);
        }
    }
}

如果要包括用于调试/发布的不同实现,请执行以下操作:如果您想在运行时打开/关闭此功能;如果要在MvxTraceLeveltag上进行过滤;或者,如果您想使用某些第三方日志记录引擎,那么在每个平台上使用简单的C#也应该很容易做到.

If you wanted to include different implementations for debug/release; if you wanted to switch this on/off at runtime; if you wanted to filter on MvxTraceLevel or tag; or if you wanted to use some third party logging engine then this should also be easy to do using simple C# on each platform.

这篇关于Mvvm跨Mvx.Trace的用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆