xamarin - 错误 : Specified cast is not valid.

标签 xamarin xamarin.forms

我有一个 xaml,它有一个带有 ListView 和按钮的网格,并且在 编译出现这个错误的时刻: 错误:指定的转换无效。

我认为问题可能出在网格内部的 ListView 中,但我还没有真正找到解决方案

这是我的 xaml:

<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="FCR.App.Views.AdvanceSearchResultPage" Title="AdvanceSearchResultPage" xmlns:extended="clr-namespace:FCR.App.ExtendedClasses;assembly=FCR.App">
    <ContentPage.Padding>
        <OnPlatform x:TypeArguments="Thickness" iOS="0, 10, 0, 0" WinPhone="20,20,20,20" />
    </ContentPage.Padding>
    <ContentPage.Content>
        <StackLayout VerticalOptions="StartAndExpand">
            <ContentView IsVisible="false" x:Name="TitleHC">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="0.5*" />
                        <ColumnDefinition Width="9.5*" />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <Image Source="blue_circle.png" Grid.Column="0" />
                    <StackLayout Padding="0,4,0,5" VerticalOptions="Fill" Grid.Column="1">
                        <Label VerticalOptions="CenterAndExpand" TextColor="#0368b1" Text="Histórico de conéxiones" FontSize="20">
                            <Label.FontFamily>
                                <OnPlatform x:TypeArguments="x:String">
                                    <OnPlatform.iOS>FrutigerLTStd-Cn</OnPlatform.iOS>
                                    <OnPlatform.Android>FrutigerLTStd-Cn.otf#FrutigerLTStd-Cn</OnPlatform.Android>
                                </OnPlatform>
                            </Label.FontFamily>
                        </Label>
                    </StackLayout>
                </Grid>
            </ContentView>
            <ContentView IsVisible="false" x:Name="TitleHM">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="0.5*" />
                        <ColumnDefinition Width="9.5*" />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <Image Source="blue_circle.png" Grid.Column="0" />
                    <StackLayout Padding="0,4,0,5" VerticalOptions="Fill" Grid.Column="1">
                        <Label VerticalOptions="CenterAndExpand" TextColor="#0368b1" Text="Histórico de módificaciones" FontSize="20">
                            <Label.FontFamily>
                                <OnPlatform x:TypeArguments="x:String">
                                    <OnPlatform.iOS>FrutigerLTStd-Cn</OnPlatform.iOS>
                                    <OnPlatform.Android>FrutigerLTStd-Cn.otf#FrutigerLTStd-Cn</OnPlatform.Android>
                                </OnPlatform>
                            </Label.FontFamily>
                        </Label>
                    </StackLayout>
                </Grid>
            </ContentView>
            <Grid VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>
                <ContentView x:Name="contentListView" IsVisible="true" Padding="0,10,0,0" BackgroundColor="White" Grid.Row="0">
                    <ListView x:Name="resultListView" BackgroundColor="White" HasUnevenRows="true" HorizontalOptions="FillAndExpand" SeparatorVisibility="None">
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <ViewCell>
                                    <ViewCell.View>
                                        <StackLayout BackgroundColor="White" Padding="0" Opacity="90">
                                            <ContentView Padding="1,0,1,1" BackgroundColor="Gray">
                                                <Grid
                                                    BackgroundColor="White">
                                                    <Grid.ColumnDefinitions>
                                                        <ColumnDefinition Width="0.7*">
                                                        </ColumnDefinition>
                                                        <ColumnDefinition Width="0.15*">
                                                        </ColumnDefinition>
                                                        <ColumnDefinition Width="0.15*">
                                                        </ColumnDefinition>
                                                    </Grid.ColumnDefinitions>
                                                    <StackLayout Grid.Column="0">
                                                        <Label Margin="-5" Text="{Binding NombreCompleto}" TextColor="Gray" Font="Bold,13" />
                                                        <Label Margin="-5"
                                                            Text="{Binding OrganizacionMayusculas}" TextColor="Gray" FontSize="13" />
                                                        <Label Margin="-5" Text="{Binding Region}" TextColor="Gray" FontSize="13" />
                                                    </StackLayout>
                                                    <Label Margin="0,0,0,2" Text="Detalle" Grid.Column="1"
                                                        Font="Bold, 13" ClassId="{Binding IdContacto}" TextColor="Gray" VerticalOptions="End">
                                                        <Label.FontFamily>
                                                            <OnPlatform x:TypeArguments="x:String" iOS="8">
                                                                <OnPlatform.iOS>FrutigerLTStd-BoldCn</OnPlatform.iOS>
                                                                <OnPlatform.Android>FrutigerLTStd-BoldCn.otf#FrutigerLTStd-BoldCn</OnPlatform.Android>
                                                            </OnPlatform>
                                                        </Label.FontFamily>
                                                        <Label.GestureRecognizers>
                                                            <TapGestureRecognizer Tapped="OnTapGestureRecognizerDetail" />
                                                        </Label.GestureRecognizers>
                                                    </Label>
                                                    <Label Margin="0,0,0,2" Text="Borrar" Grid.Column="2"
                                                        Font="Bold, 13" ClassId="{Binding IdContacto}" TextColor="Gray" VerticalOptions="End">
                                                        <Label.FontFamily>
                                                            <OnPlatform x:TypeArguments="x:String" iOS="8">
                                                                <OnPlatform.iOS>FrutigerLTStd-BoldCn</OnPlatform.iOS>
                                                                <OnPlatform.Android>FrutigerLTStd-BoldCn.otf#FrutigerLTStd-BoldCn</OnPlatform.Android>
                                                            </OnPlatform>
                                                        </Label.FontFamily>
                                                        <Label.GestureRecognizers>
                                                            <TapGestureRecognizer Tapped="OnTapGestureRecognizerDelete" />
                                                        </Label.GestureRecognizers>
                                                    </Label>
                                                </Grid>
                                            </ContentView>
                                        </StackLayout>
                                    </ViewCell.View>
                                </ViewCell>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>
                </ContentView>
                <StackLayout>
                    <extended:CustomButton Text="Volver" Grid.Row="1" x:Name="dismissbutton" Clicked="OnDismissButtonClicked" WidthRequest="100" Style="{StaticResource buttonStyle}" />
                </StackLayout>
            </Grid>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

最佳答案

问题

  1. 通常,您在使用数据绑定(bind)时可能会发现这类错误。当我们尝试将不同类型与一些编译器不完全期望的控件绑定(bind)时。

很少有地方我能发现装订有点奇怪 - ClassId="{Binding IdContacto}"

我不确定你是否绑定(bind)正确。如果我是你,我会删除所有 ClassId 绑定(bind),然后继续构建编译过程。

  1. 此外,我会尝试注释掉 OnPlatform 代码并构建/编译/运行项目。

这些是您可以尝试的一些远射。在查看整个 Stacktrace 之后,我们可能能够针对具体问题做出更多诊断。

关于xamarin - 错误 : Specified cast is not valid.,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43880628/

相关文章:

android - 在 Xamarin.Forms 中布局背景图像

适用于 Windows 桌面应用程序的 Xamarin 窗体?

ios - 在不使用 Xamarin 中的默认/内置应用程序的情况下在 iOS 和 Android 中发送电子邮件

ios - 如何根据缩放级别为 map 锻炼相关注释?

C#如何将图像保存到SD卡?

listview - 如何在Xamarin表单中设计listview viewcell设计?

xamarin - android中的TableView部分标题为空白且可点击

c# - Xamarin Resource.Id 无法识别

c# - 如何根据反序列化 json 字符串的响应在两个类之间切换?

c# - Xamarin 表单 - 切换条目 View 的 IsPassword 时出错(仅在 uwp 上)