Skip to content

Instantly share code, notes, and snippets.

@KerryRitter
Last active March 16, 2017 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KerryRitter/e6bf73bd37ec741dfbb3071ead653104 to your computer and use it in GitHub Desktop.
Save KerryRitter/e6bf73bd37ec741dfbb3071ead653104 to your computer and use it in GitHub Desktop.
xamarin.forms form example
using System;
using System.Collections.Generic;
using System.Linq;
using Realms;
using StubbsTest.Models;
namespace StubbsTest.Data
{
public class FormRepository : IDisposable
{
private Realm _realm;
private Realm Realm => _realm ?? (_realm = Realm.GetInstance());
public List<FormModel> GetAll()
{
return Realm.All<FormModel>().ToList();
}
public void Save(FormModel formModel)
{
Realm.Write(() =>
{
Realm.Add(new FormModel
{
FirstName = formModel.FirstName,
LastName = formModel.LastName
});
});
}
public void Dispose()
{
_realm.Dispose();
}
}
}
using Xamarin.Forms;
namespace StubbsTest.Views
{
public class FormView : ContentPage
{
private static readonly Entry FirstNameEntry = new Entry();
private static readonly Entry LastNameEntry = new Entry();
public FormView()
{
BackgroundImage = "bg.jpg";
var grid = new Grid();
grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
grid.Children.Add(new Label { Text = "First Name" }, 0, 0);
grid.Children.Add(new Label { Text = "Last Name" }, 0, 1);
grid.Children.Add(FirstNameEntry, 1, 0);
grid.Children.Add(LastNameEntry, 1, 1);
var submitButton = new Button { Text = "Submit" };
submitButton.Clicked += async (sender, args) =>
{
await DisplayAlert("Submitted", $"Thanks {FirstNameEntry.Text} {LastNameEntry.Text}!", "Close");
};
Content = new AbsoluteLayout
{
Children =
{
new StackLayout
{
VerticalOptions = LayoutOptions.Center,
HorizontalOptions = LayoutOptions.FillAndExpand,
Children =
{
grid,
submitButton
}
}
}
};
}
}
}
using Xamarin.Forms;
namespace StubbsTest.Views
{
public class FormView : ContentPage
{
private Entry _firstNameEntry;
private Entry _lastNameEntry;
private Grid _formGrid;
private Button _submitButton;
private Entry FirstNameEntry
{
get
{
if (_firstNameEntry != null)
{
return _firstNameEntry;
}
return _firstNameEntry = new Entry();
}
}
private Entry LastNameEntry
{
get
{
if (_lastNameEntry != null)
{
return _lastNameEntry;
}
return _lastNameEntry = new Entry();
}
}
private Grid FormGrid
{
get
{
if (_formGrid != null)
{
return _formGrid;
}
var grid = new Grid();
grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
grid.Children.Add(new Label { Text = "First Name" }, 0, 0);
grid.Children.Add(new Label { Text = "Last Name" }, 0, 1);
grid.Children.Add(FirstNameEntry, 1, 0);
grid.Children.Add(LastNameEntry, 1, 1);
return _formGrid = grid;
}
}
private Button SubmitButton
{
get
{
if (_submitButton != null)
{
return _submitButton;
}
var submitButton = new Button { Text = "Submit" };
submitButton.Clicked += async (sender, args) =>
{
await DisplayAlert("Submitted", $"Thanks {FirstNameEntry.Text} {LastNameEntry.Text}!", "Close");
};
return _submitButton = submitButton;
}
}
public FormView()
{
BackgroundImage = "bg.jpg";
Content = new StackLayout
{
VerticalOptions = LayoutOptions.Center,
HorizontalOptions = LayoutOptions.FillAndExpand,
Children =
{
FormGrid,
SubmitButton
}
};
}
}
}
using StubbsTest.ViewComponents;
using Xamarin.Forms;
namespace StubbsTest.Models
{
public class FormModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
}
namespace StubbsTest.ViewComponents
{
public class FormComponent : Grid
{
private Entry _firstNameEntry;
private Entry _lastNameEntry;
private Entry FirstNameEntry => _firstNameEntry ?? (_firstNameEntry = new Entry());
private Entry LastNameEntry => _lastNameEntry ?? (_lastNameEntry = new Entry());
public FormComponent()
{
RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
Children.Add(new Label { Text = "First Name" }, 0, 0);
Children.Add(new Label { Text = "Last Name" }, 0, 1);
Children.Add(FirstNameEntry, 1, 0);
Children.Add(LastNameEntry, 1, 1);
}
public FormModel GetInputValues()
{
return new FormModel
{
FirstName = FirstNameEntry.Text,
LastName = LastNameEntry.Text
};
}
}
}
namespace StubbsTest.Views
{
public class FormView : ContentPage
{
private Button _submitButton;
private Button SubmitButton
{
get
{
if (_submitButton != null)
{
return _submitButton;
}
var submitButton = new Button { Text = "Submit" };
submitButton.Clicked += async (sender, args) =>
{
var formData = Form.GetInputValues();
await DisplayAlert("Submitted", $"Thanks {formData.FirstName} {formData.LastName}!", "Close");
};
return _submitButton = submitButton;
}
}
private FormComponent _form;
private FormComponent Form => _form ?? (_form = new FormComponent());
public FormView()
{
BackgroundImage = "bg.jpg";
Content = new StackLayout
{
VerticalOptions = LayoutOptions.Center,
HorizontalOptions = LayoutOptions.FillAndExpand,
Children =
{
Form,
SubmitButton
}
};
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment