WPF'';local';是未声明的前缀

本文关键字:前缀 未声明 local WPF | 更新日期: 2023-09-27 17:59:03

我对已定义的local别名有问题;为什么它无效?

我有所有这些没有找到的类。

错误列表

Error   3   ''local' is an undeclared prefix. Line 1, position 2.' XML is not valid.    c:'..'SofiaCarRental.WPF'Views'AddEditWindow.xaml   1   2   SofiaCarRental.WPF
Error   5   The attachable property 'Resources' was not found in type 'Window'. c:'..'SofiaCarRental.WPF'Views'AddEditWindow.xaml   8   6   SofiaCarRental.WPF
Error   2   The name "NullableBooleanConverter" does not exist in the namespace "clr-namespace:SofiaCarRental.WPF.Views".   c:'..'SofiaCarRental.WPF'Views'MainWindow.xaml  10  9   SofiaCarRental.WPF
Error   1   The namespace prefix "local" is not defined.    c:'..'SofiaCarRental.WPF'Views'AddEditWindow.xaml   1   1   SofiaCarRental.WPF
Error   4   The type 'local:BaseDialogWindow' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built.  c:'..'SofiaCarRental.WPF'Views'AddEditWindow.xaml   1   2   SofiaCarRental.WPF
Error   8   The type 'local:EmptyStringConverter' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built.  c:'..'SofiaCarRental.WPF'Views'AddEditWindow.xaml   11  10  SofiaCarRental.WPF
Error   6   The type 'local:NullableBooleanConverter' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built.  c:'..'SofiaCarRental.WPF'Views'AddEditWindow.xaml   9   10  SofiaCarRental.WPF
Error   7   The type 'local:YearConverter' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built. c:'..'SofiaCarRental.WPF'Views'AddEditWindow.xaml   10  10  SofiaCarRental.WPF

主窗口(此处我指定为"本地")

<Window x:Class="SofiaCarRental.WPF.Views.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
    xmlns:local ="clr-namespace:SofiaCarRental.WPF.Views" 
    Title="Sofia Car Rental" 
    Height="720" Width="1280"
    MinHeight="720" MinWidth="1280">
<Window.Resources>
    <local:NullableBooleanConverter x:Key="booleanConverter" />
    <Style x:Key="checkBoxColStyle" TargetType="telerik:GridViewCell">
        <Setter Property="HorizontalContentAlignment" Value="Center" />
    </Style>
</Window.Resources>
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="Auto">
   ...
</Grid></Window>

添加编辑窗口

<local:BaseDialogWindow x:Class="SofiaCarRental.WPF.Views.AddEditWindow"
    xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="AddEditWindow" 
    Height="417" Width="383"
    Title="{Binding Path=Title}">
<Window.Resources>
    <local:NullableBooleanConverter x:Key="booleanConverter" />
    <local:YearConverter x:Key="yearConverter" />
    <local:EmptyStringConverter x:Key="emptyStringConverter" />
</Window.Resources>
<Grid Margin="20,10,50,10">
   ...
</Grid></local:BaseDialogWindow>

BaseDialogWindow类:

namespace SofiaCarRental.WPF.Views
{
public class BaseDialogWindow : Window
{
    public BaseDialogWindow()
    {
        this.Owner = App.Current.MainWindow;
        this.ShowInTaskbar = false;
        this.ResizeMode = System.Windows.ResizeMode.NoResize;
        this.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner;
    }
}}

NullableBooleanConverter

namespace SofiaCarRental.WPF.Views{
public class NullableBooleanConverter : IValueConverter
{
    public object Convert(object value, Type targetType,
        object parameter, CultureInfo culture)
    {
        object result = this.NullableBooleanToFalse(value);
        return result;
    }
    public object ConvertBack(object value, Type targetType,
        object parameter, CultureInfo culture)
    {
        object result = this.NullableBooleanToFalse(value);
        return result;
    }
    private object NullableBooleanToFalse(object value)
    {
        if (value == null)
        {
            return false;
        }
        else
        {
            return value;
        }
    }
}}

WPF'';local';是未声明的前缀

错误3"local"是未声明的前缀。第1行,位置2。"XML无效。"。c: 。。''SofiaCarRental.WPF

在文件AddEditWindow.xaml中,不声明local前缀。XML名称空间声明在逐个文件的基础上工作。它们不会被继承,并且只在当前文件中处于活动状态。如果您想在该文件中使用来自其他命名空间的组件,您也必须在其中添加声明。你可以在代码中看到它们像using一样——每当你想使用一个类型时,你必须告诉编译器首先在哪里查找它:

<local:BaseDialogWindow x:Class="SofiaCarRental.WPF.Views.AddEditWindow"
    …
    xmlns:local="clr-namespace:SofiaCarRental.WPF.Views"
    … >

错误5在类型"Window"中找不到可附加的属性"Resources"。c: 。。''SofiaCarRental.WPF

虽然local:BaseDialogWindowWindow的一个子类型,但它仍然是该文件的类型。编译器在查看以下部分的XAML时看到了这一点:

<SomeType …>
    <OtherType.Property>…</OtherType.Property>
</SomeType>

这基本上相当于:

<SomeType … OtherType.Property="…" />

由于OtherTypeSomeType不同,因此这是XAML中的附加属性。但Window没有一个名为Resources的附加属性。

您要做的是设置窗口的Resources属性。你的窗口类型是SomeType,所以你需要这样写:

<SomeType …>
    <SomeType.Property>…</SomeType.Property>
</SomeType>

因此,在您的情况下,您需要这样设置您的资源:

<local:BaseDialogWindow x:Class="SofiaCarRental.WPF.Views.AddEditWindow"
        … >
    <local:BaseDialogWindow.Resources>
        …
    </local:BaseDialogWindow.Resources>
    …
</local:BaseDialogWindow>

剩下的错误都是因为您使用了local:前缀而没有首先声明它,并且编译器找不到您的类型。