SORU
2 EKİM 2008, PERŞEMBE


WPF uygulamasından konsola çıktı?

Konsol kullanıyorum.() WriteLine çok basit WPF test uygulamasından, ama komut satırından uygulamayı çalıştırmak zaman, hiçbir şey konsola yazılan görüyorum. Herkes burada neler oluyor olabilir biliyor mu?

VS WPF uygulamasında 2008, ve sadece Konsol ekleme oluşturarak yeniden yapabilirsiniz.("") Metin yere idam edilecek. WriteLine Herhangi bir fikir?

Şu an ihtiyacım olan tek şey Konsol kadar basit.() WriteLine. Log4net kullanımı ya da diğer günlük çözüm somet olabilir farkındayım, ama gerçekten işlevselliği bu uygulama için o kadar da gerek yok.

Düzenleme:Bu Konsol hatırlamalıydım.() WriteLine konsol uygulamaları için. Oh iyi, aptalca bir soru, değil mi? :-) Sadece Sistem kullanacağım.Tanılama.İz.() WriteLine ve şimdilik DebugView.

CEVAP
5 NİSAN 2009, Pazar


Aslında herhangi bir Konsol aramadan önce bir Konsol penceresinde el ile oluşturmanız gerekir.Yöntemler yazmak. İnit Konsolun düzgün proje türü için WPF uygulama çalışmaz) değiştirmeden çalışma olacak.

İşte ConsoleManager bir sınıf gibi görünebilir, ve/devre dışı bırak Konsolu etkinleştirmek için nasıl kullanılabileceğini tam kaynak kodu örnek bir proje türünden bağımsız olarak.

Aşağıdaki sınıf ile, sadece herhangi bir yerde aramadan önce ConsoleManager.Show() yazma ihtiyacı Console.Write...

[SuppressUnmanagedCodeSecurity]
public static class ConsoleManager
{
    private const string Kernel32_DllName = "kernel32.dll";

    [DllImport(Kernel32_DllName)]
    private static extern bool AllocConsole();

    [DllImport(Kernel32_DllName)]
    private static extern bool FreeConsole();

    [DllImport(Kernel32_DllName)]
    private static extern IntPtr GetConsoleWindow();

    [DllImport(Kernel32_DllName)]
    private static extern int GetConsoleOutputCP();

    public static bool HasConsole
    {
        get { return GetConsoleWindow() != IntPtr.Zero; }
    }

    /// <summary>
    /// Creates a new console instance if the process is not attached to a console already.
    /// </summary>
    public static void Show()
    {
        //#if DEBUG
        if (!HasConsole)
        {
            AllocConsole();
            InvalidateOutAndError();
        }
        //#endif
    }

    /// <summary>
    /// If the process has a console attached to it, it will be detached and no longer visible. Writing to the System.Console is still possible, but no output will be shown.
    /// </summary>
    public static void Hide()
    {
        //#if DEBUG
        if (HasConsole)
        {
            SetOutAndErrorNull();
            FreeConsole();
        }
        //#endif
    }

    public static void Toggle()
    {
        if (HasConsole)
        {
            Hide();
        }
        else
        {
            Show();
        }
    }

    static void InvalidateOutAndError()
    {
        Type type = typeof(System.Console);

        System.Reflection.FieldInfo _out = type.GetField("_out",
            System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);

        System.Reflection.FieldInfo _error = type.GetField("_error",
            System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);

        System.Reflection.MethodInfo _InitializeStdOutError = type.GetMethod("InitializeStdOutError",
            System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);

        Debug.Assert(_out != null);
        Debug.Assert(_error != null);

        Debug.Assert(_InitializeStdOutError != null);

        _out.SetValue(null, null);
        _error.SetValue(null, null);

        _InitializeStdOutError.Invoke(null, new object[] { true });
    }

    static void SetOutAndErrorNull()
    {
        Console.SetOut(TextWriter.Null);
        Console.SetError(TextWriter.Null);
    }
}

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Kim Barbin

    Kim Barbin

    3 Mayıs 2012
  • spectragirlz16's channel

    spectragirlz

    22 Ocak 2012
  • ThreadBanger

    ThreadBanger

    2 Mart 2007