【问题标题】:Write a Text File (or PDF File) from Json in Xamarin.forms在 Xamarin.forms 中从 Json 写入文本文件(或 PDF 文件)
【发布时间】:2021-10-12 13:39:45
【问题描述】:

我正在尝试将我的 listView(其中包含多个绑定对象和用户条目作为输入)写入文本文件。 我想出了将其序列化为 Json 的想法,然后单击按钮将其写入文本(我是新来的 :))。但是我经常遇到 Json Loops 错误。

我的listView的名字是LL:

private void Save_Clicked(object sender, EventArgs e)
{
    string Listi = JsonConvert.SerializeObject(LL);

    File.WriteAllText(DailyTex, Listi);

    editor.Text = File.ReadAllText(DailyTex);

    DisplayAlert("Save completed", "Please Try Egain", "Continue");
}

错误是:

Newtonsoft.Json.JsonSerializationException:“检测到类型为“Xamarin.Forms.Grid”的属性“ParentView”的自引用循环。路径 'TemplatedItems[0].View.Children[0]'.'

我的 Xaml 页面

<ListView x:Name="LL" Grid.Row="3"   ItemsSource="{Binding energy}" HeightRequest="300" >
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <ViewCell>

                                    <Grid  Padding="5" BackgroundColor="White" RowSpacing="40" >
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="50"></RowDefinition>
                                            <RowDefinition Height="50"></RowDefinition>
                                        </Grid.RowDefinitions>

                                        <Label x:Name="label" HorizontalOptions="Center" Text="{Binding EE}" FontSize="18"  TextColor="Black" Grid.Column="0" Grid.Row="0" />
                                        <Entry TextChanged="Entry_TextChanged" HorizontalOptions="Center" VerticalOptions="Center" Keyboard="Numeric" Placeholder=". . . . . . . ." Grid.Column="1" Grid.Row="0" BackgroundColor="Beige" WidthRequest="80" />
                                        <Entry TextChanged="Entry_TextChanged_1" HorizontalOptions="Center" VerticalOptions="Center" Keyboard="Numeric" Placeholder=". . . . . . . ." Grid.Column="2" Grid.Row="0" BackgroundColor="Beige" WidthRequest="80"/>
                                        <Entry TextChanged="Entry_TextChanged_2" HorizontalOptions="Center" VerticalOptions="Center" Keyboard="Numeric" Placeholder=". . . . . . . ." Grid.Column="3" Grid.Row="0" BackgroundColor="Beige" WidthRequest="80"/>
                                        <BoxView Grid.Column="0" Grid.Row="0" BackgroundColor="Black" WidthRequest="1" HorizontalOptions="EndAndExpand" VerticalOptions="FillAndExpand"/>
                                        <BoxView Grid.Column="1" Grid.Row="0" BackgroundColor="Black" WidthRequest="1" HorizontalOptions="EndAndExpand" VerticalOptions="FillAndExpand"/>
                                        <BoxView Grid.Column="2" Grid.Row="0" BackgroundColor="Black" WidthRequest="1" HorizontalOptions="EndAndExpand" VerticalOptions="FillAndExpand"/>


                                    </Grid>

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

现在我可以用模型打印这个:

【问题讨论】:

  • ListView 是一个复杂的 UI 对象。你不能像那样序列化它。您需要序列化作为 ListvView 的 ItemsSource 的数据对象
  • 没有 NuGet 来完成这项工作吗?
  • 是的,Newtonsoft。将您的 ItemSource 序列化为文件将需要两行代码。
  • 它有趣的列表视图没有 foreach 语句...
  • @BehzadChangizi - listview 不支持 foreach 并非偶然(或疏忽)。 ListView 是一个复杂的实体,它的工作是使某些东西在视觉上出现。它不打算以您尝试的方式使用。事实上,考虑到不同的平台以及它们的发展方式,很难使这项工作始终如一地工作。不要尝试从列表视图中提取任何内容。相反,从用于创建列表视图的 Source 中获取该信息。这是“视图和模型分离”的一部分。

标签: xamarin.forms json.net


【解决方案1】:

您可以将 listview 的 ItemsSource 设置为对象集合,例如:

    ObservableCollection<MyText> txts = new ObservableCollection<MyText>();
    MyText txt1 = new MyText();
    MyText txt2 = new MyText();
    txt1.num = "1";
    txt2.num = "2";
    txts.Add(txt1);
    txts.Add(txt2);
   ll.ItemsSource = txts;

当你保存时,你可以像这样序列化 ItemsSource:

 private void saveload_clicked(object sender, EventArgs e)
{
    string jsontxt = JsonConvert.SerializeObject(ll.ItemsSource);
    File.WriteAllText(myfile, jsontxt);
    displaytext.Text = File.ReadAllText(myfile);
    Console.WriteLine("done");
}

这是我的 xaml 和运行截图:

     <ContentPage.Content>
        <StackLayout>
            <Button Text="SaveandLoad" Clicked="saveload_clicked"/>
            <Label x:Name="displaytext"/>
       <ListView x:Name="ll" SeparatorColor="Blue" RowHeight="200">
           <ListView.ItemTemplate>
               <DataTemplate>
                   <ViewCell >
                       <StackLayout>
                       <Label Text="{Binding num}" BackgroundColor="Green" WidthRequest="20"/>
                       <Entry Text="{Binding line1}"/>
                       <Entry Text="{Binding line2}"/>
                       <Entry Text="{Binding line3}"/>
                           </StackLayout>
                       </ViewCell>
               </DataTemplate>
           </ListView.ItemTemplate>
       </ListView>
            </StackLayout>
    </ContentPage.Content>
</ContentPage>

【讨论】:

  • Tanx Adrain,我会尝试这样做并发送结果。
  • 我快到了,伙计。有了你的帮助,我现在可以写我的条目了。但是你可以看到它一团糟。我不想在我的文本中使用 Cax Sym Fla。有人有什么建议吗?
  • 我在我的问题中添加了一张图片。
  • 问题编号 2:正如您所见,每次我向模型添加条目值时,模型中的其他两个值都保存为 Null .....
  • 对于您的第一个问题,这就是 JsonConvert 的工作原理。对于第二个问题,如果不希望 null 值被序列化,您可以选择不序列化整个 itemssource 而是序列化条目文本。
猜你喜欢
  • 2021-06-22
  • 2019-01-15
  • 2017-05-01
  • 2019-01-19
  • 1970-01-01
  • 1970-01-01
  • 2019-04-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多