Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将列表框绑定到ObservableCollection时发生ArgumentException_C#_Xaml_Windows Phone 8 - Fatal编程技术网

C# 将列表框绑定到ObservableCollection时发生ArgumentException

C# 将列表框绑定到ObservableCollection时发生ArgumentException,c#,xaml,windows-phone-8,C#,Xaml,Windows Phone 8,首先我应该补充一点,我是Windows Phone开发的新手,所以请对我放松:-) 我想将一个列表框绑定到一个observeCollection,其中每个LinkElement都由一个名为Tile的用户控件表示。到目前为止,代码工作正常,我在ObservableCollection中获得了与LinkElement:s一样多的Tile:s(我对下面的代码进行了一些简化,现在只有一个LinkElement) XAML: 而我所绑定的集合(目前没有多少作用): 使用系统; 使用System.Colle

首先我应该补充一点,我是Windows Phone开发的新手,所以请对我放松:-)

我想将一个列表框绑定到一个observeCollection,其中每个LinkElement都由一个名为Tile的用户控件表示。到目前为止,代码工作正常,我在ObservableCollection中获得了与LinkElement:s一样多的Tile:s(我对下面的代码进行了一些简化,现在只有一个LinkElement)

XAML:

而我所绑定的集合(目前没有多少作用):

使用系统;
使用System.Collections.Generic;
使用System.Collections.ObjectModel;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间SoftTeam.SoftLink
{
公共类链接投影
{
私人链接(u Links),;
公共ObservableCollection列表=新ObservableCollection();
公共链接投影(链接)
{
_链接=链接;
}
公共无效刷新()
{
List.Clear();
var元素=新的LinkElement();
element.Name=“Button1”;
element.Header=“媒体”;
单元高度=200;
单元宽度=480;
element.IsLink=false;
element.Tag=null;
element.URL=“”;
列表。添加(元素);
}
}
公共类链接元素
{
公共字符串名称;
公共字符串头;
公共字符串URL;
公众双高;
公共双宽度;
公共图书馆;
公共对象标签;
}
}

问题是,当我尝试将Tile控件的属性绑定到LinkElement类的属性时,我得到一个System.ArgumentException“值不在预期范围内”。异常没有提示问题在哪里,因为它没有出现在我的代码中

也就是说,当我改变的时候

<Controls:Tile> // This works!
//这很管用!
例如,在XAML中

<Controls:Tile TileHeader="{Binding Path=Header}"> // This crashes
//这会崩溃

//这也会崩溃
出现例外情况。我绑定到哪个属性并不重要,它给出了一个例外。如果没有属性绑定,代码可以正常工作


我想我的问题是:为什么

我终于在这篇文章中找到了答案:


问题是我的UserControl位于另一个项目/程序集中,因此我需要为我的控件创建依赖项项目。

我认为应该在实现
INotifyPropertyChanged
接口的模型类中使用这些属性。然后可以将这些属性绑定到XAML控件。检查此链接以了解更多信息,我已将INotifyPropertyChanged添加到LinkElement类,并在您发送的链接中实现了它,但现在我得到了一个NullReferenceException:System.Windows.ni.dll中发生了“System.NullReferenceException”类型的第一次意外异常附加信息:对象引用未设置为对象的实例。我现在正在调查,不确定这是否与我的原始问题有关…不,这是另一个问题,这意味着主要问题仍然存在…好的,另一个更新。INotifyPropertyChanged确实有效,但到目前为止,它只适用于数值属性(在我的例子中是双精度)。它在字符串和布尔属性上仍然失败。。。
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SoftTeam.SoftLink
{
 public class LinkProjection
{
    private Links _links;
    public ObservableCollection<LinkElement> List = new ObservableCollection<LinkElement>();
    public LinkProjection(Links links)
    {
        _links = links;
    }

    public void Refresh()
    {
        List.Clear(); 

        var element = new LinkElement();

        element.Name = "Button1"; 
        element.Header = "Media";
        element.Height = 200;
        element.Width = 480;
        element.IsLink = false;
        element.Tag = null;
        element.URL = "";

        List.Add(element);
    }
}

public class LinkElement
{
    public string Name;
    public string Header;
    public string URL;
    public double Height;
    public double Width;
    public bool IsLink;
    public object Tag;
}
<Controls:Tile> // This works!
<Controls:Tile TileHeader="{Binding Path=Header}"> // This crashes
<Controls:Tile Name="{Binding Path=Name}">  // This crashes too