Simple Xamarin Forms XAML / C# problem

  • Thread starter Thread starter john pp nn
  • Start date Start date
J

john pp nn

Guest
Hi folks,


I am doing a tutorial from a book Mastering Xamarin.Forms (Third edition).

I have pasted the code from the website but there is an error. It wont compile.


using System;
using System.Collections.Generic;
using System.Linq;
using TripLog.Models;
using Xamarin.Forms;

namespace TripLog.Views
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();

var items = new List<TripLogEntry>
{
new TripLogEntry
{
Title = "Washington Monument",
Notes = "Amazing!",
Rating = 3,
Date = new DateTime(2019, 2, 5),
Latitude = 38.8895,
Longitude = -77.0352
},
new TripLogEntry
{
Title = "Statue of Liberty",
Notes = "Inspiring!",
Rating = 4,
Date = new DateTime(2019, 4, 13),
Latitude = 40.6892,
Longitude = -74.0444
},
new TripLogEntry
{
Title = "Golden Gate Bridge",
Notes = "Foggy, but beautiful.",
Rating = 5,
Date = new DateTime(2019, 4, 26),
Latitude = 37.8268,
Longitude = -122.4798
}
};
// Here is the problem. trips is underlined in red. The file does NOT know what it is.
trips.ItemsSource = items;
}
}
}

Here is the corresponding XAML file:

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="Xamarin | Open-source mobile app platform for .NET" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="TripLog.Views.MainPage"
Title="TripLog">
<ContentPage.ToolbarItems>
<ToolbarItem Text="New" Clicked="New_Clicked" />
</ContentPage.ToolbarItems>
<ContentPage.Content>
<CollectionView x:Name="trips"
SelectionMode="Single"
SelectionChanged="Trips_SelectionChanged">
<CollectionView.ItemTemplate>
<DataTemplate>
<Grid Padding="10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="3*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Label Grid.RowSpan="2"
Text="{Binding Date, StringFormat='{0:MMM d}'}" />
<Label Grid.Column="1"
Text="{Binding Title}"
FontAttributes="Bold" />
<Label Grid.Column="1"
Grid.Row="1"
Text="{Binding Notes}" />
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</ContentPage.Content>
</ContentPage>>

I am assuming the the line in the c# file

trips.ItemsSource = items;

is referring to

<CollectionView x:Name="trips"


How can I get the C# file to see the object in the xaml file ??


Thanks,


John



jppnn

Continue reading...
 
Back
Top