大家都知道:UI是做好一个软件很重要的因素,如果没有一个漂亮的UI,功能做的再好也无法吸引很多用户使用,而且没有漂亮的界面,那么普通用户会感觉这个软件没有多少使用价值。
WPF系统基于流布局的标准,开发人员可以建立与分辨率和窗口大小无关的界面UI,在不同显示器上可以得到自由缩放。在开发过程中应该注意以下原则:
1,不需要设置元素尺寸大小
2,不需要使用坐标指定元素位置
3,嵌套布局容器
4,一个窗口包含一个元素
控件很多,不需要个个都精确学会
今天来掌握几个简单的东西,Button按钮,Label标签,TextBox文本,PasswordBox密码,RadioButton单选,首先在项目中创建文件夹images,复制几个图片到文件夹中。
第一步,在xaml文件中添加如下代码
<Window x:Class="WpfApp6.MainWindow"
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"
xmlns:local="clr-namespace:WpfApp6"
mc:Ignorable="d"
Title="WPF基础控件" Height="758.275" Width="1078.5" Icon="images/22i.jpg" WindowStartupLocation="CenterScreen" Loaded="Window_Loaded">
<Grid Name="mygrid" >
<Button Name="btnlogin" IsDefault="True" Click="btnlogin_Click" Background="YellowGreen" BorderThickness="3" BorderBrush="Red" Content="登录" HorizontalAlignment="Left" Margin="166,95,0,0" VerticalAlignment="Top" Width="74" RenderTransformOrigin="-0.2,0.368"/>
<Label Content="帐号" HorizontalAlignment="Left" HorizontalContentAlignment="Center" Margin="54,23,0,0" VerticalAlignment="Top" Width="102"/>
<TextBox Name="txtname" HorizontalAlignment="Left" Height="23" Margin="142,23,0,0" TextWrapping="Wrap" Text="admin" VerticalAlignment="Top" Width="120"/>
<Label Content="密码" HorizontalAlignment="Left" Margin="276,23,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.088,0.6"/>
<PasswordBox Name="txtpass" HorizontalAlignment="Left" Height="22" Margin="330,23,0,0" Password="123456" PasswordChar="#" VerticalAlignment="Top" Width="120"/>
<Button Name="btncancle" IsCancel="True" Foreground="Chartreuse" FontSize="14" Click="btncancle_Click" Content="取消" HorizontalAlignment="Left" Margin="266,95,0,0" VerticalAlignment="Top" Width="74">
<Button.Background>
<ImageBrush ImageSource="images/32s.jpg"></ImageBrush>
</Button.Background>
</Button>
<RadioButton Content="老师" GroupName="role" HorizontalAlignment="Left" Margin="156,65,0,0" VerticalAlignment="Top" Checked="RadioButton_Checked"/>
<RadioButton Content="学生" GroupName="role" HorizontalAlignment="Left" Margin="230,65,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.14,0.357" Checked="RadioButton_Checked"/>
<RadioButton Content="管理员" GroupName="role" HorizontalAlignment="Left" Margin="303,65,0,0" VerticalAlignment="Top" Checked="RadioButton_Checked"/>
</Grid>
</Window>
所有控件都可以在左侧窗口中找到
说明下:
1、设置了窗口的图标,启动位置在正中间,启动事件中动态添加一个单选按钮
2、Grid设置了名称, Name="mygrid",Button设置了背景,RadioButton设置了分组,控件都设置了Content表示显示内容,Name表示控件名称,这在后台代码可能需要用到,所以设置了名称,HorizontalAlignment表示水平对齐,Foreground表示前景色,Margin表示左上右下边距,跟CSS的盒子模型意思一样,Click表示事件名称,这些属性名称很多,不一一说明,这些东西跟CSS里的东西含义一样,所以证明了这个xaml文件是布局文件
第二步,在.cs文件中写逻辑代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
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 WpfApp6
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void btncancle_Click(object sender, RoutedEventArgs e)
{
}
private void btnlogin_Click(object sender, RoutedEventArgs e)
{
string uname = txtname.Text.Trim();
string passwd = txtpass.Password.Trim();
MessageBox.Show("登录成功","登录提示",MessageBoxButton.OK,MessageBoxImage.Information);
}
private void RadioButton_Checked(object sender, RoutedEventArgs e)
{
string t = (sender as RadioButton).Content.ToString();
MessageBox.Show(t);
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
//动态添加一个单选按钮
RadioButton radbtn = new RadioButton();
radbtn.Content = "主任";
radbtn.GroupName = "role";
radbtn.HorizontalAlignment = HorizontalAlignment.Left;
radbtn.VerticalAlignment = VerticalAlignment.Top;
radbtn.Margin = new Thickness(375,64, 0, 0);
radbtn.Checked += RadioButton_Checked;//指定事件处理
mygrid.Children.Add(radbtn);
}
}
}
第三步,运行程序,效果是
别小看这些演练,那可是万丈高楼拔地而起,一步步堆砌,就是摩天楼。
效果还可以,看到了炫彩,确实眼前一亮,比winform厉害得多,得加鸡腿了!