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

2024-08-21 02:36

本文主要是介绍《深入浅出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>

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

这篇关于《深入浅出WPF》读书笔记.4名称空间详解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/1091876

相关文章

HTML5 搜索框Search Box详解

《HTML5搜索框SearchBox详解》HTML5的搜索框是一个强大的工具,能够有效提升用户体验,通过结合自动补全功能和适当的样式,可以创建出既美观又实用的搜索界面,这篇文章给大家介绍HTML5... html5 搜索框(Search Box)详解搜索框是一个用于输入查询内容的控件,通常用于网站或应用程

Python中使用uv创建环境及原理举例详解

《Python中使用uv创建环境及原理举例详解》uv是Astral团队开发的高性能Python工具,整合包管理、虚拟环境、Python版本控制等功能,:本文主要介绍Python中使用uv创建环境及... 目录一、uv工具简介核心特点:二、安装uv1. 通过pip安装2. 通过脚本安装验证安装:配置镜像源(可

C++ 函数 strftime 和时间格式示例详解

《C++函数strftime和时间格式示例详解》strftime是C/C++标准库中用于格式化日期和时间的函数,定义在ctime头文件中,它将tm结构体中的时间信息转换为指定格式的字符串,是处理... 目录C++ 函数 strftipythonme 详解一、函数原型二、功能描述三、格式字符串说明四、返回值五

LiteFlow轻量级工作流引擎使用示例详解

《LiteFlow轻量级工作流引擎使用示例详解》:本文主要介绍LiteFlow是一个灵活、简洁且轻量的工作流引擎,适合用于中小型项目和微服务架构中的流程编排,本文给大家介绍LiteFlow轻量级工... 目录1. LiteFlow 主要特点2. 工作流定义方式3. LiteFlow 流程示例4. LiteF

CSS3中的字体及相关属性详解

《CSS3中的字体及相关属性详解》:本文主要介绍了CSS3中的字体及相关属性,详细内容请阅读本文,希望能对你有所帮助... 字体网页字体的三个来源:用户机器上安装的字体,放心使用。保存在第三方网站上的字体,例如Typekit和Google,可以link标签链接到你的页面上。保存在你自己Web服务器上的字

MySQL存储过程之循环遍历查询的结果集详解

《MySQL存储过程之循环遍历查询的结果集详解》:本文主要介绍MySQL存储过程之循环遍历查询的结果集,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言1. 表结构2. 存储过程3. 关于存储过程的SQL补充总结前言近来碰到这样一个问题:在生产上导入的数据发现

MyBatis ResultMap 的基本用法示例详解

《MyBatisResultMap的基本用法示例详解》在MyBatis中,resultMap用于定义数据库查询结果到Java对象属性的映射关系,本文给大家介绍MyBatisResultMap的基本... 目录MyBATis 中的 resultMap1. resultMap 的基本语法2. 简单的 resul

从基础到进阶详解Pandas时间数据处理指南

《从基础到进阶详解Pandas时间数据处理指南》Pandas构建了完整的时间数据处理生态,核心由四个基础类构成,Timestamp,DatetimeIndex,Period和Timedelta,下面我... 目录1. 时间数据类型与基础操作1.1 核心时间对象体系1.2 时间数据生成技巧2. 时间索引与数据

Mybatis Plus Join使用方法示例详解

《MybatisPlusJoin使用方法示例详解》:本文主要介绍MybatisPlusJoin使用方法示例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,... 目录1、pom文件2、yaml配置文件3、分页插件4、示例代码:5、测试代码6、和PageHelper结合6

一文全面详解Python变量作用域

《一文全面详解Python变量作用域》变量作用域是Python中非常重要的概念,它决定了在哪里可以访问变量,下面我将用通俗易懂的方式,结合代码示例和图表,带你全面了解Python变量作用域,需要的朋友... 目录一、什么是变量作用域?二、python的四种作用域作用域查找顺序图示三、各作用域详解1. 局部作