WPF“本地”是未声明的前缀

时间:2015-06-21 21:32:38

标签: c# wpf local

我从WPF开始。我正在做教程SofiaCarRental,我有'本地'别名的问题。有人能用这个别名帮我吗? 我找不到所有这些课程。

错误列表

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>

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

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

1 个答案:

答案 0 :(得分:4)

  

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

在文件AddEditWindow.xaml中,未声明local前缀。 XML名称空间声明在逐个文件的基础上工作。它们不是继承的,只对当前文件有效。如果要在该文件中使用其他命名空间中的组件,则还必须在其中添加声明。你可以在代码中看到它们就像using一样 - 只要你想使用一个类型,就必须先告诉编译器在哪里查找它:

<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:前缀而没有先声明它而编译器没有找到你的类型。