UWP C#如何附加.json文件并在组中识别它们
我正在尝试创建一个UWP C#如何附加.json文件并在组中识别它们,c#,json,uwp,windows-iot-core-10,C#,Json,Uwp,Windows Iot Core 10,我正在尝试创建一个usercontrol来显示windows iot core上json文件中的项目组 我有一个“创建组”按钮。按下后,它将创建64个带有相应详细信息的用户控件,并显示在滚动查看器中。随后,我可以编辑64个usercontrol中的任意一个项目,然后保存json文件 我的usercontrol课程如下: 我有一个问题,就是如何创建64个项目的不同组,并将所有项目附加到同一个json文件中,然后从所提到的不同组的选择中显示它们 请帮忙,谢谢 集体课 [DataContract] p
usercontrol
来显示windows iot core上json
文件中的项目组
我有一个“创建组”按钮。按下后,它将创建64个带有相应详细信息的用户控件,并显示在滚动查看器中。随后,我可以编辑64个usercontrol
中的任意一个项目,然后保存json
文件
我的usercontrol
课程如下:
我有一个问题,就是如何创建64个项目的不同组,并将所有项目附加到同一个json
文件中,然后从所提到的不同组的选择中显示它们
请帮忙,谢谢
集体课
[DataContract]
public class DecoderGroup
{
[DataMember]
public int groupID{ get; set; }
[DataMember]
public string groupName{ get; set; }
[DataMember]
public int cardAddress { get; set; }
[DataMember]
public bool enabled { get; set; }
[DataMember]
public int z1label { get; set; }
[DataMember]
public int z2label { get; set; }
[DataMember]
public int z3label { get; set; }
[DataMember]
public int z4label { get; set; }
[DataMember]
public bool zone1 { get; set; }
[DataMember]
public bool zone2 { get; set; }
[DataMember]
public bool zone3 { get; set; }
[DataMember]
public bool zone4 { get; set; }
[DataMember]
public List<byte> txData { get; set; }
public DecoderGroup(int id, int address, int z1, int z2, int z3, int z4)
{
groupName = "Group";
zone1 = false;
zone2 = false;
zone3 = false;
zone4 = false;
z1label = z1;
z2label = z2;
z3label = z3;
z4label = z4;
}
}
private void AddGroup_Click(object sender, RoutedEventArgs e)
{
ZonesList_Panel.Children.Clear();
int groupid = 1;
int cardadr;
for (cardadr = 1; cardadr <= MAXCARDS; cardadr++)
{
var z4 = (4 * cardadr);
var z3 = (4 * cardadr) - 1;
var z2 = (4 * cardadr) - 2;
var z1 = (4 * cardadr) - 3;
DecoderGroupUserControl decoderGroupControl = new DecoderGroupUserControl(this, new DecoderGroup(groupid, cardadr, z1, z2, z3, z4));
ZonesList_Panel.Children.Add(decoderGroupControl);
}
}
private async void SaveGroup_Click(object sender, RoutedEventArgs e)
{
await saveGroupsToJSON(getGroups());
}
public async Task saveGroupsToJSON(List<DecoderGroup> groups)
{
var serializer = new DataContractJsonSerializer(typeof(List<DecoderGroup>));
using (var stream = await ApplicationData.Current.LocalFolder.OpenStreamForWriteAsync(DECODERGROUPS_FILE, CreationCollisionOption.OpenIfExists))
{
serializer.WriteObject(stream, groups);
}
}
public List<DecoderGroup> getGroups()
{
List<DecoderGroup> ret = new List<DecoderGroup>();
foreach (DecoderGroupUserControl u in ZonesList_Panel.Children)
{
//add condition for group ID
ret.Add(u.decoderGroup);
}
return ret;
}
建议使用UserControl作为ListView的日期模板,这样您就不需要创建多个UserControl并将它们添加到页面中。然后,您可以读取json文件并将json对象转换为集合,并且可以将该集合用作ListView的Itemsource 通过实现
INotifyPropertyChanged
接口,双向数据绑定可以反映对集合的UI更改。最后,您可以将更改后的集合写入json文件
注意,您需要下载Newtonsoft.Json以通过Manage NuGet包解析Json对象。请参考以下代码
MyUserControl1.xaml:
<UserControl
..>
<Grid>
<!--customize the usercontrol style-->
<StackPanel>
<StackPanel Orientation="Horizontal" >
<TextBox Margin="0,0,20,0" Text="{Binding Name,Mode=TwoWay}" BorderThickness="0"/>
<TextBox Text="{Binding Job,Mode=TwoWay}" BorderThickness="0"/>
</StackPanel>
<TextBox Text="{Binding Age,Mode=TwoWay}" BorderThickness="0"/>
</StackPanel>
</Grid>
</UserControl>
MainPage.xaml:
<Page..>
<Grid>
<StackPanel>
<ListView ItemsSource="{x:Bind Results,Mode=TwoWay}">
<ListView.ItemTemplate>
<DataTemplate x:DataType="local:Person">
<local:MyUserControl1>
</local:MyUserControl1>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Button x:Name="SaveButton" Content="Save" Click="SaveButton_Click"/>
</StackPanel>
</Grid>
</Page>
MainPage.xaml.cs:
namespace WriteJson
{
public sealed partial class MainPage : Page
{
public ObservableCollection<Person> Persons { get; set; }
public ObservableCollection<Person> Results { get; set; }
public string path;
public MainPage()
{
this.InitializeComponent();
CreateJsonFile();
path = ApplicationData.Current.LocalFolder.Path + "\\info.json";
Results = JsonConvert.DeserializeObject<ObservableCollection<Person>>(File.ReadAllText(path));
Debug.WriteLine("bind successfully");
}
public async void CreateJsonFile()
{
//check if info.json exists, if it doesn't exist, create it
StorageFolder folder = ApplicationData.Current.LocalFolder;
StorageFile file;
try
{
file = await folder.GetFileAsync("info.json");
}
catch
{
await folder.CreateFileAsync("info.json");
Persons = new ObservableCollection<Person>()
{
new Person(){Name="tom",Job="teacher",Age=24},
new Person(){Name="lily",Job="nurse",Age=20},
new Person(){Name="ming",Job="student",Age=26},
new Person(){Name="kiki",Job="lawyer",Age=28},
new Person(){Name="jack",Job="worker",Age=21},
};
path = ApplicationData.Current.LocalFolder.Path + "\\info.json";
File.WriteAllText(path, JsonConvert.SerializeObject(Persons));
Debug.WriteLine("create a json file successfully");
}
}
private void SaveButton_Click(object sender, RoutedEventArgs e)
{
File.WriteAllText(path, JsonConvert.SerializeObject(Results));
Debug.WriteLine("save successfully");
}
}
public class Person:INotifyPropertyChanged
{
private string name;
private string job;
private int age;
public string Name
{
get { return name; }
set
{
name = value;
RaisePropertyChanged("Name");
}
}
public string Job
{
get { return job; }
set
{
job = value;
RaisePropertyChanged("Job");
}
}
public int Age
{
get { return age; }
set
{
age = value;
RaisePropertyChanged("Age");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propertyname=null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyname));
}
}
}
namespace WriteJson
{
公共密封部分类主页面:第页
{
公共可观察集合人员{get;set;}
公共ObservableCollection结果{get;set;}
公共字符串路径;
公共主页()
{
this.InitializeComponent();
CreateJsonFile();
path=ApplicationData.Current.LocalFolder.path+“\\info.json”;
结果=JsonConvert.DeserializeObject(File.ReadAllText(path));
Debug.WriteLine(“绑定成功”);
}
公共异步void CreateJsonFile()
{
//检查info.json是否存在,如果不存在,则创建它
StorageFolder folder=ApplicationData.Current.LocalFolder;
存储文件;
尝试
{
file=await folder.GetFileAsync(“info.json”);
}
抓住
{
wait folder.CreateFileAsync(“info.json”);
人员=新观察到的集合()
{
新人(){Name=“tom”,Job=“teacher”,年龄=24},
新人(){Name=“lily”,Job=“护士”,年龄=20},
新人(){Name=“ming”,Job=“student”,年龄=26},
新人(){Name=“kiki”,Job=“律师”,年龄=28},
新人(){Name=“jack”,Job=“worker”,年龄=21},
};
path=ApplicationData.Current.LocalFolder.path+“\\info.json”;
WriteAllText(路径,JsonConvert.SerializeObject(Persons));
WriteLine(“成功创建json文件”);
}
}
私有无效保存按钮\u单击(对象发送方,路由目标)
{
WriteAllText(路径,JsonConvert.SerializeObject(结果));
Debug.WriteLine(“保存成功”);
}
}
公共类人员:INotifyPropertyChanged
{
私有字符串名称;
私人字符串作业;
私人互联网;
公共字符串名
{
获取{返回名称;}
设置
{
名称=值;
RaiseProperty变更(“名称”);
}
}
公共字符串作业
{
获取{返回作业;}
设置
{
工作=价值;
RaiseProperty变更(“工作”);
}
}
公共信息
{
获取{返回年龄;}
设置
{
年龄=价值;
提高产权变更(“年龄”);
}
}
公共事件属性更改事件处理程序属性更改;
public void RaisePropertyChanged(字符串propertyname=null)
{
PropertyChanged?.Invoke(这是新的PropertyChangedEventArgs(propertyname));
}
}
}
您有什么进展吗?如果问题尚未解决,请随时与我们联系。
namespace WriteJson
{
public sealed partial class MainPage : Page
{
public ObservableCollection<Person> Persons { get; set; }
public ObservableCollection<Person> Results { get; set; }
public string path;
public MainPage()
{
this.InitializeComponent();
CreateJsonFile();
path = ApplicationData.Current.LocalFolder.Path + "\\info.json";
Results = JsonConvert.DeserializeObject<ObservableCollection<Person>>(File.ReadAllText(path));
Debug.WriteLine("bind successfully");
}
public async void CreateJsonFile()
{
//check if info.json exists, if it doesn't exist, create it
StorageFolder folder = ApplicationData.Current.LocalFolder;
StorageFile file;
try
{
file = await folder.GetFileAsync("info.json");
}
catch
{
await folder.CreateFileAsync("info.json");
Persons = new ObservableCollection<Person>()
{
new Person(){Name="tom",Job="teacher",Age=24},
new Person(){Name="lily",Job="nurse",Age=20},
new Person(){Name="ming",Job="student",Age=26},
new Person(){Name="kiki",Job="lawyer",Age=28},
new Person(){Name="jack",Job="worker",Age=21},
};
path = ApplicationData.Current.LocalFolder.Path + "\\info.json";
File.WriteAllText(path, JsonConvert.SerializeObject(Persons));
Debug.WriteLine("create a json file successfully");
}
}
private void SaveButton_Click(object sender, RoutedEventArgs e)
{
File.WriteAllText(path, JsonConvert.SerializeObject(Results));
Debug.WriteLine("save successfully");
}
}
public class Person:INotifyPropertyChanged
{
private string name;
private string job;
private int age;
public string Name
{
get { return name; }
set
{
name = value;
RaisePropertyChanged("Name");
}
}
public string Job
{
get { return job; }
set
{
job = value;
RaisePropertyChanged("Job");
}
}
public int Age
{
get { return age; }
set
{
age = value;
RaisePropertyChanged("Age");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propertyname=null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyname));
}
}
}