我正在使用Xamarin开发跨平台应用程序。数据源是一个JSON文件,其中包含一些单词及其韵律,如下所示。我必须从JSON文件中读取单词,并为此设计了一个模型类“ WordRhyme.cs”。 。我的代码工作正常,但是由于某些原因,我无法为每个单词检索押韵。我一直在寻找过去三天的解决方案,但是没有运气。当Rhymes返回列表时,我还尝试用嵌套在单词list内的List代替Labels,但效果不佳(在下面添加了xaml编码)。还有一种方法只能从JSON中选择一些记录,因为它可能有数百个,但我想显示一些。如此谷歌搜索之前,我还没有在Xamarin中编程。
json1.json
{
"words": [
{
"word": "coffee",
"syllables": 2,
"topic": "Food and drink",
"rhymes": [
{
"word": "toffee",
"score": 385,
"numSyllables": 2
},
{
"word": "naafi",
"score": 178,
"numSyllables": 2
},
{
"word": "tophi",
"score": 172,
"numSyllables": 2
},
{
"word": "sofie",
"score": 75,
"numSyllables": 2
},
{
"word": "yoffie",
"numSyllables": 2
}
]
},
{
"word": "Cat",
"syllables": 2,
"topic": "Animals",
"rhymes": [
{
"word": "Hat",
"score": 385,
"numSyllables": 2
},
{
"word": "Fat",
"score": 178,
"numSyllables": 2
},
{
"word": "Mat",
"score": 172,
"numSyllables": 2
},
{
"word": "coffey2",
"score": 152,
"numSyllables": 2
},
{
"word": "toffy2",
"score": 119,
"numSyllables": 2
} ]
}
]
}
WordRhyme.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace JsonTest2.Models
{
public class Rhyme
{
public string word { get; set; }
public int score { get; set; }
public int numSyllables { get; set; }
}
public class Word
{
public string word { get; set; }
public int syllables { get; set; }
public string topic { get; set; }
public List<Rhyme> rhymes { get; set; }
}
public class WordRhymes
{
public List<Word> words { get; set; }
}
}
的Page1.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"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="JsonTest2.Page1">
<ContentPage.Content>
<StackLayout Orientation="Vertical">
<Grid>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Label Grid.Row="0" Margin="30" Text="Rhyming Words JSON Parsing" FontSize="25" />
<ListView x:Name="listViewWords" Grid.Row="1" HorizontalOptions="FillAndExpand" Footer="" HasUnevenRows="True">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid HorizontalOptions="FillAndExpand" Padding="10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Label Text="{Binding word}" HorizontalOptions="StartAndExpand" Grid.Row="0" TextColor="Blue" FontAttributes="Bold"/>
<Label Text="{Binding syllables}" HorizontalOptions="StartAndExpand" Grid.Row="1" TextColor="Orange" FontAttributes="Bold"/>
<Label Text="{Binding topic}" HorizontalOptions="StartAndExpand" Grid.Row="2" TextColor="Gray" FontAttributes="Bold"/>
<Label Text="{Binding rhymes.word}" Grid.Row="3" TextColor="Blue" FontAttributes="Bold"/>
<Label Text="{Binding rhymes.score}" Grid.Row="4" TextColor="Blue" FontAttributes="Bold"/>
<Label Text="{Binding rhymes.numSyllables}" Grid.Row="5" TextColor="Blue" FontAttributes="Bold"/>
<BoxView HeightRequest="2" Margin="0,10,10,0" BackgroundColor="Gray" Grid.Row="6" HorizontalOptions="FillAndExpand" />
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</Grid>
</StackLayout>
</ContentPage.Content>
</ContentPage>
Page1.xaml.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Reflection;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JsonTest2.Models;
using Newtonsoft.Json;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace JsonTest2
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Page1 : ContentPage
{
public Page1()
{
InitializeComponent();
GetJsonData();
}
void GetJsonData()
{
string jsonFileName = "json1.json";
WordRhymes ObjWordList = new WordRhymes();
var assembly = typeof(Page1).GetTypeInfo().Assembly;
Stream stream = assembly.GetManifestResourceStream($"{assembly.GetName().Name}.{jsonFileName}");
using (var reader = new System.IO.StreamReader(stream))
{
var jsonString = reader.ReadToEnd();
//Converting JSON Array Objects into generic list
ObjWordList = JsonConvert.DeserializeObject<WordRhymes>(jsonString);
}
//Binding listview with json string
listViewWords.ItemsSource = ObjWordList.words;
}
}
}
嵌套ListView Page2Json.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"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="JsonTest2.Page2Json">
<ContentPage.Content>
<StackLayout Orientation="Vertical">
<Grid>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Label Grid.Row="0" Margin="30" Text="Rhyming Words JSON Parsing" FontSize="25" />
<ListView x:Name="listViewWords" Grid.Row="1" HorizontalOptions="FillAndExpand" Footer="" HasUnevenRows="True">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid HorizontalOptions="FillAndExpand" Padding="10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Label Text="{Binding word}" HorizontalOptions="StartAndExpand" Grid.Row="0" TextColor="Blue" FontAttributes="Bold"/>
<Label Text="{Binding syllables}" HorizontalOptions="StartAndExpand" Grid.Row="1" TextColor="Orange" FontAttributes="Bold"/>
<Label Text="{Binding topic}" HorizontalOptions="StartAndExpand" Grid.Row="2" TextColor="Gray" FontAttributes="Bold"/>
<ListView x:Name="listViewRhymes" HorizontalOptions="FillAndExpand" ItemsSource="{Binding listViewWords.rhymes}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid HorizontalOptions="FillAndExpand">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Label Text="{Binding rhymes.word}"></Label>
<Label Text="{Binding rhymes.score}"></Label>
<Label Text="{Binding rhymes.numSyllables}"></Label>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Label Text="{Binding rhymes.word}" HorizontalOptions="StartAndExpand" Grid.Row="2" TextColor="Gray" FontAttributes="Bold"/>
<BoxView HeightRequest="2" Margin="0,10,10,0" BackgroundColor="Gray" Grid.Row="3" HorizontalOptions="FillAndExpand" />
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<StackLayout Orientation="Horizontal">
<Button x:Name="nextButton" Text="Next" Clicked="NextButton_Clicked"></Button>
<Button x:Name="backButton" Text="Back" Clicked="BackButton_Clicked"></Button>
</StackLayout>
</Grid>
</Grid>
</StackLayout>
</ContentPage.Content>
</ContentPage>
答案 0 :(得分:0)
因此,在Page2Json.xaml的内部列表中,您的itemsource与listViewWords.rhymes
绑定,我认为应该仅与rhymes
绑定,即使在此listview标签中也不应与rhymes.something
绑定他们应该是something
这是我认为应该工作的更新代码
<ListView x:Name="listViewRhymes" HorizontalOptions="FillAndExpand" ItemsSource="{Binding rhymes}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid HorizontalOptions="FillAndExpand">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Label Text="{Binding word}"></Label>
<Label Text="{Binding score}"></Label>
<Label Text="{Binding numSyllables}"></Label>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
这些更改正在考虑您的page2json.xaml.cs与page1json.xaml.cs相同