ObservableCollection导致无效的强制转换异常



我对XAML完全陌生,一直在尝试基于微软的示例构建一个小型手机应用程序:ListView绑定。当我的代码运行时;指定的强制转换无效";在下面标记的点处出现异常。我不确定这其中有多重要,但请耐心等待。

这是页面部分的XAML代码:

<ListView x:Name="ListCoords" ItemsSource="{Binding Coordinates}">
<ListView.ItemTemplate>
<DataTemplate>
<StackLayout Margin="0" Orientation="Horizontal">
<Label x:Name="LabelA" HeightRequest="32" WidthRequest="100" Text="{Binding Path=angle, StringFormat='{0:F2}'}" />
<Label x:Name="LabelX" HeightRequest="32" WidthRequest="100" Text="{Binding Path=x, StringFormat='{0:F2}'}" />
<Label x:Name="LabelY" HeightRequest="32" WidthRequest="100" Text="{Binding Path=y, StringFormat='{0:F2}'}" />
</StackLayout>
</DataTemplate>
</ListView.ItemTemplate>

页面后面的cs代码的相关部分如下所示:

public partial class PCDPage : ContentPage
{
public ObservableCollection<Coordinate> CoordinateList = new ObservableCollection<Coordinate> ();
public ObservableCollection<Coordinate> Coordinates { get { return CoordinateList; } }
public PCDPage()
{
InitializeComponent();
ListCoords.ItemsSource = CoordinateList;
}
... Code here responds to a button press and calls the following ...

//
// Calculate and display a table of hole coordinates
//
private void CalculateCoordinates()
{
// Start a new list
CoordinateList.Clear();
double x, y, a, b;
for (int i = 0; i < count; i++)
{
... Calculate a, x, y

// Add the corrdinate to the list
CoordinateList.Add(new Coordinate(a, x, y));      <<<< - Exception
}
... Finish off
}

小坐标类是:

public class Coordinate
{
public Coordinate()
{
}
public Coordinate(double va, double vx, double vy)
{
angle = va;
x = vx;
y = vy;
}
public double angle { get; set; }
public double x { get; set; }
public double y { get; set; }
}

当我运行我的应用程序时,它在将第一个坐标添加到列表上标记的点处崩溃。当它在调试器中停止时,我可以将鼠标悬停在CoordinateList上,查看它是否包含预期的一项。在XAML中,我可以对绑定执行同样的操作。因此,在我看来,问题似乎发生在添加项目之后,返回到我的代码之前。我是否错过了一些显而易见的东西,或者有什么微妙之处我还没有了解?

提前感谢

Coordinate类的属性类型为double,但;文本";Labels的属性是字符串。你需要创建一个转换器

public  class DoubleToStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, 
object parameter, CultureInfo culture)
{
return value!=null?((double)value).ToString():"";
}

public object ConvertBack(object value, Type targetType, 
object parameter, CultureInfo culture)
{
return null;
}
}

<ListView x:Name="ListCoords" ItemsSource="{Binding Coordinates}">
<ListView.Resources>
<l:DoubleToStringConverter x:Key="converter" />
</ListView.Resources>
<ListView.ItemTemplate>
<DataTemplate>
<StackLayout Margin="0" Orientation="Horizontal">
<Label x:Name="LabelA" HeightRequest="32" WidthRequest="100" Text="{Binding Path=angle, StringFormat='{0:F2}',Converter={StaticResource converter}}" />
<Label x:Name="LabelX" HeightRequest="32" WidthRequest="100" Text="{Binding Path=x, StringFormat='{0:F2}',Converter={StaticResource converter}}" />
<Label x:Name="LabelY" HeightRequest="32" WidthRequest="100" Text="{Binding Path=y, StringFormat='{0:F2}',Converter={StaticResource converter}}" />
</StackLayout>
</DataTemplate>
</ListView.ItemTemplate>

"l〃;将是指示DoubleToStringConverter类的命名空间的前缀。

xmlns:l="clr-namespace:MyNamespace"
namespace MyNamespace
{
public class DoubleToStringConverter : IValueConverter
{

但在这种情况下,转换器是不必要的,这不是错误。

我发现有必要将内容包含在ViewCell 的视图中

<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<StackLayout Margin="0" Orientation="Horizontal">
<Label  x:Name="LabelA" HeightRequest="32" WidthRequest="100" Text="{Binding Path=angle, StringFormat='{0:F2}'}" />
<Label  x:Name="LabelX" HeightRequest="32" WidthRequest="100" Text="{Binding Path=x, StringFormat='{0:F2}'}" />
<Label  x:Name="LabelY" HeightRequest="32" WidthRequest="100" Text="{Binding Path=y, StringFormat='{0:F2}'}" />
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>

抱歉我英语不好。

最新更新