C#学习(八)——WPF基础入门

一、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架构核

  • 8
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值