如何在uwp中获取我的复选框值(在列表视图上)并将其转换为json数组

时间:2017-05-03 15:48:41

标签: c# uwp

// check box code    
private void Checkbox_Checked(object sender, RoutedEventArgs e)
{            
    List<Service> selectedItems = new List<Service>();
    CheckBox checkbox = (CheckBox)sender;
    item = checkbox.DataContext.ToString();
    string items = string.Join(",", item.ToArray());
    you = new Service();
    you.id = items;
}

// submit button

private void book_Click(object sender, RoutedEventArgs e)
{
    string name = Name.Text;
    string phone = Phone.Text;
    string email = Email.Text;
    string date = Date.Date.ToString();
    string description = Description.Text;
    Rootobjectsss objnewobject = new Rootobjectsss();
    objnewobject.customerName = name;
    objnewobject.email = email;
    objnewobject.appointmentDate = date;
    objnewobject.description = description;
    objnewobject.service[0].id = you.id.ToString();
    json = string.Empty;
    json = Newtonsoft.Json.JsonConvert.SerializeObject(objnewobject, Newtonsoft.Json.Formatting.Indented);
    Debug.WriteLine(json);
    getData(json);
}


// Object class
public class Rootobjectsss
{
    public string customerName { get; set; }
    public string email { get; set; }
    public string appointmentDate { get; set; }
    public string description { get; set; }
    public Medicalcenters medicalCenters { get; set; }
    public Service[] service { get; set; }
}

public class Medicalcenters
{
    public string id { get; set; }
    public string email { get; set; }
    public string address { get; set; }
    public string centerName { get; set; }
}


public class Service
{
    public string id { get; set; }
}


//XAML on listview and checkbox
<ListView x:Name="serviceListView"
     IsItemClickEnabled="True"
     ItemClick="serviceListView_ItemClick"
     SelectionChanged="serviceListView_SelectionChanged"
     HorizontalAlignment="Stretch">
  <ListView.ItemTemplate>
     <DataTemplate x:DataType="data:Class3">
        <Grid>
        <StackPanel HorizontalAlignment="Stretch" Orientation="Horizontal">
           <TextBlock FontSize="16" Text="{Binding sname}" TextWrapping="Wrap"/>
           <CheckBox DataContext="{Binding id}" Name="myCheckBox" FontSize="10" Checked="Checkbox_Checked" Unchecked="myCheckBox_Checked" Margin="20,0,0,0">
           </CheckBox>
       </StackPanel>
     </Grid>
   </DataTemplate>
 </ListView.ItemTemplate>
</ListView>

// expected result
{"customerName": customer_name,
 "email": email,
 "appointmentDate": appointment_date,
 "description":description,
 "medicalCenters":{"id": medical_center_id, "email": medical_center_email, "address": medical_center_address, "centerName": medical_center_name}
"service",[{"id": service_id}, {"id": service_id}]
}

1 个答案:

答案 0 :(得分:0)

我测试了你的代码片段,它会在System.NullReferenceException代码行抛出objnewobject.service[0].id = you.id.ToString();异常。因此,您似乎没有将Rootobjectsss实例转换为json字符串的问题,但是如何将选定的CheckBox记录转换为service数组存在问题。

在这种情况下,您可以将选定的Service保存到List<Service>集合,然后按ToArray方法将此集合转换为数组。

更新后的代码如下(还添加了未上传的未经检查的代码):

Service you = new Service();
List<Service> selectedItems = new List<Service>();
private string json;
public MainPage()
{
    this.InitializeComponent();
    ObservableCollection<Class3> items = new ObservableCollection<Class3>()
    {
        new Class3()
        {
            id="1",
            sname="name1"
        },
        new Class3()
         {
            id="2",
            sname="name2"
        },
        new Class3()
        {
            id="3",
            sname="name3"
        }
    };
    serviceListView.ItemsSource = items;
}
// check box code    
private void Checkbox_Checked(object sender, RoutedEventArgs e)
{
    CheckBox checkbox = (CheckBox)sender;
    string item = checkbox.DataContext.ToString();
    Service you = new Service();
    you.id = item;
    selectedItems.Add(you);
}
// submit button

private void book_Click(object sender, RoutedEventArgs e)
{
    string name = "name1";
    string email = "test@microsoft.com";
    string date = System.DateTime.Now.ToString();
    string description = "I'm the describtion";
    Rootobjectsss objnewobject = new Rootobjectsss()
    {
        customerName = name,
        email = email,
        appointmentDate = date,
        description = description,
        service = selectedItems.ToArray()
    };
    json = string.Empty;
    json = Newtonsoft.Json.JsonConvert.SerializeObject(objnewobject, Newtonsoft.Json.Formatting.Indented);
    Debug.WriteLine(json);
    //getData(json);
}
private void myCheckBox_Checked(object sender, RoutedEventArgs e)
{
    CheckBox checkbox = (CheckBox)sender;
    foreach (Service removeitem in selectedItems)
    {
        if (removeitem.id == checkbox.DataContext.ToString())
        {
            selectedItems.Remove(removeitem);
            break;
        }
    }
}

结果:

{
  "customerName": "name1",
  "email": "test@microsoft.com",
  "appointmentDate": "5/4/2017 5:59:30 PM",
  "description": "I'm the describtion",
  "medicalCenters": null,
  "service": [
    {
      "id": "1"
    },
    {
      "id": "3"
    }
  ]
}

我建议您为IsChecked实体添加Class3属性,然后您可以直接获取所有选中的项目,而不是checkedunchecked个事件。