【问题标题】:Xamarin.Forms Binding Specified cast is not validXamarin.Forms 绑定指定的强制转换无效
【发布时间】:2016-11-13 16:05:30
【问题描述】:

我有一个奇怪的例外,编译器告诉我指定的强制转换无效,即使我正在做的事情非常简单。

我有一个绑定到 ObservableCollection 的 ListView。在我的 Listview 里面是一个带有 Grid 的 ViewCell。 Xamarin.Forms 版本 2.3.2.127

<ListView ItemsSource="{Binding GiftCollection}">
      <ListView.ItemTemplate>
        <DataTemplate>
          <ViewCell>
            <ViewCell.View>
              <Grid>
          <Grid.RowDefinitions>
            <RowDefinition Height="20"/>
            <RowDefinition Height="20"/>
          </Grid.RowDefinitions>
          <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="40"/>
          </Grid.ColumnDefinitions>

          <Label Grid.Row="0" Grid.Column="0" Text="{Binding GiftName}"/>
          <Label Grid.Row="1" Grid.Column="0" Text="{Binding GiftDescription}"/>
          <Image Grid.Row="0" Grid.RowSpan="2" Grid.Column="1" Source="{Binding GiftImage}"/>
        </Grid>
        </ViewCell.View>
      </ViewCell>
        </DataTemplate>
      </ListView.ItemTemplate>
    </ListView>

型号:

 public class GiftModel {

        public string GiftName { get; set; }
        public string GiftDescription { get; set; }
        public ImageSource GiftImage { get; set; }
    }

视图模型:

public class NextRoundViewModel : BaseViewModel {

        public NextRoundViewModel(ApplicationModel applicationModel) {
            ApplicationModel = applicationModel;
            Initialize();
        }

        public ApplicationModel ApplicationModel { get; set; }
        public ObservableCollection<GiftModel> GiftCollection { get; set; }
        public string CurrentRound => "Runde 2";

        private void Initialize() {
            GiftCollection = new ObservableCollection<GiftModel> {
                new GiftModel {
                    GiftName = "100 Punkte",
                    GiftDescription = "Test",
                    GiftImage = ImageSource.FromFile("Star.png"),
                },
                new GiftModel {
                    GiftName = "200 Punkte",
                    GiftDescription = "Test",
                    GiftImage = ImageSource.FromFile("Star.png"),
                },
                new GiftModel {
                    GiftName = "300 Punkte",
                    GiftDescription = "Test",
                    GiftImage = ImageSource.FromFile("Star.png"),
                },
            };
        }
    }

所以我尝试了所有方法,但如果我使用例如 TextCell 异常就消失了。 System.InvalidCastException:指定的强制转换无效。这很奇怪,因为我不知道在哪里寻找错误。

【问题讨论】:

  • 哪个特定绑定导致了问题?被绑定的属性是什么类型的?
  • 只是图像在工作。标签绑定到一个简单的字符串。如果我用 TextCell 替换标签,异常就消失了。我需要使用标签:)
  • 您正在绑定 CategoryName 和 Description,这些在您的模型中吗?
  • 抱歉搞砸了。更新了我的答案
  • 我能够将上面的代码复制并粘贴到一个新项目中,在删除对 BaseViewModel 的引用并设置 BindingContext 之后,它工作得很好。我认为问题出在未发布的代码中——与周围页面、BaseViewModel 或 BaseViewModel 如何绑定到视图的一些交互。尝试简化显示此 ListView 的页面以及整个页面的 ViewModel,看看是否有帮助。

标签: c# xamarin xamarin.forms


【解决方案1】:

我也遇到过这个问题,问题出在xaml 上。我的&lt;DataTemplate&gt; 中有一个&lt;StackLayout&gt;,您可以删除您的&lt;Grid&gt;,这应该可以解决问题。

您是否知道可以将&lt;Grid&gt; 替换为&lt;ImageCell&gt;

<ListView.ItemTemplate>
    <DataTemplate>
        <ImageCell
            Text="{Binding GiftName}"
            Detail="{Binding GiftDescription}"
            ImageSource="{Binding GiftImage}">
        </ImageCell>
    </DataTemplate>
</ListView.ItemTemplate>

【讨论】:

  • 如果您仍然想使用 Grid,请将其包装在 ViewCell 标签中
【解决方案2】:

你可以放在里面。

类似的东西

<ListView.ItemTemplate>
<DataTemplate>
     <ViewCell>
          <StackLayout>
          </StackLayout>
     </ViewCell>
</DataTemplate>

【讨论】:

    【解决方案3】:

    从 DataTemplate 中删除 &lt;ViewCell&gt;。这应该可以解决错误。 &lt;Grid&gt; 将在 &lt;CollectionView&gt; 中工作

     <ListView.ItemTemplate>
         <DataTemplate>
             <Grid>
             :    
             :    
             </Grid>
         </DataTemplate>
     </ListView.ItemTemplate>
    

    【讨论】:

    • 拯救了我的一天 :)
    【解决方案4】:

    以下代码给出了相同的异常,可能对某人有所帮助: 使用 ListView 或 CollectionView 但在 ItemTemplate 我使用 BindableLayout.ItemTemplate 而不是 CollectionView.ItemTemplate

    <CollectionView Grid.Row="0" ItemsSource="{Binding SummaryInfos}">
            <CollectionView.ItemsLayout>
                <LinearItemsLayout Orientation="Horizontal" />
            </CollectionView.ItemsLayout>
    
            <!-- this code gives the same exception -->
            <!-- must use CollectionView instead BindableLayout -->
            <BindableLayout.ItemTemplate>
                <DataTemplate>
                    <StackLayout>
                        <Label Text="{Binding SomeText}"/>
                    </StackLayout>
                </DataTemplate>
            </BindableLayout.ItemTemplate>
    </CollectionView>
    

    【讨论】:

    • 你的意思是使用collectionview.itemTemplate不会得到同样的异常吗?
    猜你喜欢
    • 1970-01-01
    • 2018-05-27
    • 2014-07-30
    • 2012-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多