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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30969555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:13:14  来源:igfitidea点击:

WPF ''local' is an undeclared prefix

c#wpflocal

提问by tauri

I'm starting with WPF. I am doing tutorial SofiaCarRental, and I have a problem with 'local' alias. Can somebody help me with this alias? I have all of these classes wich are not found.

我从 WPF 开始。我正在做 SofiaCarRental 教程,我遇到了“本地”别名的问题。有人可以帮我使用这个别名吗?我有所有这些没有找到的课程。

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

回答by poke

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

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

In the file AddEditWindow.xaml, the localprefix is not declared. XML namespace declarations work on a file-by-file basis. 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 usings in code—whenever you want to use a type, you have to tell the compiler where to look for it first:

在文件中AddEditWindow.xamllocal没有声明前缀。XML 命名空间声明在逐个文件的基础上工作。它们不会被继承,并且只对当前文件有效。如果您想在该文件中使用来自其他命名空间的组件,您也必须在那里添加声明。你可以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'. c:..\SofiaCarRental.WPF\Views\AddEditWindow.xaml 8 6 SofiaCarRental.WPF

错误 5 在类型“Window”中找不到可附加属性“Resources”。c:..\SofiaCarRental.WPF\Views\AddEditWindow.xaml 8 6 SofiaCarRental.WPF

While local:BaseDialogWindowis a subtype of Window, this is still the type for this file. The compiler sees this when it looks at the XAML for this part:

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

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

And this is essentially equivalent to this:

这基本上等同于:

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

Since OtherTypeis not the same as SomeType, this is an attached propertyin XAML. But Windowdoes not have an attached property called Resources.

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

What you want to do instead is set the Resourcesproperty of your window instead. And your window type is SomeType, so you need to write it like this:

你想要做的是设置Resources你的窗口的属性。你的窗口类型是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:前缀而没有先声明它并且编译器没有找到您的类型。