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

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

背景

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

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

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

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

代码

x:Type

指定数据类型

cs 复制代码
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();
            }
        }
    }
}
cs 复制代码
<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>
cs 复制代码
<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

为属性设置空值

cs 复制代码
<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

数据提供者

cs 复制代码
<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中

cs 复制代码
<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>

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

相关推荐
拓端研究室4 分钟前
专题:2025人形机器人、工业机器人、智能焊接机器人、扫地机器人产业洞察报告 | 附158+份报告PDF、数据仪表盘汇总下载
microsoft·机器人·pdf
@小红花8 分钟前
从0到1学习Vue框架Day03
前端·javascript·vue.js·学习·ecmascript
mysla25 分钟前
嵌入式学习day45-硬件—汇编
汇编·学习
时光追逐者38 分钟前
C#/.NET/.NET Core技术前沿周刊 | 第 53 期(2025年9.1-9.7)
c#·.net·.netcore
冷冷的菜哥40 分钟前
ASP.NET Core使用MailKit发送邮件
后端·c#·asp.net·发送邮件·mailkit
永日456701 小时前
学习日记-HTML-day51-9.9
前端·学习·html
weixin_447103581 小时前
C#之LINQ
c#·linq
ysn111112 小时前
反编译分析C#闭包
c#
xy_recording2 小时前
Day20 K8S学习
学习·容器·kubernetes
Lynnxiaowen2 小时前
今天继续昨天的正则表达式进行学习
linux·运维·学习·正则表达式·云计算·bash