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"的話, 是會報錯.

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