[XAM311] Solution using Page written in C# rather than XAML

CheesebaronCheesebaron DKInsider, University mod

Just if someone is interested in the solution for the exercises written in C# rather than in XAML:

AllContacts.cs

public class AllContacts : ContentPage
{
    bool isEditing;
    ToolbarItem editButton, endEditButton;

    public AllContacts ()
    {
        var listView = new ListView ();

        var sorted = from person in SimpsonFactory.Characters
            orderby person.Name
            group person by person.Name.Substring(0, 1) into personGroup
            select new Grouping<string, Person>(personGroup.Key, personGroup);

        var observableSorted = new ObservableCollection<Grouping<string, Person>>(sorted);

        listView.ItemTemplate = new DataTemplate (typeof(SimpsonsCell));
        listView.RowHeight = 80;
        listView.IsGroupingEnabled = true;
        listView.GroupDisplayBinding = listView.GroupShortNameBinding = new Binding ("Key");
        listView.ItemsSource = observableSorted;
        listView.ItemTapped += async (sender, e) => {
            var person = (Person)e.Item;
            if (person == null)
                return;

            if (isEditing && 
                await DisplayAlert("Confirm", "Are you sure you want to delete " + person.Name, "Yes", "No"))
            {
                var groups = observableSorted.ToList();
                foreach(var group in groups)
                {
                    if (group.Contains(person))
                    {
                        group.Remove(person);
                        if (group.Count == 0)
                            observableSorted.Remove(group);
                    }
                }
                OnEndEdit(null, EventArgs.Empty);
                return;
            }

            await Navigation.PushAsync(new ContactDetails(person));
        };

        Content = listView;

        editButton = new ToolbarItem { Text = "Edit" };
        editButton.Clicked += OnEdit;

        endEditButton = new ToolbarItem { Text = "End Edit" };
        endEditButton.Clicked += OnEndEdit;
        OnEndEdit(null, EventArgs.Empty);
    }

    void OnEdit(object sender, EventArgs e)
    {
        isEditing = true;
        ToolbarItems.Remove(editButton);
        ToolbarItems.Add(endEditButton);
    }

    void OnEndEdit(object sender, EventArgs e)
    {
        isEditing = false;
        ToolbarItems.Remove(endEditButton);
        ToolbarItems.Add(editButton);
    }
}

SimpsonsCell.cs

public class SimpsonsCell : ViewCell
{
    public SimpsonsCell ()
    {
        var grid = new Grid ();
        grid.ColumnDefinitions = new ColumnDefinitionCollection {
            new ColumnDefinition { Width = 75 },
            new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) },
            new ColumnDefinition { Width = GridLength.Auto },
            new ColumnDefinition { Width = 5 },
        };

        var image = new Image {
            Aspect = Aspect.AspectFit,
            WidthRequest = 75,
            HeightRequest = 75,
        };
        image.SetBinding(Image.SourceProperty, new Binding("HeadshotUrl", BindingMode.OneWay, new ImageResourceConverter()));

        var stackLayout = new StackLayout {
            Orientation = StackOrientation.Vertical,
            VerticalOptions = LayoutOptions.Center
        };

        var name = new Label {
            FontSize = Device.GetNamedSize (NamedSize.Medium, typeof(Label)),
            FontAttributes = FontAttributes.Italic,
            TextColor = Color.Black
        };
        name.SetBinding (Label.TextProperty, "Name");

        var email = new Label { 
            FontSize = Device.GetNamedSize (NamedSize.Small, typeof(Label)), 
            TextColor = Color.Accent 
        };
        email.SetBinding (Label.TextProperty, "Email");

        stackLayout.Children.Add (name);
        stackLayout.Children.Add (email);

        var favorite = new Image {
            Aspect = Aspect.AspectFit,
            VerticalOptions = LayoutOptions.Center,
            HorizontalOptions = LayoutOptions.End,
            Source = "star.png"
        };

        favorite.SetBinding (VisualElement.IsVisibleProperty, "IsFavorite");

        var birthday = new Label { 
            FontSize = Device.GetNamedSize (NamedSize.Micro, typeof(Label)), 
            TextColor = Color.Accent,
            VerticalOptions = LayoutOptions.End,
            HorizontalOptions = LayoutOptions.End,
        };
        birthday.SetBinding (Label.TextProperty, new Binding ("Dob", BindingMode.OneWay, new BirthdayConverter (), null, "{0} yrs. old"));

        grid.Children.Add (image, 0, 0);
        grid.Children.Add (stackLayout, 1, 0);
        grid.Children.Add (favorite, 2, 0);
        grid.Children.Add (birthday, 1, 0);
        Grid.SetColumnSpan (birthday, 2);

        View = grid;
    }
}

Grouping.cs

public class Grouping<K, T> : ObservableCollection<T>
{
    public K Key { get; private set; }

    public Grouping(K key, IEnumerable<T> items)
    {
        Key = key;
        foreach (var item in items)
            Items.Add(item);
    }
}

Posts

  • MarkSmith.8123MarkSmith.8123 USXamarin Team, University, XamUProfessors Xamurai

    Very nice @Cheesebaron!

    I always tend to default to XAML when building labs just because most people want to see that approach (or use it), it's great to see alternatives - thanks for sharing!

    mark

Sign In or Register to comment.