c#和Silverlight:打印文本

我正在开始一个使用Silverlight的新项目,我选择了C#作为语言。到目前为止,我的表现并不太好。我无法找到一种使用C#在屏幕上打印文本的简单方法。 回到C ++(我使用OF)我可以简单地将一些字体加载到一个对象并使用字符串+位置调用一个函数,一切正常。 Silverlight应该是“简单”的东西,所以我认为必须有类似的东西。 你是怎样做的?     
已邀请:
嗯,
TextBlock.Text = "hello world";
?     
Silverlight与您使用的基于控制台的布局系统非常不同。在很多情况下我会认为Silverlight很简单,但当然它不能替代使用printf和绝对定位字符的UI。 我真的建议你阅读以下几个教程: http://www.silverlight.net/learn/quickstarts/ 下面是一个完整的端到端示例,但是我将一个简单的控制台(如应用程序)放在一起(最基本的)。有很多方法可以完成我在下面所做的工作。 它演示了一种间接从另一个类访问UI元素的方法。我建议不要直接将UI元素暴露给其他类。
<UserControl x:Class="SL1Test.MainPage"
    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"
    mc:Ignorable="d" FontSize="16"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="Black">
        <Grid.RowDefinitions>
            <!-- Give first row max height -->
            <RowDefinition Height="*"/>
            <!-- Give second row as little as needed-->
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <!--A giant textblock, in a scrolling area-->
        <ScrollViewer 
            VerticalScrollBarVisibility="Auto"
            HorizontalScrollBarVisibility="Auto">
            <TextBlock x:Name="tbMyConsole" Foreground="Green"
            FontFamily="Courier New" FontWeight="Bold"/>
        </ScrollViewer>
        <Grid Grid.Row="1" >
            <Grid.ColumnDefinitions>
                <!-- Give first column max width -->
                <ColumnDefinition Width="*"/>
                <!-- Give second column as little as needed-->
                <ColumnDefinition Width="Auto"/>
            </Grid.ColumnDefinitions>               
            <TextBox x:Name="txtConsoleInput" Foreground="Green" Background="Black"
                     SelectionForeground="Black"
                     CaretBrush="Green"        
                     SelectionBackground="YellowGreen"/>
            <Button Grid.Column="1"  x:Name="btnSend">Send</Button>
        </Grid>
    </Grid>
</UserControl>
而C#代码隐藏:
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace SL1Test
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();            
            btnSend.Click += new RoutedEventHandler(btnSend_Click);
            txtConsoleInput.KeyUp += new KeyEventHandler(txtConsoleInput_KeyUp);
            AutoTicker ticker = new AutoTicker("AutoTicker", this);
        }

        void txtConsoleInput_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                e.Handled = true;
                ProcessSend();
            }
        }

        void btnSend_Click(object sender, RoutedEventArgs e)
        {
            ProcessSend();
        }

        private void ProcessSend()
        {
            string input = txtConsoleInput.Text;
            ProcessInput(input);
            txtConsoleInput.Text = "";
        }

        public void ProcessInput(string input)
        {
            if (!string.IsNullOrWhiteSpace(input))
            {
                input = DateTime.Now.ToLocalTime() + "> " + input + "n";
                tbMyConsole.Text = tbMyConsole.Text + input;
                txtConsoleInput.Text = "";
            }
        }
    }
}
最后,第二个类将结果发送回第一个类:
using System;
using System.Windows.Threading;

namespace SL1Test
{
    public class AutoTicker
    {
        private MainPage _page;
        private string _caption;

        public AutoTicker(string caption, MainPage page)
        {
            this._page = page;
            this._caption = caption;
            // start a timer to send back fake input
            DispatcherTimer timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(2);
            timer.Tick += new EventHandler(timer_Tick);
            timer.Start();

        }

        void timer_Tick(object sender, EventArgs e)
        {
            _page.ProcessInput(string.Format("{0} @ {1}",
                this._caption,
                DateTime.Now.ToLongTimeString()));
        }
    }
}
    

要回复问题请先登录注册