2014-09-22 74 views
0

基于此示例,我在MSDN处创建了一个“非泛型自定义集合类,它从ObservableCollection派生,并将其限制为特定类型。这用作ListView控件的ItemsSource属性。这一切都很完美,xaml设计视图显示我加载到自定义集合类中的示例数据。xaml中未知类型错误,派生自ObservableCollection的类

当我尝试构建项目时会出现问题;我得到这个错误:“错误1在XML命名空间'clr-namespace:Messaging_2._0;程序集=消息传递2.0.WindowsPhone,版本= 1.0.0.0,文化=中立,PublicKeyToken = null'未知类型'ThreadCollection'C:\ Users \韦斯利\源\回购\信使2.0 \消息2.0 \消息2.0 \消息2.0.WindowsPhone \ 2.0.WindowsPhone。MainPage.xaml中14 10短信

此错误从下面的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.> 

下面是C#代码由XAML后面引用:

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

我会将ThreadCollection放置在它自己的文件(最好是ThreadCollection.cs)中,并将其放置在它自己的名称空间中,如Messaging_2._0.Collections或类似的东西。然后将xmlns:c指向该名称空间。

这将确保您的类不受编码XAML时涉及的自动代码生成的影响。

如果它不能解决您的问题,错误文本可能至少会更有帮助。