Россия, г. Ейск |
Опубликован: 01.11.2011 | Уровень: специалист | Доступ: платный
Лекция 6:
Знакомство с XAML
9.2. Разработка интерфейса приложения
На смену традиционным Windows Forms постепенно приходит технология WPF. С помощью xaml можно создавать программы с очень красочным интерфейсом. Рассмотрим пример программы-переводчика. Интерфейс был создан по мотивам приложения, описанного в книге Мэтью Мак-Дональда [20].
Создаем приложение Lecture_6_7.
Код MainWindow.xaml:
<Window x:Class="Lecture_6_7.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid Name="grid1"> <Grid.RowDefinitions> <RowDefinition Height="*"/> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <TextBox Name="txtQuestion" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="10,10,13,10" TextWrapping="Wrap" Grid.Row="0" FontFamily="Verdana" FontSize="24" Foreground="Green"> Введите слово по-английски </TextBox> <Button VerticalAlignment="Top" HorizontalAlignment="Left" Margin="10,0,0,20" Width="127" Height="23" Name="cmdAnswer" Click="cmdTranslate_Click" Grid.Row="1"> Перевести </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"> Здесь будет перевод </TextBox> <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> </Grid> </Window>
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace Lecture_6_7 { class AnswerGenerator { string str; public string getTranslate(string Question) { switch (Question) { case "Apple": str = "Яблоко"; break; case "Lemon": str = "Лимон"; break; case "Orange": str = "Апельсин"; break; case "Mellon": str = "Дыня"; break; case "Water": str = "Вода"; break; case "Bred": str = "Хлеб"; break; case "Butter": str = "Масло"; break; } return str; } } public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void cmdTranslate_Click(object sender, RoutedEventArgs e) { AnswerGenerator generator = new AnswerGenerator(); txtAnswer.Text = generator.getTranslate(txtQuestion.Text); } } }Листинг .
Результат: