James Poly
James Poly

Reputation: 13

Xamarin forms: how create a form and save it to json file?

I need of an example of xamarin form how to create a form and save it to json file. I create four classes:

public string id { get; set; }
public string test { get; set; }
public string objects { get; set; }
public string tolerance { get; set; }

I create my json file:

{   "testresults": [
    {
      "id": "c200",
      "test": "GT3514/6-G01",
      "objects": "X-axis pitch, EBX",
      "tolerance": "0,040/1000"
    },
    {
      "id": "c201",
      "test": "GT3514/6-G02",
      "objects": "X-axis pitch, EAX",
      "tolerance": "0,030/1000"
    },
    {
      "id": "c202",
      "test": "GT3514/6-G03",
      "objects": "Y-axis pitch, EBY",
      "tolerance": "0,040/1000"
    }   ] }

I create the code to read a json file:

    GetJsonData();
}

void GetJsonData()
{
    string jsonFileName = "testresults.json";
    ContactList ObjContactList = new ContactList();
    var assembly = typeof(MainPage).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  
        ObjContactList = JsonConvert.DeserializeObject<ContactList> 
(jsonString);
    }
    //Binding listview with json string   
    listviewConacts.ItemsSource = ObjContactList.contacts;
}

And I create a form in mainpage.xaml: the form has test, objects, tolerance and two empty input fields Results and Note N..

...
    <ListView.Header>
    <Label Text="Test" Grid.Column="0" Grid.Row="0" /> 
    <Label Text="Object" Grid.Column="1" Grid.Row="0"/> 
    <Label Text="Tolerance" Grid.Column="2" Grid.Row="0"/> 
    <Label Text="Results" Grid.Column="3" Grid.Row="0"/> 
    <Label Text="Note N." Grid.Column="4" Grid.Row="0"/>
...  

<Label Text="{Binding test}" HorizontalOptions="StartAndExpand" Grid.Row="1" Grid.Column="0" FontAttributes="Bold"/>  
<Label Text="{Binding objects}" HorizontalOptions="StartAndExpand" Grid.Row="1" Grid.Column="1"  FontAttributes="Bold"/>  
<Label Text="{Binding tolerance}" HorizontalOptions="StartAndExpand" Grid.Row="1" Grid.Column="2"  FontAttributes="Bold"/>
<Entry Text=" " Grid.Row="1" Grid.Column="3" />
<Entry Text=" " Grid.Row="1" Grid.Column="4" />

How save the whole form to json file with Test, Object, Tolerance and with input field results and note n.?

Upvotes: 0

Views: 644

Answers (1)

Lucas Zhang
Lucas Zhang

Reputation: 18861

You can also use data binding even if the data of listview's head is static .

<ListView.Header>
  <StackLayout Orientation="Horizontal">
     <Label Text="{Binding Test}"  />
     <Label Text="{Binding Object}" />
     <Label Text="{Binding Tolerance}" />
     <Label Text="{Binding Results}" />
     <Label Text="{Binding Note}" />
  </StackLayout>
</ListView.Header>

in ViewCell

<StackLayout Orientation="Horizontal">
    <Label Text="{Binding test}" HorizontalOptions="StartAndExpand"  FontAttributes="Bold"/>
    <Label Text="{Binding objects}" HorizontalOptions="StartAndExpand"   FontAttributes="Bold"/>
    <Label Text="{Binding tolerance}" HorizontalOptions="StartAndExpand"   FontAttributes="Bold"/>
    <Entry Text="{Binding content1 ,Mode=TwoWay}"  />
    <Entry Text="{Binding content2 ,Mode=TwoWay}"  />
</StackLayout>

And in your ViewModel

public class MyViewModel
{

  public ObservableCollection<JsonDataModel> mySource { get; set; }
  public string Test { get; private set; }
  public string Object{ get; private set; }
  public string Tolerance{ get; private set; }
  public string Results{ get; private set; }
  public string Note{ get; private set; }

  public MyViewModel()
  {
     // ... set the ItemsSource of listview

      Test = "Test";
      Object = "Object";
      //...

  }
}

And you can get them and save them to json file as you want .

Upvotes: 1

Related Questions