Content.View初始化上的System.FormatException

时间:2019-05-06 13:01:05

标签: c# xaml xamarin.forms

我正在为我的学士学位开发一个应用程序,用于创建和管理考古现场指南。一种功能应使用户能够在应用程序中创建新的《现场指南》。现场指南应包括常规信息(标题,作者等)和工件类型的多个条目。 我想通过一个ContentPage具有两个视图来实现这一点-一个用于book-info,一个用于条目列表。用户应该能够在两个视图之间切换。 为此,我创建了两个ContentView,将它们包含在Parent-XAML中,并将其IsVisible属性绑定到ViewModel中的各个布尔值。奇怪的是,“ NewBookInfo”在初始化时抛出了异常,而“ NewBookEntryList”工作得很好。我在Google上找不到解决方案,现在有点抓稻草...

<ColumnDefinition Width="100/3*" />InitializeComponent()中导致运行时异常:

System.FormatException
Message=One of the identified items was in an invalid format.
Source=Xamarin.Forms.Core
StackTrace:
 at Xamarin.Forms.GridLengthTypeConverter.ConvertFromInvariantString(String value)
 ...

我的父页面:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"     
             xmlns:local="clr-namespace:FieldGuide.Views"
             x:Class="FieldGuide.AddBook">

    <StackLayout Spacing="1" VerticalOptions="Fill">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />

            </Grid.ColumnDefinitions>
            <Label Text="New Guide" FontAttributes="Bold" FontSize="30" 
                   HorizontalOptions="Center"
                   VerticalOptions="CenterAndExpand"
                   Grid.Row="0" Grid.Column="0" />
            <ImageButton Source="Return_klein.png" Grid.Row="0" Grid.Column="1" Command="{Binding ReturnCommand}"/>
            <ImageButton Source="Hamburger_Icon_klein.png" Grid.Row="0" Grid.Column="2" Command="{Binding MenuCommand}"/>
        </Grid>
        <!--BackgroundColor="Transparent" bei Buttons einfügen-->
        <local:NewBookInfo VerticalOptions="FillAndExpand" IsVisible="{Binding BookInfoVisible}"/>
        <local:NewBookEntryList VerticalOptions="FillAndExpand" IsVisible="{Binding BookEntriesVisible}"/>
    </StackLayout>

</ContentPage>

NewBookInfo.xaml

<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="FieldGuide.Views.NewBookInfo">
  <ContentView.Content>
        <StackLayout>
            <Grid Margin="10, 10">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="30*"/>
                    <ColumnDefinition Width="70*"/>
                </Grid.ColumnDefinitions>
                <Label Text="Title" FontSize="20" 
                   HorizontalOptions="Center" VerticalOptions="CenterAndExpand" 
                   Grid.Row="0" Grid.Column="0"/>
                <Label Text="Author" FontSize="20" 
                   HorizontalOptions="Center" VerticalOptions="CenterAndExpand" 
                   Grid.Row="1" Grid.Column="0"/>
                <Label Text="Tags" FontSize="20" 
                   HorizontalOptions="Center" VerticalOptions="CenterAndExpand" 
                   Grid.Row="2" Grid.Column="0"/>
                <Entry Grid.Row="0" Grid.Column="1"/>
                <Entry Grid.Row="1" Grid.Column="1"/>
                <Entry Placeholder="Separate with Comma" Grid.Row="2" Grid.Column="1"/>
            </Grid>
            <Grid VerticalOptions="EndAndExpand" Margin="10, 10">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="100/3*"/>
                    <ColumnDefinition Width="100/3*"/>
                    <ColumnDefinition Width="100/3*"/>
                </Grid.ColumnDefinitions>
                <Button Text="Save" Grid.Row="0" Grid.Column="0" 
                        Command="{Binding SaveBook}"/>
                <Button Text="Entries" Grid.Row="0" Grid.Column="1" 
                        Command="{Binding NewEntry}"/>
                <Button Text="Discard" Grid.Row="0" Grid.Column="2" 
                        Command="{Binding DiscardBook}"/>
            </Grid>
        </StackLayout>
    </ContentView.Content>
</ContentView>

为进行比较NewBookEntryList.xaml(ListView尚未完成)

<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="FieldGuide.Views.NewBookEntryList">
    <ContentView.Content>
        <StackLayout>
            <ListView x:Name="FileSystem" ItemsSource="{Binding Entries}" SelectedItem="{Binding SelectedEntry}" VerticalOptions="FillAndExpand">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*"/>
                                </Grid.ColumnDefinitions>

                                <Label Text="{Binding Name}" Margin="10" FontSize="Medium" VerticalOptions="Center" HorizontalOptions="StartAndExpand"/>
                            </Grid>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
            <Grid VerticalOptions="EndAndExpand" Margin="10, 10">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="50*"/>
                    <ColumnDefinition Width="50*"/>
                </Grid.ColumnDefinitions>
                <Button Text="Book Info" Grid.Row="0" Grid.Column="0" 
                        Command="{Binding SaveBook}"/>
                <Button Text="New Entry" Grid.Row="0" Grid.Column="1" 
                        Command="{Binding NewEntry}"/>
            </Grid>
        </StackLayout>
    </ContentView.Content>
</ContentView>

这就是我希望它最终出现的方式。

1 个答案:

答案 0 :(得分:1)

问题出在NewBookInfo.xaml中最后一个网格的宽度声明上,但是VS中内置的错误检测功能并没有标记该错误。 代替

<ColumnDefinition Width="100/3*"/>

我只是将其替换为

<ColumnDefinition Width="33*"/>

感谢@Jason在评论中提供帮助。