【问题标题】:Unknown type error in xaml with class derived from ObservableCollectionxaml 中的未知类型错误,类派生自 ObservableCollection
【发布时间】:2014-09-22 19:52:56
【问题描述】:

基于MSDN 的这个示例,我创建了一个“从 ObservableCollection 派生的非泛型自定义集合类,并将其限制为特定类型”。这用作列表视图控件的 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\Messaging 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;
        }
    }
}

因为设计视图正确地预览了哈利波特的消息,我认为问题相对较小,我只是无法弄清楚它到底是什么。

【问题讨论】:

    标签: c# .net wpf xaml


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-13
      • 2017-12-27
      • 1970-01-01
      相关资源
      最近更新 更多