[英]No output to console from a WPF application?
我在一個非常簡單的 WPF 測試應用程序中使用Console.WriteLine()
,但是當我從命令行執行應用程序時,我沒有看到任何內容寫入控制台。 有誰知道這里會發生什么?
我可以通過在 VS 2008 中創建一個 WPF 應用程序來重現它,只需在它執行的任何地方添加Console.WriteLine("text")
即可。 有任何想法嗎?
我現在需要的只是像Console.WriteLine()
這樣簡單的東西。 我意識到我可以使用 log4net 或其他一些日志記錄解決方案,但我真的不需要這個應用程序的那么多功能。
編輯:我應該記得Console.WriteLine()
是用於控制台應用程序的。 哦,沒有愚蠢的問題,對吧? :-) 我現在只使用System.Diagnostics.Trace.WriteLine()
和 DebugView。
您可以使用
Trace.WriteLine("text");
這將輸出到 Visual Studio 中的“輸出”窗口(調試時)。
確保包含診斷程序集:
using System.Diagnostics;
右鍵單擊項目,“屬性”,“應用程序”選項卡,將“輸出類型”更改為“控制台應用程序”,然后它也會有一個控制台。
在實際調用任何 Console.Write 方法之前,您必須手動創建一個控制台窗口。 這將使控制台在不更改項目類型的情況下正常工作(這對於 WPF 應用程序將不起作用)。
這是一個完整的源代碼示例,說明了 ConsoleManager 類的外觀,以及如何獨立於項目類型使用它來啟用/禁用控制台。
使用以下類,您只需要在調用Console.Write
之前在某處編寫ConsoleManager.Show()
...
[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);
}
}
舊帖子,但我遇到了這個,所以如果你想在 Visual Studio 的 WPF 項目中輸出一些東西到輸出,當代的方法是:
包括這個:
using System.Diagnostics;
接着:
Debug.WriteLine("something");
盡管 John Leidegren 不斷否定這個想法,但 Brian 是正確的。 我剛剛在 Visual Studio 中使用它。
需要明確的是,WPF 應用程序默認不會創建控制台窗口。
您必須創建一個 WPF 應用程序,然后將 OutputType 更改為“控制台應用程序”。 當您運行該項目時,您將看到一個控制台窗口,它前面有您的 WPF 窗口。
它看起來不是很漂亮,但我發現它很有幫助,因為我希望我的應用程序從命令行運行並在那里提供反饋,然后對於某些命令選項,我會顯示 WPF 窗口。
我使用 Console.WriteLine() 在輸出窗口中使用...
我創建了一個解決方案,混合了各種帖子的信息。
它是一種表單,包含一個標簽和一個文本框。 控制台輸出被重定向到文本框。
還有一個叫做 ConsoleView 的類,它實現了三個公共方法:Show()、Close() 和 Release()。 最后一個是保持打開控制台並激活關閉按鈕以查看結果。
這些表格稱為 FrmConsole。 下面是 XAML 和 c# 代碼。
使用非常簡單:
ConsoleView.Show("Title of the Console");
用於打開控制台。 采用:
System.Console.WriteLine("The debug message");
用於向控制台輸出文本。
采用:
ConsoleView.Close();
對於關閉控制台。
ConsoleView.Release();
保持打開控制台並啟用關閉按鈕
XAML
<Window x:Class="CustomControls.FrmConsole"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:CustomControls"
mc:Ignorable="d"
Height="500" Width="600" WindowStyle="None" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" Topmost="True" Icon="Images/icoConsole.png">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="40"/>
<RowDefinition Height="*"/>
<RowDefinition Height="40"/>
</Grid.RowDefinitions>
<Label Grid.Row="0" Name="lblTitulo" HorizontalAlignment="Center" HorizontalContentAlignment="Center" VerticalAlignment="Center" VerticalContentAlignment="Center" FontFamily="Arial" FontSize="14" FontWeight="Bold" Content="Titulo"/>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="10"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="10"/>
</Grid.ColumnDefinitions>
<TextBox Grid.Column="1" Name="txtInner" FontFamily="Arial" FontSize="10" ScrollViewer.CanContentScroll="True" VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Visible" TextWrapping="Wrap"/>
</Grid>
<Button Name="btnCerrar" Grid.Row="2" Content="Cerrar" Width="100" Height="30" HorizontalAlignment="Center" HorizontalContentAlignment="Center" VerticalAlignment="Center" VerticalContentAlignment="Center"/>
</Grid>
窗口代碼:
partial class FrmConsole : Window
{
private class ControlWriter : TextWriter
{
private TextBox textbox;
public ControlWriter(TextBox textbox)
{
this.textbox = textbox;
}
public override void WriteLine(char value)
{
textbox.Dispatcher.Invoke(new Action(() =>
{
textbox.AppendText(value.ToString());
textbox.AppendText(Environment.NewLine);
textbox.ScrollToEnd();
}));
}
public override void WriteLine(string value)
{
textbox.Dispatcher.Invoke(new Action(() =>
{
textbox.AppendText(value);
textbox.AppendText(Environment.NewLine);
textbox.ScrollToEnd();
}));
}
public override void Write(char value)
{
textbox.Dispatcher.Invoke(new Action(() =>
{
textbox.AppendText(value.ToString());
textbox.ScrollToEnd();
}));
}
public override void Write(string value)
{
textbox.Dispatcher.Invoke(new Action(() =>
{
textbox.AppendText(value);
textbox.ScrollToEnd();
}));
}
public override Encoding Encoding
{
get { return Encoding.UTF8; }
}
}
//DEFINICIONES DE LA CLASE
#region DEFINICIONES DE LA CLASE
#endregion
//CONSTRUCTORES DE LA CLASE
#region CONSTRUCTORES DE LA CLASE
public FrmConsole(string titulo)
{
InitializeComponent();
lblTitulo.Content = titulo;
Clear();
btnCerrar.Click += new RoutedEventHandler(BtnCerrar_Click);
Console.SetOut(new ControlWriter(txtInner));
DesactivarCerrar();
}
#endregion
//PROPIEDADES
#region PROPIEDADES
#endregion
//DELEGADOS
#region DELEGADOS
private void BtnCerrar_Click(object sender, RoutedEventArgs e)
{
Close();
}
#endregion
//METODOS Y FUNCIONES
#region METODOS Y FUNCIONES
public void ActivarCerrar()
{
btnCerrar.IsEnabled = true;
}
public void Clear()
{
txtInner.Clear();
}
public void DesactivarCerrar()
{
btnCerrar.IsEnabled = false;
}
#endregion
}
ConsoleView 類的代碼
static public class ConsoleView
{
//DEFINICIONES DE LA CLASE
#region DEFINICIONES DE LA CLASE
static FrmConsole console;
static Thread StatusThread;
static bool isActive = false;
#endregion
//CONSTRUCTORES DE LA CLASE
#region CONSTRUCTORES DE LA CLASE
#endregion
//PROPIEDADES
#region PROPIEDADES
#endregion
//DELEGADOS
#region DELEGADOS
#endregion
//METODOS Y FUNCIONES
#region METODOS Y FUNCIONES
public static void Show(string label)
{
if (isActive)
{
return;
}
isActive = true;
//create the thread with its ThreadStart method
StatusThread = new Thread(() =>
{
try
{
console = new FrmConsole(label);
console.ShowDialog();
//this call is needed so the thread remains open until the dispatcher is closed
Dispatcher.Run();
}
catch (Exception)
{
}
});
//run the thread in STA mode to make it work correctly
StatusThread.SetApartmentState(ApartmentState.STA);
StatusThread.Priority = ThreadPriority.Normal;
StatusThread.Start();
}
public static void Close()
{
isActive = false;
if (console != null)
{
//need to use the dispatcher to call the Close method, because the window is created in another thread, and this method is called by the main thread
console.Dispatcher.InvokeShutdown();
console = null;
StatusThread = null;
}
console = null;
}
public static void Release()
{
isActive = false;
if (console != null)
{
console.Dispatcher.Invoke(console.ActivarCerrar);
}
}
#endregion
}
我希望這個結果有用。
看看這個帖子,對自己很有幫助。 下載代碼示例:
http://www.codeproject.com/Articles/335909/Embedding-a-Console-in-aC-Application
我最近也有這個問題。
如果 Brians 解決方案(在項目設置中更改為控制台應用程序)沒有解決問題:嘗試將目標框架從 .NET 5.0 更改為 .NET Core 3.1。
據我所知,Console.WriteLine()僅適用於控制台應用程序。 我想這是你的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.