C# WPF设计器代码一直告诉我有错误
这是我的表单XAML代码C# WPF设计器代码一直告诉我有错误,c#,wpf,visual-studio-2010,xaml,C#,Wpf,Visual Studio 2010,Xaml,这是我的表单XAML代码 <Window x:Class="Bail.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns="clr-namespace:Bail" Title="MainWindow" H
<Window x:Class="Bail.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns="clr-namespace:Bail"
Title="MainWindow" Height="768" Width="1024" WindowStartupLocation="CenterScreen"
Closing="Window_Closing" ResizeMode="NoResize">
<Grid>
<Grid.Resources>
<src:ListboxMenuItems x:Key="ListboxMenuItems"/>
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="185" />
<!-- Or Auto -->
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<ListBox Width="150" Margin="0,5,0,10" Grid.Column="0"
ItemsSource="{StaticResource ListboxMenuItems}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Padding="5,0,5,0" Text="{Binding FirstName}" />
<TextBlock Text="{Binding LastName}" />
<TextBlock Text=", " />
<TextBlock Text="{Binding Address}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Canvas Grid.Column="1" />
</Grid>
</Window>
以下是错误
在更正xmlns:src之后,我得到以下结果 警告: 警告1“src”是未声明的前缀。第8行,位置14。“XML无效。C:\Users\Shayaan Siddiqui\Documents\Bail\Bail\Bail\MainWindow.xaml 8 14 Bail ListBoxMenuItems是我在C#中创建的一个类 在这里;这是该类的代码
//FileName: ListboxMenuItems.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Bail
{
public class ListboxMenuItem
{
public String FirstName { get; set; }
public String LastName { get; set; }
public String Address { get; set; }
public ListboxMenuItem(String firstName, String lastName, String address)
{
this.FirstName = firstName;
this.LastName = lastName;
this.Address = address;
}
}
class ListboxMenuItems
{
List<ListboxMenuItem> Items { get; private set; }
public ListboxMenuItems()
{
Items = new List<ListboxMenuItem>();
Items.Add(new ListboxMenuItem("Michael", "Anderberg", "12 North Third Street, Apartment 45"));
Items.Add(new ListboxMenuItem("Chris", "Ashton", "34 West Fifth Street, Apartment 67"));
Items.Add(new ListboxMenuItem("Cassie", "Hicks", "56 East Seventh Street, Apartment 89"));
Items.Add(new ListboxMenuItem("Guido", "Pica", "78 South Ninth Street, Apartment 10"));
}
}
}
//文件名:ListboxMenuItems.cs
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
名称空间保护
{
公共类ListboxMenuItem
{
公共字符串名{get;set;}
公共字符串LastName{get;set;}
公共字符串地址{get;set;}
公共ListboxMenuItem(字符串名、字符串名、字符串地址)
{
this.FirstName=FirstName;
this.LastName=LastName;
这个地址=地址;
}
}
类ListboxMenuItems
{
列表项{get;private set;}
公共ListboxMenuItems()
{
项目=新列表();
添加(新列表框MenuItem(“Michael”、“Anderberg”、“北三街12号,45号公寓”);
添加项目(新列表框MenuItem(“Chris”、“Ashton”、“34 West Fifth Street,67号公寓”);
添加(新列表框MenuItem(“卡西”、“希克斯”、“东七街56号,89号公寓”);
添加(新列表框MenuItem(“Guido”、“Pica”、“南九街78号,10号公寓”);
}
}
}
xmlns:src=“clr命名空间:ListBoxSnippetEx”
代码中缺少这一行。用项目名称空间替换ListBoxSnippetEx。xmlns:src=“clr名称空间:ListBoxSnippetEx”
代码中缺少这一行。用项目名称空间替换ListBoxSnippetEx。您在
窗口中缺少名称空间声明
标记:
<window x:Class="Bail.MainWindow"
xmlns:src="clr-namespace:Bail"
.../>
此外,如果您更改了对象的名称以删除末尾的“s”,那么您还需要在xaml中更改该名称
<Grid.Resources>
<src:ListboxMenuItem x:Key="ListboxMenuItems"/>
</Grid.Resources>
您在
窗口中缺少一个名称空间声明
标记:
<window x:Class="Bail.MainWindow"
xmlns:src="clr-namespace:Bail"
.../>
此外,如果您更改了对象的名称以删除末尾的“s”,那么您还需要在xaml中更改该名称
<Grid.Resources>
<src:ListboxMenuItem x:Key="ListboxMenuItems"/>
</Grid.Resources>
您还有一个额外的
<Grid.Resources>
<
<src:ListboxMenuItems x:Key="ListboxMenuItems"/>
<
您还有一个额外的 <Grid.Resources>
<
<src:ListboxMenuItems x:Key="ListboxMenuItems"/>
<
您收到的错误是什么?如果您同时提供错误=Dposted errors和更新的代码,这将非常有用。您收到的错误是什么?如果您同时提供错误=Dposted errors和更新的代码,这将非常有用。问题是intellisense不会建议src。我将项目名称添加到xmlns中,但表单设计器也有同样的问题。如果你需要看更多的代码,让我看看know@CocoaDev,您忘记将“src”添加到xmlns:src=“clr namespace:Bail”
声明中。我已经更新了我的答案,这样你就可以把这行代码复制/粘贴到你的代码中。奇怪的是,这个错误消失了。没有xmlns
声明中的src
,它将不知道
标记中的
是什么。在将src
添加到xmlns
声明之后,您还应该在xaml中看到intellisense。问题是intellisense不会建议src。我将项目名称添加到xmlns中,但表单设计器也有同样的问题。如果你需要看更多的代码,让我看看know@CocoaDev,您忘记将“src”添加到xmlns:src=“clr namespace:Bail”
声明中。我已经更新了我的答案,这样你就可以把这行代码复制/粘贴到你的代码中。奇怪的是,这个错误消失了。没有xmlns
声明中的src
,它将不知道
标记中的
是什么。在将src
添加到xmlns
声明之后,您还应该在xaml中看到intellisense。这是一个很好的观察结果,但仅出现在本文中。这一定是复制/粘贴错误。这个额外的<没有出现在我的代码中。很好的观察,但它只出现在这篇文章中。这一定是复制/粘贴错误。我的代码中没有额外的<。