In XamarinForms I have to develop a page which is displaying UserSettings. Therefore I created a SettingsPage which inherits from ContentPage. On this page I have a ListView which ItemSource is an ObservableCollection. All cells on this page must have an Label on the left side with an Entry underneath it. The right side of this Cell must display an Image or SwitchControl. That depends on the UserSetting it is displaying.
So far I created a SettingsPage and a custom Cell for the ItemTemplate of the ListView. In this code the needed Image is hardcoded, that also has to change in the future. What is the best way to achieve this? I am totally new to XamarinForms, so hints on the code so far are appreciated.
Thanks, HJS
public class SettingsPage : ContentPage
{
public SettingsPage ()
{
var userSettings = DemoData.UserSettings ();
var listView = new ListView { ItemsSource = userSettings };
listView.ItemTemplate = new DataTemplate (typeof(UserCell));
listView.RowHeight = 60;
Content = listView;
}
}
public class UserCell : ViewCell
{
public UserCell ()
{
var titleLabel = new Label ();
var titleBinding = new Binding ("Name");
titleLabel.SetBinding (Label.TextProperty, titleBinding);
var valueEntry = new Entry ();
var valueBinding = new Binding ("Value");
valueEntry.SetBinding (Entry.TextProperty, valueBinding);
var accessoryImage = new Image ();
accessoryImage.Source = "ic_action_new.png";
var stackLayout = new StackLayout ();
stackLayout.Children.Add (titleLabel);
stackLayout.Children.Add (valueEntry);
var grid = new Grid {
HorizontalOptions = LayoutOptions.StartAndExpand,
VerticalOptions = LayoutOptions.FillAndExpand,
ColumnDefinitions = {
new ColumnDefinition{ Width = 200 },
new ColumnDefinition{ Width = 50 }
}
};
grid.Children.Add (stackLayout, 0, 0);
Grid.SetColumnSpan (stackLayout, 1);
grid.Children.Add (accessoryImage, 1, 0);
View = grid;
}
}