XAML/C# - 来自 JSON 的汽车集线器
XAML/C# - Auto hub from JSON
如何根据 XAML/C# 中的 JSON 文件的内容自动创建中心?我的 JSON 看起来像这样:
[
{
"Title": "All Things Go",
"Length": "4:53",
"Features": "N/A"
},
{
"Title": "I Lied",
"Length": "5:04",
"Features": "N/A"
}
]
然后我想为每个对象自动创建一个中心,然后在其中添加信息。有人知道如何或是否可行吗?
假设有一个名为 "test.json" 的 JSON 文件包含您上面显示的内容,位于项目的 "Assets" 文件夹内。 "test.json"文件如下:
[
{
"Title": "All Things Go",
"Length": "4:53",
"Features": "N/A"
},
{
"Title": "I Lied",
"Length": "5:04",
"Features": "N/A"
}
]
现在我们要创建一个 Hub control from this JSON file. To do this, firstly need to read items one by one from this JSON file(we can read it into an entity here I create a Hubcontrol
class), secondly need to set these items as properties for HubSection
,然后将 HubSection
添加到 Hub 控件。所以示例代码如下:
XAML代码
<Page
...
mc:Ignorable="d" Loaded="Page_Loaded">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" x:Name="root">
</Grid>
</Page>
后面的代码:
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private async void Page_Loaded(object sender, RoutedEventArgs e)
{
StorageFile jsonfile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/test.json"));
string jsonString = await FileIO.ReadTextAsync(jsonfile);
var Allhubs = JsonArray.Parse(jsonString);
//Read json file, and deserialization the json string to Hubcontrol class.
List<Hubcontrol> hubsources = new List<Hubcontrol>();
foreach (IJsonValue jsonValue in Allhubs)
{
if (jsonValue.ValueType == JsonValueType.Object)
{
JsonObject hubcontrolitem = jsonValue.GetObject();
hubsources.Add(new Hubcontrol()
{
Title = hubcontrolitem.GetNamedString("Title"),
Length = hubcontrolitem.GetNamedString("Length"),
Features = hubcontrolitem.GetNamedString("Features")
});
}
}
//Create a new hub control, add hubsections which title is got from json
Hub HubFromJson = new Hub();
foreach (Hubcontrol hubcontrolitem in hubsources)
{
HubSection sectionitem = new HubSection();
sectionitem.Header = hubcontrolitem.Title;
HubFromJson.Sections.Add(sectionitem);
}
root.Children.Add(HubFromJson);
}
}
public class Hubcontrol
{
public string Title { get; set; }
public string Length { get; set; }
public string Features { get; set; }
}
我上传了演示 here。
如何根据 XAML/C# 中的 JSON 文件的内容自动创建中心?我的 JSON 看起来像这样:
[
{
"Title": "All Things Go",
"Length": "4:53",
"Features": "N/A"
},
{
"Title": "I Lied",
"Length": "5:04",
"Features": "N/A"
}
]
然后我想为每个对象自动创建一个中心,然后在其中添加信息。有人知道如何或是否可行吗?
假设有一个名为 "test.json" 的 JSON 文件包含您上面显示的内容,位于项目的 "Assets" 文件夹内。 "test.json"文件如下:
[
{
"Title": "All Things Go",
"Length": "4:53",
"Features": "N/A"
},
{
"Title": "I Lied",
"Length": "5:04",
"Features": "N/A"
}
]
现在我们要创建一个 Hub control from this JSON file. To do this, firstly need to read items one by one from this JSON file(we can read it into an entity here I create a Hubcontrol
class), secondly need to set these items as properties for HubSection
,然后将 HubSection
添加到 Hub 控件。所以示例代码如下:
XAML代码
<Page
...
mc:Ignorable="d" Loaded="Page_Loaded">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" x:Name="root">
</Grid>
</Page>
后面的代码:
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private async void Page_Loaded(object sender, RoutedEventArgs e)
{
StorageFile jsonfile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/test.json"));
string jsonString = await FileIO.ReadTextAsync(jsonfile);
var Allhubs = JsonArray.Parse(jsonString);
//Read json file, and deserialization the json string to Hubcontrol class.
List<Hubcontrol> hubsources = new List<Hubcontrol>();
foreach (IJsonValue jsonValue in Allhubs)
{
if (jsonValue.ValueType == JsonValueType.Object)
{
JsonObject hubcontrolitem = jsonValue.GetObject();
hubsources.Add(new Hubcontrol()
{
Title = hubcontrolitem.GetNamedString("Title"),
Length = hubcontrolitem.GetNamedString("Length"),
Features = hubcontrolitem.GetNamedString("Features")
});
}
}
//Create a new hub control, add hubsections which title is got from json
Hub HubFromJson = new Hub();
foreach (Hubcontrol hubcontrolitem in hubsources)
{
HubSection sectionitem = new HubSection();
sectionitem.Header = hubcontrolitem.Title;
HubFromJson.Sections.Add(sectionitem);
}
root.Children.Add(HubFromJson);
}
}
public class Hubcontrol
{
public string Title { get; set; }
public string Length { get; set; }
public string Features { get; set; }
}
我上传了演示 here。