Fra*_*umb 12 c# wpf ssh console emulation
我想要一些正确的方向,甚至是解决这个问题的方法,而且我很困难(我只是初学者/中级):
我正在尝试在我的应用程序中实现SSH.SSH后端工作正常等,但我被困在前端.WPF-Combination会给我一个足够的模拟控制台的解决方案吗?抛开一个完整的终端仿真,我很乐意将readline/writeline简单地写成一个看起来像控制台的东西:-)
我最好的方法是80x50单个字符网格,导致4000个单细胞,感觉就像一个完全矫枉过正.
另一个想法是制作一个控制台-Appl.绑定到另一个项目中的wpf窗口.但是......这是可能的,怎么样?
Zom*_*eep 33
鉴于你想模仿一个控制台,我会这样做.请注意,您必须自己处理命令并输出结果.
page.xaml
<Window x:Class="ConsoleEmulation.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" MinHeight="350" MinWidth="525" Height="350" Width="525">
<Grid>
<ScrollViewer Name="Scroller" Margin="0" Background="Black">
<StackPanel>
<ItemsControl ItemsSource="{Binding ConsoleOutput, Mode=OneWay}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=.}" Foreground="White" FontFamily="Consolas"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<TextBox Text="{Binding ConsoleInput, Mode=TwoWay}" Background="Black" Foreground="White" FontFamily="Consolas" Name="InputBlock" BorderBrush="{x:Null}" SelectionBrush="{x:Null}" />
</StackPanel>
</ScrollViewer>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
page.xaml.cs
public partial class MainWindow : Window
{
ConsoleContent dc = new ConsoleContent();
public MainWindow()
{
InitializeComponent();
DataContext = dc;
Loaded += MainWindow_Loaded;
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
InputBlock.KeyDown += InputBlock_KeyDown;
InputBlock.Focus();
}
void InputBlock_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
dc.ConsoleInput = InputBlock.Text;
dc.RunCommand();
InputBlock.Focus();
Scroller.ScrollToBottom();
}
}
}
public class ConsoleContent : INotifyPropertyChanged
{
string consoleInput = string.Empty;
ObservableCollection<string> consoleOutput = new ObservableCollection<string>() { "Console Emulation Sample..." };
public string ConsoleInput
{
get
{
return consoleInput;
}
set
{
consoleInput = value;
OnPropertyChanged("ConsoleInput");
}
}
public ObservableCollection<string> ConsoleOutput
{
get
{
return consoleOutput;
}
set
{
consoleOutput = value;
OnPropertyChanged("ConsoleOutput");
}
}
public void RunCommand()
{
ConsoleOutput.Add(ConsoleInput);
// do your stuff here.
ConsoleInput = String.Empty;
}
public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged(string propertyName)
{
if (null != PropertyChanged)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
Run Code Online (Sandbox Code Playgroud)
您是否知道可以使用 AllocConsole 从应用程序显示控制台窗口?
这是创建“双模式”应用程序的简单方法,可以是控制台或 Windows 窗体应用程序。
[DllImport("kernel32")]
static extern bool AllocConsole();
Run Code Online (Sandbox Code Playgroud)
或者你可以使用这个:
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<TextBlock Text="Console contents..." HorizontalAlignment="Stretch" VerticalAlignment="Stretch" x:Name="ConsoleTextBlock"/>
<DockPanel Grid.Row="1">
<TextBox/>
</DockPanel>
</Grid>
Run Code Online (Sandbox Code Playgroud)
为了获得更好的外观,请将 TextBlock 替换为 ListBox,并相应地设置 ItemTemplate 的样式。
归档时间: |
|
查看次数: |
25093 次 |
最近记录: |