簡體   English   中英

ObservableCollection 導致無效的強制轉換異常

[英]ObservableCollection causing invalid cast exception

我對 XAML 完全陌生,並且一直在嘗試基於此處的 Microsoft 示例構建一個小型手機應用程序: ListView binding 當我的代碼運行時,我在下面標記的點處收到“指定的強制轉換無效”異常。 我不確定其中有多少是相關的,但請耐心等待。

這是部分頁面的 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
    }

而小坐標 class 是:

    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 上使用 hover 並查看它是否包含預期的一項。 在 XAML 中,我可以對綁定做同樣的事情。 所以在我看來,好像問題是在添加項目之后和返回我的代碼之前發生的。 我是否遺漏了一些明顯的東西,還是有一些我尚未了解的微妙之處?

提前致謝

坐標 class 的屬性是雙精度類型,但標簽的“文本”屬性是字符串。 您需要創建一個轉換器

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 class 的命名空間的前綴。

xmlns:l="clr-namespace:MyNamespace"

namespace MyNamespace
{
    public class DoubleToStringConverter : IValueConverter
    {

但在這種情況下,轉換器不是必需的,這不是錯誤。

我發現有必要在 ViewCell 的 View 中包含內容

<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>

對不起,我的英語不好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM