当前位置: 首页 > news >正文

《深入浅出WPF》读书笔记.4名称空间详解

《深入浅出WPF》读书笔记.4名称空间详解

背景

主要讲明名称空间概念,可以理解为命名空间的引用。

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

👆如x可以理解为一些列命名空间的引用。

不一一列举,只讲几个特殊的名称空间x:Type x:Null x:Data x:Code

代码

x:Type

指定数据类型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;namespace WpfBookDemo
{public class MyButton:Button{public Type UserWindowType {  get; set; }protected override void OnClick(){base.OnClick();Window win=Activator.CreateInstance(this.UserWindowType) as Window;if(win is not null){win.Show();}}}
}
<Window x:Class="WpfBookDemo.MyWindow"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:WpfBookDemo"mc:Ignorable="d"Title="MyWindow" Height="300" Width="300" ><Grid><StackPanel VerticalAlignment="Center"><TextBox Width="120" Background="LightGreen" Margin="5"></TextBox><TextBox Width="120" Background="LightGreen" Margin="5"></TextBox><TextBox Width="120" Background="LightGreen" Margin="5"></TextBox><Button Width="120" Background="LightGreen" Content="点击一下" Margin="5"></Button></StackPanel></Grid>
</Window>
<Window x:Class="WpfBookDemo.xTypeDemo"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:WpfBookDemo"mc:Ignorable="d"Title="xTypeDemo" Height="450" Width="800"><Grid><local:MyButton x:Name="mbtn" Content="点击一下" Width="120" Height="40"UserWindowType="{x:Type local:MyWindow}"></local:MyButton></Grid>
</Window>

x:Null

为属性设置空值

<Window x:Class="WpfBookDemo.xNullDemo"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:WpfBookDemo"mc:Ignorable="d"Title="xNullDemo" Height="450" Width="300"><Window.Resources><Style x:Key="{x:Type Button}" TargetType="{x:Type Button}"><Setter Property="Width" Value="60"></Setter><Setter Property="Height" Value="36"></Setter><Setter Property="Margin" Value="5"></Setter></Style></Window.Resources><Grid><StackPanel VerticalAlignment="Center"><Button></Button><Button ></Button><Button ></Button><Button  Style="{x:Null}" Content="没有样式"></Button></StackPanel></Grid>
</Window>

x:Data

数据提供者

<Window x:Class="WpfBookDemo.xDataDemo"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:WpfBookDemo"mc:Ignorable="d"Title="xDataDemo" Height="200" Width="300"><Window.Resources><XmlDataProvider x:Key="InventoryData" XPath="Fruits"><x:XData xmlns=""><Fruits ><Fruit Name="Apple">Apple</Fruit><Fruit Name="Banana">Banana</Fruit><Fruit Name="Orange">Orange</Fruit></Fruits></x:XData></XmlDataProvider></Window.Resources><Grid><StackPanel VerticalAlignment="Center" Height="150"><ListBox ItemsSource="{Binding Source={StaticResource InventoryData}, XPath=Fruit}"></ListBox></StackPanel></Grid>
</Window>

x:Code

可以将后端代码挪到xam中

<Window x:Class="WpfBookDemo.xCodeDemo"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:WpfBookDemo"mc:Ignorable="d"Title="xCodeDemo" Height="450" Width="800"><Grid><StackPanel VerticalAlignment="Center" HorizontalAlignment="Center"><Button x:Name="btn" Content="点击一下" Click="btn_Click" Width="120" Height="40"></Button></StackPanel></Grid><x:Code><![CDATA[private void btn_Click(object sender, RoutedEventArgs e){MessageBox.Show("使用x:Code可以将后端代码挪动到前端!");}]]></x:Code>
</Window>

不想写代码,只想干黑悟空,但是菜!

http://www.lryc.cn/news/428780.html

相关文章:

  • 电驱动总成
  • JavaScript class和正则
  • [Linux#42][线程] 锁的接口 | 原理 | 封装与运用 | 线程安全
  • 奇异递归Template有啥奇的?
  • 每天五分钟深度学习框架pytorch:神经网络工具箱nn的介绍
  • 【办公软件】安全风险 Microsoft 已阻止宏运行,因为此文件的来源不受信任
  • JavaScript语法基础之流程结构(顺序、选择、循环结构)
  • 集团数字化转型方案(四)
  • 【MySQL索引】索引失效场景
  • 基于MATLAB视觉的静态手势识别系统
  • day02-作业题
  • torch.cuda.set_divice()
  • <数据集>RSOD数据集<目标检测>
  • 企业高性能web服务器之Nginx
  • 11-sentinel利用nacos作持久化
  • 密码学之哈希算法
  • 杰发科技AC7801——GPIO通过寄存器地址控制高低电平
  • 代码随想录算法训练营第三十一天| 01背包问题 二维 01背包问题 一维 416. 分割等和子集
  • github删除历史所有commit
  • C++前向声明简介
  • 华为手机是越贵越好吗?
  • 【java基础】IDEA 的断点调试(Debug)
  • MPLS相关实验
  • 从零开始学习SLAM(五):极几何与极约束
  • Freertos学习笔记
  • 线程(Thread)的使用方法和锁(同步代码块,lock锁)的问题
  • Java 反射机制
  • 详解MBR分区结构以及GPT分区结构
  • jvm 调优篇
  • Spring AOP应用指南:概念、通知与表达式分析