Lesson 1: Set Data Object
- 2 minutes to read
The DataFormView component allows you to quickly generate an edit form for your data object.
Create a New Application and Add a Data Form
Create a new Xamarin.Forms cross-platform solution and add a DevExpress DataForm component.
In the main project’s App.xaml.cs file, call the DevExpress.XamarinForms.DataForm.Initializer.Init() method to prepare DevExpress DataForm for use in the application.
using Xamarin.Forms; namespace DataFormExample { public partial class App : Application { public App() { DevExpress.XamarinForms.DataForm.Initializer.Init(); InitializeComponent(); MainPage = new MainPage(); } } }
In the iOS project’s AppDelegate.cs file, call the DevExpress.XamarinForms.DataForm.iOS.Initializer.Init() method.
using Foundation; using UIKit; namespace DataFormExample.iOS { [Register("AppDelegate")] public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate { public override bool FinishedLaunching(UIApplication app, NSDictionary options) { global::Xamarin.Forms.Forms.Init(); DevExpress.XamarinForms.DataForm.iOS.Initializer.Init(); LoadApplication(new App()); return base.FinishedLaunching(app, options); } } }
Tip
If you use the DevExpress Xamarin App v22.1 template to create an application, these method calls are automatically added to the code.
In the MainPage.xaml file of a .NET Standard project that has shared code, use the dxdf prefix to declare a namespace and add a DataFormView instance to the ContentPage:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:DataFormExample" x:Class="DataFormExample.MainPage" xmlns:dxdf="http://schemas.devexpress.com/xamarin/2014/forms/dataform"> <ScrollView> <StackLayout> <dxdf:DataFormView x:Name="dataForm"/> </StackLayout> </ScrollView> </ContentPage>
Set a Data Object
Declare the PersonalInfo sample class that defines data to be edited via a data form.
namespace DataFormExample { public class PersonalInfo { public string FirstName { get; set; } public string LastName { get; set; } public DateTime BirthDate { get; set; } public virtual Gender Gender { get; set; } public string Password { get; set; } public string Email { get; set; } public string Phone { get; set; } } public enum Gender { Female, Male, RatherNotSay } }
Assign the PersonalInfo object to the DataFormView.DataObject property.
dataForm.DataObject = new PersonalInfo();
The data form detects all property types for the bound data object. It then displays the appropriate editor (based on each type) with the property name as the label.
The next lesson lists supported editor types and explains how to replace a data form’s default editors.