简体   繁体   English

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

[英]WPF ''local' is an undeclared prefix

I have a problem with a defined local alias;我在定义local别名时遇到问题; why is it not valid?为什么它无效?

I have all of these classes which are not found.我有所有这些找不到的类。

Error list错误列表

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

Main Window (here I specified '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>

AddEditWindow添加编辑窗口

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

Error 3 ''local' is an undeclared prefix.错误 3 ''local' 是一个未声明的前缀。 Line 1, position 2.'第 1 行,位置 2。 XML is not valid. XML 无效。 c:..\\SofiaCarRental.WPF\\Views\\AddEditWindow.xaml 1 2 SofiaCarRental.WPF c:..\\SofiaCarRental.WPF\\Views\\AddEditWindow.xaml 1 2 SofiaCarRental.WPF

In the file AddEditWindow.xaml , the local prefix is not declared.在文件AddEditWindow.xaml ,未声明local前缀。 XML namespace declarations work on a file-by-file basis. XML 命名空间声明在逐个文件的基础上工作。 They are not inherited, and only ever active for the current file.它们不会被继承,并且只对当前文件有效。 If you want to use components from other namespaces in that file, you will have to add the declaration there too.如果您想在该文件中使用来自其他命名空间的组件,您也必须在那里添加声明。 You can see them like using s in code—whenever you want to use a type, you have to tell the compiler where to look for it first:你可以看到它们就像在代码中using s 一样——每当你想使用一个类型时,你必须先告诉编译器在哪里寻找它:

<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'.错误 5 在类型“Window”中找不到可附加属性“Resources”。 c:..\\SofiaCarRental.WPF\\Views\\AddEditWindow.xaml 8 6 SofiaCarRental.WPF c:..\\SofiaCarRental.WPF\\Views\\AddEditWindow.xaml 8 6 SofiaCarRental.WPF

While local:BaseDialogWindow is a subtype of Window , this is still the type for this file.虽然local:BaseDialogWindowWindow的子类型,但这仍然是此文件的类型。 The compiler sees this when it looks at the XAML for this part:编译器在查看这部分的 XAML 时会看到这一点:

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

And this is essentially equivalent to this:这基本上等同于:

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

Since OtherType is not the same as SomeType , this is an attached property in XAML.由于OtherTypeSomeType ,因此这是 XAML 中的附加属性 But Window does not have an attached property called Resources .但是Window没有名为Resources的附加属性。

What you want to do instead is set the Resources property of your window instead.您想要做的是设置窗口的Resources属性。 And your window type is SomeType , so you need to write it like this:你的窗口类型是SomeType ,所以你需要这样写:

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

So in your case, you want to set your resources like this:因此,在您的情况下,您希望像这样设置资源:

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

The remaining errors are all because you are using the local: prefix without declaring it first and the compiler not finding your types.其余的错误都是因为您使用了local:前缀而没有先声明它并且编译器没有找到您的类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM