【问题标题】:WPF ''local' is an undeclared prefixWPF ''local' 是一个未声明的前缀
【发布时间】:2015-06-21 21:32:37
【问题描述】:

我对定义的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;
        }
    }
}}

【问题讨论】:

  • 第一个错误行上的目录结构是否正确?没关系,但如果你把telerik线放在local下面怎么办?也没有关系,但是如果您创建了一个“SofiaCarRentallWPF”项目(没有句点)来查看它是否会发生变化?
  • Telerik 低于本地没有帮助。没有句号的 SofiaCarRentalWPF 名称也没有任何改变。结构看起来不错,我完全按照教程中的方式完成了......我从头开始做了两次教程,并且出现了同样的错误......
  • 1.在您的 AddEditWindow xaml 文件中,未声明前缀 local。 2. 您的NullableBooleanConverter 可能在与SofiaCarRental.WPF.Views 不同的命名空间中声明。你能贴出它的代码(包括命名空间声明)吗?
  • "将开始标签的名称从 Window 更改为 local:BaseDialogWindow。这样窗口实际上将是之前定义的类的实例 - BaseDialogWindow" 我认为我可以使用 'local'在 MainWindow 中声明。
  • @BeataK:不,你不能。每个 xaml 文件都是一个单独的 xml 文档,因此,每个文件都必须定义自己的命名空间。

标签: c# wpf local


【解决方案1】:

错误 3 ''local' 是一个未声明的前缀。第 1 行,位置 2。 XML 无效。 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"
    … >

错误 5 在类型“窗口”中找不到可附加属性“资源”。 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 的附加属性。

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

【讨论】:

  • 谢谢戳!我解决了这个问题。本教程不清楚并且包含错​​误。现在我明白了
猜你喜欢
  • 2017-06-12
  • 2014-01-05
  • 1970-01-01
  • 2011-01-09
  • 2013-07-11
  • 2020-08-13
  • 2013-08-29
  • 1970-01-01
相关资源
最近更新 更多