2015-06-21 333 views
3

我以WPF開頭。我正在做教程SofiaCarRental,我有一個'本地'別名的問題。有人可以用這個別名來幫助我嗎? 我沒有找到所有這些類。WPF''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> 

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

第一個錯誤行上的目錄結構看起來是否正確?不是說它應該很重要,但是如果你把telerik線放在本地以下呢?也不是說它應該重要,但是如果你創建了一個「SofiaCarRentallWPF」項目(沒有期限),看看它是否會改變? – SteveFerg

+0

本地下方的Telerik沒有幫助。沒有期限的SofiaCarRentalWPF名字也沒有改變任何東西。結構看起來不錯,我完全按照它在教程中所做的那樣做了...我從頭開始做了兩次教程,並且發現了相同的錯誤... – tauri

+0

1.在'AddEditWindow' xaml文件中,前綴'local'不是聲明。 2.你的'NullableBooleanConverter'可能聲明在與'SofiaCarRental.WPF.Views'不同的命名空間中。你可以發佈它的代碼(包括名稱空間聲明)嗎? – vesan

回答

4

錯誤3 '的' 本地」是一個未聲明前綴。第1行,第2位。 XML無效。 C:.. \ SofiaCarRental.WPF \查看\ AddEditWindow.xaml 1 2 SofiaCarRental.WPF

在文件AddEditWindow.xaml,將local前綴未聲明。 XML名稱空間聲明在逐個文件的基礎上工作。它們不是繼承的,只對當前文件有效。如果您想使用該文件中其他名稱空間的組件,則還必須在其中添加聲明。你可以看到他們像using S IN代碼,只要你想使用的類型,你必須告訴編譯器到哪裏尋找它首先:

<local:BaseDialogWindow x:Class="SofiaCarRental.WPF.Views.AddEditWindow" 
    … 
    xmlns:local="clr-namespace:SofiaCarRental.WPF.Views" 
    … > 

錯誤5的附着性能「資源'在「窗口」類型中找不到。 C:.. \ SofiaCarRental.WPF \視圖\ AddEditWindow.xaml 8 6 SofiaCarRental.WPF

雖然local:BaseDialogWindowWindow一個亞型,這仍然是這個文件的類型。編譯器看到了這一點着眼於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:而沒有首先聲明它,編譯器沒有找到你的類型。

+0

謝謝你戳!我解決了這個問題。本教程不明確,幷包含錯誤。現在我明白了 – tauri