Getting all objects from another object based on it's id - c#

I have 2 classes
Terms
public class Terms
{
[PrimaryKey, AutoIncrement]
public int TermId { get; set; }
public string TermName { get; set; }
public string StartDate { get; set; }
public string EndDate { get; set; }
public DateTime RealStart => DateTime.Parse(StartDate);
public DateTime RealEnd => DateTime.Parse(EndDate);
}
Courses
public class Courses
{
[PrimaryKey, AutoIncrement]
public int CourseId { get; set; }
public int TermId { get; set; }
public string CourseName { get; set; }
public string StartDate { get; set; }
public string EndDate { get; set; }
public DateTime RealStart => DateTime.Parse(StartDate);
public DateTime RealEnd => DateTime.Parse(EndDate);
public string CIName { get; set; }
public string CIEmail { get; set; }
public string CIPhone { get; set; }
public string Status { get; set; }
public bool Notifications { get; set; }
public string Notes { get; set; }
}
Term 1 -
Course 1
Course 2
When selecting a course from Term 1, I'm trying to display the expanded course details for that course.
xaml
<CollectionView x:Name="CourseCollection"
ItemsSource="{Binding Courses}"
EmptyView="No Courses to display"
SelectionMode="Single"
SelectionChanged="CourseCollection_SelectionChanged">
C#
private async void CourseCollection_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var course = (Courses)e.CurrentSelection.FirstOrDefault();
if (e.CurrentSelection != null)
{
await Navigation.PushAsync(new CoursesDetail(course));
}
}
It's displaying both courses
CoursesDetail
xaml
<CollectionView x:Name="CoursesCollection"
ItemsSource="{Binding Courses}"
EmptyView="No courses to view"
SelectionMode="Single">
<CollectionView.ItemTemplate>
<DataTemplate>
<StackLayout>
<Grid Padding="5" RowSpacing="1" ColumnSpacing="10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" FontSize="Medium" VerticalTextAlignment="Center">Course ID</Label>
<Label Text="{Binding CourseId}" Grid.Row="0" Grid.Column="1" FontSize="Medium" x:Name="CourseId" ></Label>
<Label Grid.Row="1" Grid.Column="0" FontSize="Medium" VerticalTextAlignment="Center">Term ID</Label>
<Label Text="{Binding TermId}" Grid.Row="1" Grid.Column="1" FontSize="Medium" x:Name="TermId"></Label>
<Label Grid.Row="2" Grid.Column="0" FontSize="Medium" VerticalTextAlignment="Center">Course Name</Label>
<Label x:Name="CourseName" Grid.Row="2" Grid.Column="1" FontSize="Medium" VerticalTextAlignment="Center" Text="{Binding CourseName}"></Label>
<Label Grid.Row="3" Grid.Column="0" FontSize="Medium" VerticalTextAlignment="Center">Start Date</Label>
<Label Grid.Row="3" Grid.Column="1" FontSize="Medium" x:Name="StartDate" Text="{Binding StartDate}"></Label>
<Label Grid.Row="4" Grid.Column="0" FontSize="Medium" VerticalTextAlignment="Center">End Date</Label>
<Label Grid.Row="4" Grid.Column="1" FontSize="Medium" x:Name="EndDate" Text="{Binding EndDate}"></Label>
<Label Grid.Row="5" Grid.Column="0" FontSize="Medium" VerticalTextAlignment="Center">Instructor Name</Label>
<Label x:Name="CIName" Grid.Row="5" Grid.Column="1" FontSize="Medium" VerticalTextAlignment="Center" Text="{Binding CIName}"></Label>
<Label Grid.Row="6" Grid.Column="0" FontSize="Medium" VerticalTextAlignment="Center">Instructor Email</Label>
<Label x:Name="CIEmail" Grid.Row="6" Grid.Column="1" FontSize="Medium" VerticalTextAlignment="Center" Text="{Binding CIEmail}"></Label>
<Label Grid.Row="7" Grid.Column="0" FontSize="Medium" VerticalTextAlignment="Center">Instructor Phone</Label>
<Label x:Name="CIPhone" Grid.Row="7" Grid.Column="1" FontSize="Medium" VerticalTextAlignment="Center" Text="{Binding CIPhone}"></Label>
<Label Grid.Row="8" Grid.Column="0" FontSize="Medium" VerticalTextAlignment="Center">Course Status</Label>
<Label Grid.Row="8" Grid.Column="1" x:Name="Status" FontSize="Medium" Text="{Binding Status}"></Label>
<Label Grid.Row="9" Grid.Column="0" FontSize="Medium" VerticalTextAlignment="Center">Notes</Label>
<Label x:Name="Notes" Grid.Row="9" Grid.Column="1" FontSize="Medium" Text="{Binding Notes}"></Label>
<Label Grid.Row="10" Grid.Column="0" FontSize="Medium" VerticalTextAlignment="Center">Notifications</Label>
<Label x:Name="Notifications" Grid.Row="10" Grid.Column="1" FontSize="Medium" Text="{Binding Notifications}"></Label>
</Grid>
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
c#
public partial class CoursesDetail : ContentPage
{
private readonly int _selectedCourseId;
private Courses theBiggestCourses;
protected override async void OnAppearing()
{
base.OnAppearing();
CoursesCollection.ItemsSource = await database.GetCourses(_selectedCourseId);
AssessmentsCollection.ItemsSource = await database.GetAssessments(_selectedCourseId);
}
public CoursesDetail()
{
InitializeComponent();
}
public CoursesDetail(Courses courses)
{
_selectedCourseId = courses.CourseId;
InitializeComponent();
this.theBiggestCourses = courses;
}
I've tried manually passing through each text field through the constructor. I have sample data that should auto populate and thought that it was the issue, but I removed those methods and tried manually added a Term, Courses for that Term but as soon as it gets to more than 1 course, it wacks out.
EDIT - My pea sized brain theory could be that since Terms is getting passed through as an object, capturing all the elements of that object, that it's still holding onto that Term object, so when calling the Courses object, it's capturing all the courses objects for that terms object. I hope I explained that decent.
EDIT2 - GET COURSES
public static async Task<IEnumerable<Courses>> GetCourses(int termId)
{
await Init();
var courses = await db.Table<Courses>()
.Where(i => i.TermId == termId).ToListAsync();
return courses;
}
public static async Task<IEnumerable<Courses>> GetCourses()
{
await Init();
var courses = await db.Table<Courses>().ToListAsync();
return courses;
}
These are the two for GetCourses. Is it because we're passing through an int termId here and an object in the method through the constructor? Then it defaults to the unparameterized constructor, which is going to return all of them?

GetCourses expects a TermID
public static async Task<IEnumerable<Courses>> GetCourses(int termId)
{
await Init();
var courses = await db.Table<Courses>()
.Where(i => i.TermId == termId).ToListAsync();
return courses;
}
but when you call it you are passing a CourseID
CoursesCollection.ItemsSource = await database.GetCourses(_selectedCourseId);

Related

Xamarin forms - change page content depending on button clicked

I currently have a page where I display some objects in a List view retrieved. I am using DataTemplate to display all of them because the list will grow over time. Now, for each of the object, I want to be able to display a page with some information about each of them. So, architecture will be the same for each page, but the content will be different (Labels, Title) How can I make this happen ?
Currently, each of the objects lead to the same page. I just need to display the content for each one
My object contains the following properties:
public int NewVersionId { get; set; }
public string HtmlFileName { get; set; }
public string FeatureName { get; set; }
public string Date { get; set; }
public string Description { get; set; }
public bool IsNew { get; set; }
<ui:ExtendedListView
x:Name="ListOfVersions"
Grid.Row="3"
HasUnevenRows="True"
BackgroundColor="Transparent"
ItemsSource="{Binding Path=Results}"
RowHeight="135">
<ui:ExtendedListView.ItemTemplate>
<DataTemplate x:DataType="version:NewVersionItem">
<ui:ExtendedViewCell
SelectedBackgroundColor="Transparent"
ShowSeparator="True"
Command="{Binding Path=BindingContext.ShowVersionSettingsPage, Source={Reference ListOfVersions}}"
CommandParameter="{Binding .}" >
<ui:ExtendedGrid Margin="15,20">
<ui:ExtendedGrid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</ui:ExtendedGrid.RowDefinitions>
<ui:ExtendedStackLayout
Grid.Row="0"
Orientation="Horizontal">
<ui:ExtendedLabel
x:Name="FeatureName"
HorizontalOptions="Start"
WidthRequest="300"
MaxLines="2"
Text="{Binding FeatureName}"
TextColor="{DynamicResource ForegroundColor}"
FontSize="16"
FontAttributes="Bold" />
<Frame
HorizontalOptions="Center"
WidthRequest="60"
Padding="0,0,0,0"
Margin="0,0,30,0"
CornerRadius="10"
BackgroundColor="#6AC66A"
IsVisible="{Binding IsNew}">
<ui:ExtendedLabel
Text="new"
HorizontalOptions="Center"
VerticalOptions="Center" />
</Frame>
<Image HorizontalOptions="End"
VerticalOptions="Center"
WidthRequest="{DynamicResource NavigationArrowSize}">
<Image.Source>
<FontImageSource FontFamily="GenFont"
Glyph="{controls:Icon ArrowRight}"
Size="{DynamicResource NavigationArrowSize}"
Color="{DynamicResource ForegroundColorSecondary}" />
</Image.Source>
</Image>
</ui:ExtendedStackLayout>
<ui:ExtendedLabel
Grid.Row="1"
Text="{Binding Date}"
TextColor="{DynamicResource ForegroundColor}"
FontSize="14" />
<ui:ExtendedLabel
Grid.Row="2"
Margin="0,0,0,0"
Text="{Binding Description}"
TextColor="{DynamicResource ForegroundColor}"
FontSize="14"
MaxLines="2" />
</ui:ExtendedGrid>
</ui:ExtendedViewCell>
</DataTemplate>
</ui:ExtendedListView.ItemTemplate>
</ui:ExtendedListView>
private async void OnShowVersionSettingPageExecute()
{
try
{
await NavigationService.NavigateAsync(Pages.WhatsNewFeaturePage.Id);
}
catch (Exception e)
{
Xamarin.Forms.Application.Current.Trace("An error has occurred while navigating", e, this);
}
}

Is it possible to get the group key of the Selected Item in a CollectionView with IsGrouped=true and a CollectionView.GroupHeaderTemplate in C#

I am using a collectionview in a xamarin.Forms app. I want to identify the Group Key of a SelectedItem.
The items in each group are not unique. Item can appear in multiple groups. Perhaps I could use SelectionChangedCommand and specify the CommandParameter as the label.text in the GroupHeaderTemplate?
It is possible.
Based on Xamarin.Forms - CollectionView, we could use SelectionChanged method to get the curren selected item. Then we can loop which group contain this item, therefore the property of Group also will get.
We will modifl VerticalListEmptyGroupsPage.Xaml code as follows:
<StackLayout Margin="20">
<CollectionView ItemsSource="{Binding Animals}"
SelectionChanged="CollectionView_SelectionChanged"
SelectionMode="Single"
IsGrouped="true">
<CollectionView.ItemTemplate>
<DataTemplate>
<Grid Padding="10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Image Grid.RowSpan="2"
Source="{Binding ImageUrl}"
Aspect="AspectFill"
HeightRequest="60"
WidthRequest="60" />
<Label Grid.Column="1"
Text="{Binding Name}"
FontAttributes="Bold" />
<Label Grid.Row="1"
Grid.Column="1"
Text="{Binding Location}"
FontAttributes="Italic"
VerticalOptions="End" />
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
<CollectionView.GroupHeaderTemplate>
<DataTemplate>
<Label Text="{Binding Name}"
BackgroundColor="LightGray"
FontSize="Large"
FontAttributes="Bold" />
</DataTemplate>
</CollectionView.GroupHeaderTemplate>
<CollectionView.GroupFooterTemplate>
<DataTemplate>
<Label Text="{Binding Count, StringFormat='Total animals: {0:D}'}"
Margin="0,0,0,10" />
</DataTemplate>
</CollectionView.GroupFooterTemplate>
</CollectionView>
</StackLayout>
And VerticalListEmptyGroupsPage.xaml.cs:
public partial class VerticalListEmptyGroupsPage : ContentPage
{
static GroupedAnimalsViewModel groupedAnimals;
public VerticalListEmptyGroupsPage()
{
InitializeComponent();
groupedAnimals= new GroupedAnimalsViewModel(true);
BindingContext = groupedAnimals;
}
private void CollectionView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Animal selectedAnimal = e.CurrentSelection[0] as Animal;
foreach (var animals in groupedAnimals.Animals)
{
foreach(var animal in animals)
{
if(animal == selectedAnimal)
{
Console.WriteLine(animals.Name);
DisplayAlert("Group Name", animals.Name, "OK");
}
}
}
}
}
The effect:
===============================Update====================================
If there are multi groups contain the same Item, I think the best way is to design the model of item with containing the Group key.
For example, the Animal model could be designed as follows:
public class Animal
{
public string GroupKey { set; get; }
public string Name { get; set; }
public string Location { get; set; }
public string Details { get; set; }
public string ImageUrl { get; set; }
public override string ToString()
{
return Name;
}
}
This way is similar with the foreign key of database.

C# Xamarin Forms Items not set in a Grouped ListView

I am trying to set the ItemsSource of a ListView from a nested ObservableCollection List.
While HeaderGroups are being created, Items in the groups are empty. Any idea?
Here is my code:
Classes:
public class TableDataGroup
{
public TableDataGroup(String imagepath, String championship, String tip)//, Color headergroupbgcolor,)
{
this.ImagePath = imagepath;
this.Championship = championship;
this.Tip = tip;
this.Items = new ObservableCollection<TableDataItem>();
}
public string ImagePath { get; private set; }
public string Championship { get; private set; }
public string Tip { get; private set; }
public ObservableCollection<TableDataItem> Items { get; set; }
}
public class TableDataItem
{
public TableDataItem(String imagepath, Color backgroundTipColour, String time, String teamone...)
{
this.ImagePath = imagepath;
this.BackgroundTipColour = backgroundTipColour;
this.Time = time;
this.TeamOne = teamone;
//...
}
public string ImagePath { get; private set; }
public Color BackgroundTipColour { get; private set; }
public string Time { get; private set; }
public string TeamOne { get; private set; }
//...
}
Calls:
mainPageLoadViewModel.tableDataGroup = new ObservableCollection<TableDataGroup>();
for (int i = 0; i < 10; i++)
{
TableDataGroup tableDataGroup = new TableDataGroup("value", "value", "value","TIP");
for (int x = 0; x < 10; x++)
{
tableDataGroup.Items.Add(new TableDataItem("flag",
backgroundtipcolor,
time,
teamone,
//...));
}
mainPageLoadViewModel.tableDataGroup.Add(tableDataGroup);
}
ItemsCollectionView.ItemsSource = mainPageLoadViewModel.tableDataGroup; ===> ObservableCollection<TableDataGroup>
xaml:
<ListView x:Name="ItemsCollectionView" ItemsSource="{Binding TableDataGroup}"
IsGroupingEnabled="true" HasUnevenRows="True">
<ListView.GroupHeaderTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<StackLayout BackgroundColor="DarkSlateGray" Orientation="Horizontal">
<StackLayout Padding="2" VerticalOptions="CenterAndExpand">
<Image Source="{Binding ImagePath}" HeightRequest="15" WidthRequest="15"/>
</StackLayout>
<StackLayout Padding="2" VerticalOptions="CenterAndExpand">
<Label Text="{Binding Championship}" FontSize="15" TextColor="White"/>
</StackLayout>
<StackLayout Margin="0,0,10,0" VerticalOptions="CenterAndExpand" HorizontalOptions="EndAndExpand">
<Label Text="TIP" FontSize="15" TextColor="White"/>
</StackLayout>
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.GroupHeaderTemplate>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<Grid Padding="10,5,5,10" BackgroundColor="Gray">
<Grid.RowDefinitions>
<RowDefinition Height="25" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="35"/>
<ColumnDefinition Width="120"/>
<ColumnDefinition Width="25"/>
<ColumnDefinition Width="25"/>
<ColumnDefinition Width="120"/>
<ColumnDefinition Width="45"/>
</Grid.ColumnDefinitions>
<Label Text="{Binding Time}" Grid.Column="0" FontSize="13" TextColor="Black" VerticalTextAlignment="Center" HorizontalOptions="StartAndExpand"></Label>
<Label Text="{Binding TeamOne}" FontSize="13" TextColor="Black" Grid.Column="1" VerticalTextAlignment="Center" HorizontalOptions="End"></Label>
<Label Text="{Binding ScoreTeamOne}" FontSize="13" Grid.Column="2" BackgroundColor="Gray" TextColor="White" VerticalTextAlignment="Center" HorizontalTextAlignment="Center" WidthRequest="25" MinimumWidthRequest="25"></Label>
<Label Text="{Binding ScoreTeamTwo}" FontSize="13" Grid.Column="3" BackgroundColor="Gray" TextColor="White" VerticalTextAlignment="Center" HorizontalTextAlignment="Center" WidthRequest="25" MinimumWidthRequest="25"></Label>
<Label Text="{Binding TeamTwo}" FontSize="13" TextColor="Black" Grid.Column="4" VerticalTextAlignment="Center"></Label>
<Label Text="{Binding Tip}" FontSize="13" TextColor="White" Grid.Column="5" BackgroundColor="DarkSlateGray" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" HorizontalOptions="Fill"></Label>
</Grid>
</ViewCell.View>
</ViewCell>
<!--<TextCell Text="{Binding Name}" Detail="{Binding Description}"></TextCell>-->
</DataTemplate>
</ListView.ItemTemplate>
</ListView>

Collection was modified; enumeration operation may not execute. Xamarin Forms

I have a list view in Xamarin Froms Project as :
<ListView x:Name="ExerciseList" HasUnevenRows="False" SeparatorVisibility="None" RowHeight="200">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Vertical">
<StackLayout Orientation="Horizontal" HorizontalOptions="CenterAndExpand">
<Entry Text="{Binding ExerciseName}" HorizontalTextAlignment="Center" Focused="ExerciseName_Focused" HorizontalOptions="CenterAndExpand">
<Entry.GestureRecognizers>
<TapGestureRecognizer Tapped="ExerciseNameGestureRecognizer_Tapped"/>
</Entry.GestureRecognizers>
</Entry>
<Image IsVisible="{Binding GreenVisible}" Source="smallgreenadd.png"/>
<Image IsVisible="{Binding RedVisible}" Source="smallredremove.png"/>
</StackLayout>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<Label Text="Sets : " Grid.Column="0" Grid.Row="0" HorizontalTextAlignment="Center" HorizontalOptions="CenterAndExpand" />
<Label Text="Weights : " Grid.Column="0" Grid.Row="1" HorizontalTextAlignment="Center" HorizontalOptions="CenterAndExpand" />
<Label Text="Reps: " Grid.Column="0" Grid.Row="2" HorizontalTextAlignment="Center" HorizontalOptions="CenterAndExpand" />
<Entry Text="{Binding Sets}" Grid.Column="1" Grid.Row="0" HorizontalTextAlignment="Center" HorizontalOptions="CenterAndExpand" />
<Entry Text="{Binding Weights}" Grid.Column="1" Grid.Row="1" HorizontalTextAlignment="Center" HorizontalOptions="CenterAndExpand" />
<Entry Text="{Binding Reps}" Grid.Column="1" Grid.Row="2" HorizontalTextAlignment="Center" HorizontalOptions="CenterAndExpand" />
</Grid>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
This is attached to a View Modal called ExerciseViewModal. This is:
public class ExerciseViewModal : BaseViewModal
{
private List<AddExerciseModal> _addExerciseModals;
public List<AddExerciseModal> AddExerciseModals
{
get { return _addExerciseModals; }
set
{
_addExerciseModals = value;
OnPropertyChanged("AddExerciseModals");
}
}
public ExerciseViewModal()
{
_addExerciseModals = new List<AddExerciseModal>();
if (AddExerciseModals.Count == 0)
{
for (int i = 0; i < 7; i++)
{
AddExerciseModal addExerciseModal = new AddExerciseModal
{
ExerciseID = i,
ExerciseName = "Excercise " + i,
GreenVisible = false,
RedVisible = true,
Sets = "2",
Reps = "10",
Weights = "10"
};
AddExerciseModals.Add(addExerciseModal);
}
AddExerciseModals[AddExerciseModals.Count - 1].GreenVisible = true;
AddExerciseModals[AddExerciseModals.Count - 1].RedVisible = false;
}
}
}
AddExerciseModal class :
public class AddExerciseModal
{
public int ExerciseID { get; set; }
public string ExerciseName { get; set; }
public string Weights { get; set; }
public string Reps { get; set; }
public string Sets { get; set; }
public bool GreenVisible { get; set; }
public bool RedVisible { get; set; }
}
Whenever I try to change the sets/reps/Weights property inside the ListView I always get an error saying:
"Collection was modified; enumeration operation may not execute."
How can I solve this?
Can you change your List to ObservableCollection and try it .That will resolve the problem.

Xamarin local variable in xaml.cs and printing through the XAML file

I want to be able to print properties from 'CurrentOrder' in the XAML.
Here is what I have so far:
// OrderPage.xaml.cs
public partial class OrderPage : ContentPage
{
private Order _currentOrder;
public Order CurrentOrder
{
get { return _currentOrder; }
}
public OrderPage()
{
InitializeComponent();
_currentOrder = Order.DefaultOrder;
addPin("Start", _currentOrder.PickupAddress.Latitude, _currentOrder.PickupAddress.Longitude);
addPin("End", _currentOrder.DropoffAddress.Latitude, _currentOrder.DropoffAddress.Longitude);
this.BindingContext = this;
}
public OrderPage(Order order)
{
InitializeComponent();
_currentOrder = order;
addPin("Start", _currentOrder.PickupAddress.Latitude, _currentOrder.PickupAddress.Longitude);
addPin("End", _currentOrder.DropoffAddress.Latitude, _currentOrder.DropoffAddress.Longitude);
Debug.WriteLine(_currentOrder.ToString());
this.BindingContext = this;
}
}
Here is the Order class, which has several properties with other classes.
public class Order : INotifyPropertyChanged
{
public static Order DefaultOrder
{
// I have a default order return here, but in the sake of privacy, I'm removing my test addresses
}
// event to handle changes in the order status
public event PropertyChangedEventHandler PropertyChanged;
public enum Status { Preview, NeedsDriver, WaitingDriver, InTransit, NeedsSignature, Complete, Refunded }
public string ID { get; set; }
public string Description { get; set; }
private Status _orderStatus;
public Status OrderStatus {
get
{
return _orderStatus;
}
set
{
_orderStatus = value;
// tell the view that the order status has changed
OnPropertyChanged("OrderStatus");
}
}
public Contact PickupContact { get; set; }
public Contact DropoffContact { get; set; }
public Address PickupAddress { get; set; }
public Address DropoffAddress { get; set; }
public DateTime PickupTime { get; set; }
public DateTime DropoffTime { get; set; }
// Formatted Pickup and Dropoff Times
public string PickupTimeFormatted
{
get { return PickupTime.ToString("g"); }
}
public string DropoffTimeFormatted
{
get { return DropoffTime.ToString("g"); }
}
public Order()
{
}
// Handler to tell the view that the order status has changed
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
public override string ToString()
{
return string.Format("[Order: ID={0}, Description={1}, OrderStatus={2}, PickupContact={3}, DropoffContact={4}, PickupAddress={5}, DropoffAddress={6}, PickupTime={7}, DropoffTime={8}, PickupTimeFormatted={9}, DropoffTimeFormatted={10}]", ID, Description, OrderStatus, PickupContact, DropoffContact, PickupAddress, DropoffAddress, PickupTime, DropoffTime, PickupTimeFormatted, DropoffTimeFormatted);
}
}
Finally, the XAML.
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:maps="clr-namespace:Xamarin.Forms.Maps;assembly=Xamarin.Forms.Maps"
x:Class="Divco.OrderPage"
Title="Order">
<ContentPage.BindingContext>
</ContentPage.BindingContext>
<ContentPage.Content>
<StackLayout Spacing="10" x:Name="layout" VerticalOptions="FillAndExpand">
<StackLayout>
<maps:Map WidthRequest="320"
HeightRequest="150"
x:Name="MyMap"
IsShowingUser="false"
MapType="Street" />
</StackLayout>
<StackLayout Padding="20, 20, 20, 0">
<!--<Label Content="{Binding ID, Source={StaticResource CurrentOrder}}"></Label>-->
<Label Text="{Binding ID}"
TextColor="Fuchsia" />
<Label Text="Description"
LineBreakMode="WordWrap" />
</StackLayout>
<StackLayout Padding="20, 20, 20, 0">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Label Text="Pickup"
Grid.Row="0"
Grid.Column="0"
Grid.ColumnSpan="2"
TextColor="Fuchsia"/>
<Label Text="Top Left"
Grid.Row="1"
Grid.Column="0" />
<Label Text="Top Right"
Grid.Row="1"
Grid.Column="1" />
<Label Text="Dropoff"
Grid.Row="2"
Grid.Column="0"
Grid.ColumnSpan="2"
TextColor="Fuchsia"/>
<Label Text="Bottom Left"
Grid.Row="3"
Grid.Column="0" />
<Label Text="Bottom Right"
Grid.Row="3"
Grid.Column="1" />
</Grid>
</StackLayout>
<StackLayout Padding="20, 20, 20, 20" VerticalOptions="EndAndExpand">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Button Text="Call X"
BackgroundColor="Fuschia"
TextColor="White"
Grid.Row="0"
Grid.Column="0"/>
<Button Text="Navigate!"
BackgroundColor="Fuschia"
TextColor="White"
Grid.Row="0"
Grid.Column="1"
Grid.ColumnSpan="2"/>
</Grid>
</StackLayout>
</StackLayout>
</ContentPage.Content>
You can see where I attempted to print the ID of the order in the XAML. All of the supporting classes for the Order have ToString(s) which return the needed information for the order page, so I'm not really worried about printing '_currentOrder.PickupAddress.Address1', for example.
your BindingContext is a reference to the current Page
this.BindingContext = this;
so your binding path would look like:
<Label Text="{Binding CurrentOrder.ID}" TextColor="Fuchsia" />

Categories