一、XAML页面基础
使用控件
1.利用工具箱,通过点击拖拽使用工具箱内部的任何控件
2.在XMAL中代码控制,例如添加一个按钮 Button,以下三个代码实现效果相同
代码1
<Button Height="50" Width="100" Content="Click Me!"></Button>
代码2
<Button>
<Button.Height>50</Button.Height>
<Button.Width>100</Button.Width>
<Button.Content>Click Me!</Button.Content>
</Button>
代码3
<Button>
<Button.Height>50</Button.Height>
<Button.Width>100</Button.Width>
<Button.Content>
<TextBlock>Click Me!</TextBlock>
</Button.Content>
</Button>
XMAL的根元素Window
<Window x:Class="WpfApp1.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:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
1.xmlns:xml-namespace,定义命名空间,xmlns为默认命名空间
2.带有“:”的命名空间:xmlns:x, xmlns:d, xmlns:mc, xmlns:local 都与解析XMAL语言相关
3.Title=“MainWindow” Height=“450” Width=“800”,定义了标题、高度和宽度
4. mc:Ignorable,表示运行过程中可以忽略的空间,如<d:Button> 表示运行时,该按钮即被忽略
对应的C#实现代码
MainWindow.xaml.cs
namespace WpfApp1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();//初始化页面
}
}
}
利用C#代码进行页面控件的操作
依旧是添加一个按钮为例
namespace WpfApp1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var grid = (Grid)this.Content;
Button button = new Button();
button.Height = 50;
button.Width = 100;
Margin = new Thickness(0, 0, 700, 385);
button.Content = "This is a Button!";
grid.Children.Add(button);
}
}
}
但是仍然建议使用XAML对页面布局进行控制!!!
*二、(拓展)MVC架构
什么是MVC?
-
[ 软件工程的架构方式 ]
-
[ 模型(Model)、视图(View)、控制器(Controller) ]
-
[ 分离业务操作、数据显示、逻辑控制 ]
视图 View -
[ 用户交互界面 ]
-
[ 仅展示数据,不处理数据 ]
-
[ 接受用户输入 ]
模型 Model -
[ MVC架构核