2014-09-22 113 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時涉及的自動代碼生成的影響。

如果它不能解決您的問題,錯誤文本可能至少會更有幫助。