如何模拟在WPF控制台? [英] How to emulate a console in WPF?

查看:139
本文介绍了如何模拟在WPF控制台?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一些技巧,在最正确的方向,甚至准备解决这个问题,我敢卡住(我只是初级/中级):

I'd like some tips-in-the-right-direction or even ready solutions to this problem and I'm pretty stuck (I'm just beginner/intermediate):

我想实现我的应用程序中的SSH。 SSH的后端工作正常等,但我被困在前端。什么WPF的组合会提出我一个适当的解决方案来模拟控制台?抛开一个完整的终端仿真,我很乐意简单的readline /成的WriteLine东西,看起来像一个控制台: - )

I'm trying to implement a SSH in my application. The SSH-backend works fine and such, but I'm stuck at the frontend. What WPF-Combination would present me with an adequate solution to emulate a console? Put aside a complete terminal-emulation, I'd be happy to simply readline/writeline into something that looks like a console :-)

我的最好的办法尚未是一个80x50的而导致在4000单个细胞单个字符网格,而且感觉就像一个总矫枉过正。

My best approach yet was a 80x50 Grid of single characters resulting in 4000 single cells and that feels like a total overkill.

另一个想法是做一个控制台申请。绑定到另一个项目的WPF的窗口。但是...是,即使可能,如何?

Another idea was to make a console-Appl. bound to a wpf-window in another project. But...is that even possible and how?

推荐答案

既然你想要的的emulate 的一控制台,我不喜欢这样。请注意,你必须处理的命令和输出结果自己。

Given that you want to emulate a console, I'd do it like this. Note that you'd have to handle the commands and outputting the results yourself.

的Page.xaml

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>



page.xaml.cs

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));
    }
}

这篇关于如何模拟在WPF控制台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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