Created
July 4, 2018 08:22
-
-
Save pfaucon/c01effa8eeea39112078fadaace7cea8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.ObjectModel; | |
using System.Diagnostics; | |
using System.Threading.Tasks; | |
using Xamarin.Forms; | |
using EntryTest.Models; | |
using EntryTest.Views; | |
using System.Globalization; | |
namespace EntryTest.ViewModels | |
{ | |
public class ItemsViewModel : BaseViewModel | |
{ | |
public ObservableCollection<Item> Items { get; set; } | |
public Command LoadItemsCommand { get; set; } | |
public ItemsViewModel() | |
{ | |
Title = "Browse"; | |
Items = new ObservableCollection<Item>(); | |
LoadItemsCommand = new Command(async () => await ExecuteLoadItemsCommand()); | |
MessagingCenter.Subscribe<NewItemPage, Item>(this, "AddItem", async (obj, item) => | |
{ | |
var _item = item as Item; | |
Items.Add(_item); | |
await DataStore.AddItemAsync(_item); | |
}); | |
} | |
async Task ExecuteLoadItemsCommand() | |
{ | |
if (IsBusy) | |
return; | |
IsBusy = true; | |
try | |
{ | |
Items.Clear(); | |
var items = await DataStore.GetItemsAsync(true); | |
foreach (var item in items) | |
{ | |
Items.Add(item); | |
} | |
} | |
catch (Exception ex) | |
{ | |
Debug.WriteLine(ex); | |
} | |
finally | |
{ | |
IsBusy = false; | |
} | |
} | |
private string amount = "0"; | |
private float bgFloat; | |
private static CultureInfo parseCulture = new CultureInfo("en-US"); | |
public string Amount | |
{ | |
get | |
{ | |
//if (StopPoint == null) return "0"; | |
//return StopPoint.NewAmount.ToString(); | |
return amount; | |
} | |
set | |
{ | |
var amt = value.Replace(",", "."); | |
if (amount == amt) return; | |
var parsed = float.TryParse(amt, NumberStyles.AllowDecimalPoint, parseCulture, out float val); | |
if (parsed) | |
{ | |
if (amount == amt) | |
{ | |
OnPropertyChanged(); | |
return; | |
} | |
amount = amt; | |
if (val == bgFloat) | |
{ | |
OnPropertyChanged(); | |
return; | |
} | |
bgFloat = val; | |
OnPropertyChanged(); | |
} | |
else | |
{ | |
amount = "0"; | |
bgFloat = 0; | |
OnPropertyChanged(); | |
return; | |
} | |
} | |
} | |
} | |
} | |
////////////////////////// | |
<?xml version="1.0" encoding="utf-8"?> | |
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="EntryTest.Views.ItemsPage" | |
Title="{Binding Title}" x:Name="BrowseItemsPage"> | |
<ContentPage.ToolbarItems> | |
<ToolbarItem Text="Add" Clicked="AddItem_Clicked"> | |
<ToolbarItem.Icon> | |
<OnPlatform x:TypeArguments="FileImageSource"> | |
<On Platform="UWP" Value="add.png" /> | |
</OnPlatform> | |
</ToolbarItem.Icon> | |
</ToolbarItem> | |
</ContentPage.ToolbarItems> | |
<ContentPage.Content> | |
<StackLayout> | |
<Label Text="type some numbers!" /> | |
<Entry Text="{Binding Amount}" WidthRequest="65" HorizontalTextAlignment="End" /> | |
<ListView x:Name="ItemsListView" ItemsSource="{Binding Items}" VerticalOptions="FillAndExpand" | |
HasUnevenRows="true" RefreshCommand="{Binding LoadItemsCommand}" IsPullToRefreshEnabled="true" | |
IsRefreshing="{Binding IsBusy, Mode=OneWay}" CachingStrategy="RecycleElement" ItemSelected="OnItemSelected"> | |
<ListView.ItemTemplate> | |
<DataTemplate> | |
<ViewCell> | |
<StackLayout Padding="10"> | |
<Label Text="{Binding Text}" LineBreakMode="NoWrap" Style="{DynamicResource ListItemTextStyle}" | |
FontSize="16" /> | |
<Label Text="{Binding Description}" LineBreakMode="NoWrap" Style="{DynamicResource ListItemDetailTextStyle}" | |
FontSize="13" /> | |
</StackLayout> | |
</ViewCell> | |
</DataTemplate> | |
</ListView.ItemTemplate> | |
</ListView> | |
</StackLayout> | |
</ContentPage.Content> | |
</ContentPage> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment