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

WPF ''local' is an undeclared prefix

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

我有所有这些 class 没有找到。

错误列表

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(这里我指定'local')

<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>

添加编辑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 class:

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;
        }
    }
}}

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

在文件AddEditWindow.xaml中,没有声明local前缀。 XML 命名空间声明在逐个文件的基础上工作。它们不是继承的,只对当前文件有效。如果你想在该文件中使用来自其他命名空间的组件,你也必须在那里添加声明。你可以在代码中看到它们,就像 usings——无论何时你想使用一个类型,你都必须告诉编译器首先在哪里寻找它:

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

Error 5 The attachable property 'Resources' was not found in type 'Window'. c:..\SofiaCarRental.WPF\Views\AddEditWindow.xaml 8 6 SofiaCarRental.WPF

虽然 local:BaseDialogWindowWindow 的子类型,但仍是此文件的类型。编译器在查看此部分的 XAML 时会看到:

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

这基本上等同于:

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

由于OtherTypeSomeType不同,这是XAML中的附加属性。但是 Window 没有附加的 属性 称为 Resources.

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

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

所以在你的情况下,你想像这样设置你的资源:

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

剩下的错误都是因为你没有先声明就使用了local:前缀,编译器没有找到你的类型。