具有从ObservableCollection派生的类的xaml中存在未知类型错误
本文关键字:存在 未知 错误 类型 xaml ObservableCollection 派生 | 更新日期: 2023-09-27 18:23:54
基于MSDN上的这个例子,我创建了一个"从ObservableCollection派生的非通用自定义集合类,并将其约束为特定类型。"它用作listview控件的ItemsSource属性。这一切都很完美,xaml设计视图显示了我加载到自定义集合类中的示例数据。
当我试图构建项目时,问题就出现了;我收到此错误:"错误1 XML命名空间'clr namespace:Messaging_2_0中的未知类型'ThreadCollection';assembly=Messaging 2.0.WindowsPhone,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null'C:''Users''Wesley''Source''Repos''Messenging2.0''Messaging 2.0''Messing 2.0''WindowsPhone''MainPage.xaml 14 10 Messaging 2.0.Windows Phone.
此错误源自下面xaml代码的<c:ThreadCollection x:Key="MainThreadCollection"/>
行。
<Page
x:Class="Messaging_2._0.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Messaging_2._0"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:c="clr-namespace:Messaging_2._0"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Page.Resources>
<c:ThreadCollection x:Key="MainThreadCollection"/>
</Page.Resources>
<The code for the listview control is here, I haven't included it because it works as I expect.>
以下是xaml:引用的C#代码
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using System.Collections.ObjectModel;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
namespace Messaging_2._0
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public ThreadCollection MainThreadCollection = new ThreadCollection();
public MainPage()
{
this.InitializeComponent();
this.NavigationCacheMode = NavigationCacheMode.Required;
this.DataContext = this;
}
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached.
/// This parameter is typically used to configure the page.</param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
// TODO: Prepare page for display here.
// TODO: If your application contains multiple pages, ensure that you are
// handling the hardware Back button by registering for the
// Windows.Phone.UI.Input.HardwareButtons.BackPressed event.
// If you are using the NavigationHelper provided by some templates,
// this event is handled for you.
}
private void AddThread(object sender, RoutedEventArgs e)
{
MainThreadCollection.Add(new ThreadViewItem() { Name = "Tom Riddle", LatestMessage = "It worked!" });
}
}
public class ThreadCollection : ObservableCollection<ThreadViewItem>
{
public ThreadCollection()
: base()
{
Add(new ThreadViewItem() { Name = "Harry Potter", LatestMessage = "What's up?" });
}
}
public class ThreadViewItem
{
public String Name
{
get;
set;
}
public String LatestMessage
{
get;
set;
}
}
}
因为设计视图正确地预览了哈利波特的信息,我认为问题相对较小,我只是不知道它到底是什么。
我会将ThreadCollection放在它自己的文件(最好是ThreadCollection.cs)中,并将其放在自己的命名空间中,比如Messaging_2_0.Collections或类似的东西。然后将xmlns:c指向该名称空间。
这将确保您的类不受编码XAML时涉及的自动代码生成的影响。
如果它不能解决您的问题,那么错误文本至少会更有帮助。