WPF|--XAML|--使用其它名称空间中类型

关键字词




XAML -- 使用其它名称空间中类型

语法格式

<!-- {}括起来的表示占位符 -->
xmlns:{Prefix}="clr-namespace:{Namespace};assembly={AssemblyName}"

示例

<!-- 演示如何访问System名称空间中的基本类型,并将其映射为前缀"sys_mine:" -->
<UserControl x:Class="WpfApp1.Views.SystemNamespaceUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:WpfApp1.Views"
             xmlns:sys_mine="clr-namespace:System;assembly=System.Runtime"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <Button Width="500" Height="50">
            <sys_mine:String>使用System.Runtime命名空间下的String类型</sys_mine:String>
        </Button>
    </Grid>
</UserControl>

<UserControl x:Class="WpfApp1.Views.SystemNamespaceUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:WpfApp1.Views"
             xmlns:sys_mine="clr-namespace:System;assembly=System.Runtime"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <ListBox Width="400" Height="200">
            <ListBoxItem>
                <sys_mine:DateTime>02/06/2024 18:00</sys_mine:DateTime>
            </ListBoxItem>
            <ListBoxItem>
                <sys_mine:DateTime>02/06/2024 18:10</sys_mine:DateTime>
            </ListBoxItem>
            <ListBoxItem>
                <sys_mine:DateTime>02/06/2024 18:20</sys_mine:DateTime>
            </ListBoxItem>
        </ListBox>
    </Grid>
</UserControl>

问题1 -- 未找到程序集“mscorlib;”。

<!-- 项目属性 -->
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net7.0-windows</TargetFramework>
    <Nullable>enable</Nullable>
    <UseWPF>true</UseWPF>
  </PropertyGroup>
</Project>
<UserControl x:Class="WpfApp1.Views.SystemNamespaceUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:WpfApp1.Views"
             xmlns:sys="clr-namespace:System;assembly=mscorlib;"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
<!-- 有报错信息的 -->
    </Grid>
</UserControl>
# 报错信息

错误	
XLS0418	未找到程序集“mscorlib;”。请确保不缺少程序集引用并且已生成你的项目和所有引用的程序集。

书中使用的框架是.NET 4.5, 我这个用的是.NET7.0, 可能因为这个原因.

问题2 -- System.Runtime Vs System Vs mscorlib ...


这错中复杂的关系, 也有一定的历史渊源...

在.NET Framerwork4.5下, 是有"mscorlib"程序集的,书中的示例就是👇
xmlns:sys="clr-namespace:System;assembly=mscorlib;"

但是在.NET 7下, 如果还是按照.NET Framework4.5下直接用"mscorlib"的话, 是会报错.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章