Reputation: 11303
I am starting a new project that is using Silverlight and I have chosen C# as the language. So far I'm not doing too great. I can't manage to find a simple way of printing text on screen using C#.
Back in C++ (I was using OF) I could simply load some font to an object and call a function with string + position and everything worked just fine. Silverlight is supposed to be something "simple" so I assume there must be something like that as well.
How do you do that?
Upvotes: 1
Views: 763
Reputation: 59793
Silverlight is very different from the console based layout system you were using. I would consider Silverlight simple in many cases, but certainly it's not great as a replacement for a UI that uses printf and absolute positioned characters.
I'd really suggest you go through a few of these tutorials:
http://www.silverlight.net/learn/quickstarts/
Below is a complete end to end example however of a simple console like application that I threw together (rudimentary at best). There are a lot of ways to accomplish what I've done below.
It demonstrates one way to access UI elements, indirectly, from another class. I'd suggest not directly exposing UI elements to other classes as I demonstrate.
<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>
And the C# code-behind:
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 = "";
}
}
}
}
And finally, a second class which sends results back to the first class:
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()));
}
}
}
Upvotes: 0