In this article I present a code from the book “Pro WPF 4.5. in C#“, which I consider useful and interesting. The application concerns building a simple program where you may ask a yes-no question and get a random answer. It looks like this:
So, what do we have in this application?
- A XAML code;
- A code, associated with the button click, within the XAML;
- A class, where we keep the strings for the prediction and a method for a random answer.
With these three simple building blocks, we achieve a simple application. Let’s examine them. The XAML looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<Window x:Class="EightBall.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Eight Ball Answer" Height="328" Width="412" > <Grid> <Grid.RowDefinitions> <RowDefinition Height="*" /> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <Grid.Background> <LinearGradientBrush> <LinearGradientBrush.GradientStops> <GradientStop Offset="0.00" Color="Red" /> <GradientStop Offset="0.50" Color="Indigo" /> <GradientStop Offset="1.00" Color="Violet" /> </LinearGradientBrush.GradientStops> </LinearGradientBrush> </Grid.Background> <TextBox VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="10,10,13,10" Name="txtQuestion" TextWrapping="Wrap" FontFamily="Verdana" FontSize="24" Grid.Row="0" > [Place question here.] </TextBox> <Button VerticalAlignment="Top" HorizontalAlignment="Left" Margin="10,0,0,20" Width="127" Height="23" Name="cmdAnswer" Click="cmdAnswer_Click" Grid.Row="1"> Ask the Eight Ball </Button> <TextBox VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="10,10,13,10" Name="txtAnswer" TextWrapping="Wrap" IsReadOnly="True" FontFamily="Verdana" FontSize="24" Foreground="Green" Grid.Row="2"> [Answer will appear here.] </TextBox> </Grid> </Window> |
After declaring the namespaces and the title, we build a grid. Three rows are defined: the first and the last ones are equal (that is seen from lines 7 to 9). The colorful effect is obtained through the GradientStop property of LinearGradientBrush (lines 13 to 19). Then we notice the three elements, put in the three rows – TextBox (defined on row 20), Button (on row 26) and a new TextBox (on row 32). Thus in simply 40 lines we have “painted” with code our XAML interface.
Once, the button is clicked, the code behind the XAML is activated. This is how it looks like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public Window1() { InitializeComponent(); } private void cmdAnswer_Click(object sender, RoutedEventArgs e) { // Dramatic delay... this.Cursor = Cursors.Wait; System.Threading.Thread.Sleep(TimeSpan.FromSeconds(5)); AnswerGenerator generator = new AnswerGenerator(); txtAnswer.Text = generator.GetRandomAnswer(txtQuestion.Text); this.Cursor = null; } |
What do we have there? The required “InitializeComponent()” method. Then simply a method, telling the application how to react upon a clicked button. In order to make it dramatic :), the first two lines of the method initiate a small waiting, thus you get the impression that the application is actually thinking upon your question. Then a new object from the class “AnswerGenerator”, named “generator” is created. Then the property “Text” of the textbox, named “txtAnswer” receives the result from the method “GetRandomAnswer”, implemented over the object “generator”, containing in himself the text from the text question. At the end the cursor is fixed.
The last part of the application building is the building of the class “AnswerGenerator”. In this class we see one array of strings, containing the answers and a method “GetRandomAnswer(string)”. This method is accessed from the code behind in XAML. It returns a random string from the array answers. This is how it looks like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
class AnswerGenerator { string[] answers = new string[]{ "Ask Again later","Can Not Predict Now","Without a Doubt", "Is Decidely So","Concentrate and Ask Again","My Sources Say No", "Yes, Definitely","Don't Count On It","Signs Point to Yes", "Better Not Tell You Now","Outlook Not So Good","Most Likely", "Very Doubtful","As I See It, Yes","My Reply is No","It Is Certain", "Yes","You May Rely On It","Outlook Good","Reply Hazy Try Again","Vitoshacademy.com" }; public string GetRandomAnswer(string question) { Random rnd = new Random(); return answers[rnd.Next(0, answers.Length)]; } } |
In this example you have seen the presentation of a simple application. Its target is to show you what can be done with WPF C#. And it is quite a lot!
Enjoy the code (which is not actually mine) & have a good day 🙂