基于MSDN的这个例子,我创建了一个“非通用自定义集合类,它派生自ObservableCollection,并将其约束为特定类型。”这用作listview控件的ItemsSource属性。这一切都很完美,xaml设计视图显示我加载到自定义集合类中的示例数据。
当我尝试构建项目时会出现问题;我收到此错误:“错误1 XML命名空间中的未知类型'ThreadCollection'clr-namespace:Messaging_2._0; assembly = Messaging 2.0.WindowsPhone,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null'C:\ Users \ Wesley \ Source \ Repos \ Messenging 2.0 \ Messaging 2.0 \ Messaging 2.0 \ Messaging 2.0.WindowsPhone \ MainPage.xaml 14 10 Messaging 2.0.WindowsPhone。
此错误源自以下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;
}
}
}
因为设计视图正确地预览了来自哈利波特的信息,我认为问题相对较小,我只是无法弄清楚究竟是什么。
答案 0 :(得分:0)
我将ThreadCollection放在自己的文件中(最好是ThreadCollection.cs)并将其放在自己的命名空间中,如 Messaging_2._0.Collections 或类似的东西。然后将xmlns:c指向该命名空间。
这将确保您的课程不受编码XAML时所涉及的自动代码生成的影响。
如果它没有解决您的问题,错误文本可能至少会更有帮助。