[英]WPF ''local' is an undeclared prefix
我在定義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;
}
}}
可空布爾轉換器
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;
}
}
}}
錯誤 3 ''local' 是一個未聲明的前綴。 第 1 行,位置 2。 XML 無效。 c:..\\SofiaCarRental.WPF\\Views\\AddEditWindow.xaml 1 2 SofiaCarRental.WPF
在文件AddEditWindow.xaml
,未聲明local
前綴。 XML 命名空間聲明在逐個文件的基礎上工作。 它們不會被繼承,並且只對當前文件有效。 如果您想在該文件中使用來自其他命名空間的組件,您也必須在那里添加聲明。 你可以看到它們就像在代碼中using
s 一樣——每當你想使用一個類型時,你必須先告訴編譯器在哪里尋找它:
<local:BaseDialogWindow x:Class="SofiaCarRental.WPF.Views.AddEditWindow"
…
xmlns:local="clr-namespace:SofiaCarRental.WPF.Views"
… >
錯誤 5 在類型“Window”中找不到可附加屬性“Resources”。 c:..\\SofiaCarRental.WPF\\Views\\AddEditWindow.xaml 8 6 SofiaCarRental.WPF
雖然local:BaseDialogWindow
是Window
的子類型,但這仍然是此文件的類型。 編譯器在查看這部分的 XAML 時會看到這一點:
<SomeType …>
<OtherType.Property>…</OtherType.Property>
</SomeType>
這基本上等同於:
<SomeType … OtherType.Property="…" />
由於OtherType
與SomeType
,因此這是 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:
前綴而沒有先聲明它並且編譯器沒有找到您的類型。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.