无法隐式转换类型' AppName.MyListItem'到' AppName.MySecundPage.MyListItem'?

时间:2016-10-11 12:17:48

标签: listview xamarin xamarin.forms tabbedpage

我有TabbedPage以及我打算做的是将json ListView流式传输到MainPage下的ContentPage

据我所知,在转换为xaml时,丢失了。

任何人都可以帮我解决这个问题吗?

我的代码如下所示:

我的C#文件

void OnListViewItemSelected(object sender, SelectedItemChangedEventArgs eventInfo)
{
    if (eventInfo.SelectedItem != null)
    {
        cityItem clickedCityName = (cityItem)eventInfo.SelectedItem;

        System.Diagnostics.Debug.WriteLine(clickedCityName.cityAddress);
        citiesListView.SelectedItem = null;

        SingelCityPage pcity = new SingelCityPage ();
        pcity.currentCity = clickedCityName;

        //Looks like I am missing something here but I don't find any reference.
        //or is in the XML that its missing the connection? 
        Navigation.PushAsync(pcity);
        }
    }

我的Xaml

<ContentPage.Content>
    <StackLayout>
        <ListView x:Name="citiesListView">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                          <ViewCell.ContextActions>
                                <AbsoluteLayout Padding="10,10,10,10">
                                    <Label Text="{Binding cityName}" AbsoluteLayout.LayoutBounds="0.0, 0.0, 0.75, 0.5" AbsoluteLayout.LayoutFlags="All" />
                                    <Label Text="{Binding cityLocation}" AbsoluteLayout.LayoutBounds="0.0, 1.0, 0.75, 0.5" AbsoluteLayout.LayoutFlags="All" />
                                    <Label Text="{Binding cityAdress}" AbsoluteLayout.LayoutBounds="1.0, 0.0, 0.5, 1.0" AbsoluteLayout.LayoutFlags="All" />
                                </AbsoluteLayout>
                          </ViewCell.ContextActions>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView> 
    </StackLayout>
</ContentPage.Content>

1 个答案:

答案 0 :(得分:0)

在您的代码库中,您(至少)在不同的命名空间中有两个名为MyListItem的类。一个可能是由json反序列化自动生成的,我只能在这里猜测。

即使它们具有相同的名称,它们也是不同的类,并且一个实例不能转换为另一个的实例。

确保在任何地方都使用相同的类型。

再次猜测,因为粘贴的片段没有帮助,我说这个问题发生在这一行:

pcity.currentCity = clickedCityName;