在类型上找不到匹配的构造函数

Juh*_* T. 9 c# silverlight xaml exception-handling windows-phone-7

我正在使用Visual Studio Express 2012 for Windows Phone制作Windows Phone 7.1应用程序.

我将此命名空间添加到MainPage.xaml:

xmlns:myNameSpace="clr-namespace:MyApp"
Run Code Online (Sandbox Code Playgroud)

还有这个:

<Grid.Resources>
        <myNameSpace:MyClass x:Key="referenceToMyClass" />
</Grid.Resources>
Run Code Online (Sandbox Code Playgroud)

并在同一文件中使用如下:

<ListBox Name="MyListBox"
         Height="{Binding ElementName=ContentPanel, Path=Height}"
         Width="{Binding ElementName=ContentPanel, Path=Width}"
         ItemsSource="{StaticResource referenceToMyClass}"
         DisplayMemberPath="MyAttribute" />
Run Code Online (Sandbox Code Playgroud)

MyClass看起来像这样:

namespace MyApp
{
    class MyClass : ObservableCollection<AnotherClass>
    {
        public MyClass()
        {
            Class temp = new AnotherClass("Example attribute");
            Add(temp);
        }

        public void AddAnotherClass(AnotherClass anotherClass)
        {
            Add(anotherClass);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

因此,当我尝试在我的手机上调试它时,我收到以下错误:

System.Windows.dll中出现'System.Windows.Markup.XamlParseException'类型的第一次机会异常附加信息:在类型'MyApp.MyClass'上找不到匹配的构造函数.

Dav*_*d L 17

这是因为你的班级不公开.应该

public class MyClass : ObservableCollection<AnotherClass>
Run Code Online (Sandbox Code Playgroud)

XAML无法绑定到非公共对象/类/属性