Skip to content

Instantly share code, notes, and snippets.

@BrianJVarley
Created January 21, 2016 22:38
Show Gist options
  • Save BrianJVarley/1ddddd8483706637e9ed to your computer and use it in GitHub Desktop.
Save BrianJVarley/1ddddd8483706637e9ed to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using ParkingTagPicker.Resources;
using ParkingTagPicker.ViewModels;
using ParkingTagPicker.Interfaces;
namespace ParkingTagPicker
{
public partial class MainPage : PhoneApplicationPage, INavigationCallback
{
// Constructor
public MainPage()
{
InitializeComponent();
// Set the data context of the LongListSelector control to the sample data
DataContext = App.ViewModel;
App.ViewModel.NavigationCallback = this;
// Sample code to localize the ApplicationBar
//BuildLocalizedApplicationBar();
}
// Load data for the ViewModel Items
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (!App.ViewModel.IsDataLoaded)
{
App.ViewModel.LoadCouncilNamesData();
}
}
void INavigationCallback.NavigateTo(string ItemID)
{
NavigationService.Navigate(new Uri("/DetailsPage.xaml?selectedItem=" + ItemID, UriKind.Relative));
}
// Sample code for building a localized ApplicationBar
//private void BuildLocalizedApplicationBar()
//{
// // Set the page's ApplicationBar to a new instance of ApplicationBar.
// ApplicationBar = new ApplicationBar();
// // Create a new button and set the text value to the localized string from AppResources.
// ApplicationBarIconButton appBarButton = new ApplicationBarIconButton(new Uri("/Assets/AppBar/appbar.add.rest.png", UriKind.Relative));
// appBarButton.Text = AppResources.AppBarButtonText;
// ApplicationBar.Buttons.Add(appBarButton);
// // Create a new menu item with the localized string from AppResources.
// ApplicationBarMenuItem appBarMenuItem = new ApplicationBarMenuItem(AppResources.AppBarMenuItemText);
// ApplicationBar.MenuItems.Add(appBarMenuItem);
//}
}
}
<phone:PhoneApplicationPage x:Class="ParkingTagPicker.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
Orientation="Portrait"
SupportedOrientations="Portrait"
d:DataContext="{d:DesignData SampleData/MainViewModelSampleData.xaml}"
shell:SystemTray.IsVisible="True"
mc:Ignorable="d">
<!-- Data context is set to sample data above and LayoutRoot contains the root grid where all other page content is placed -->
<Grid x:Name="LayoutRoot" Background="#FF236A93">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<!--
LOCALIZATION NOTE:
To localize the displayed strings copy their values to appropriately named
keys in the app's neutral language resource file (AppResources.resx) then
replace the hard-coded text value between the attributes' quotation marks
with the binding clause whose path points to that string name.
For example:
Text="{Binding Path=LocalizedResources.ApplicationTitle, Source={StaticResource LocalizedStrings}}"
This binding points to the template's string resource named "ApplicationTitle".
Adding supported languages in the Project Properties tab will create a
new resx file per language that can carry the translated values of your
UI strings. The binding in these examples will cause the value of the
attributes to be drawn from the .resx file that matches the
CurrentUICulture of the app at run time.
-->
<!-- TitlePanel contains the name of the application and page title -->
<StackPanel Grid.Row="0" Margin="12,17,0,28">
<TextBlock Style="{StaticResource PhoneTextNormalStyle}" Text="{Binding Path=LocalizedResources.ApplicationTitle, Source={StaticResource LocalizedStrings}}" />
<TextBlock Margin="9,-7,0,0"
Style="{StaticResource PhoneTextTitle1Style}"
Text="{Binding Path=LocalizedResources.ApplicationMainPageName,
Source={StaticResource LocalizedStrings}}" />
</StackPanel>
<!-- ContentPanel contains LongListSelector and LongListSelector ItemTemplate. Place additional content here -->
<Grid x:Name="ContentPanel"
Grid.Row="1"
Margin="12,0,12,0">
<phone:LongListSelector x:Name="MainLongListSelector"
Margin="0,0,-12,0"
ItemsSource="{Binding Items}"
SelectedItem="{Binding SelectedCouncilAcronymItem}">
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,17">
<TextBlock Style="{StaticResource PhoneTextExtraLargeStyle}"
Text="{Binding CouncilAcronym}"
TextWrapping="Wrap" />
<TextBlock Margin="12,-6,12,0"
Style="{StaticResource PhoneTextSubtleStyle}"
Text="{Binding CouncilFullName}"
TextWrapping="Wrap" />
</StackPanel>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
</Grid>
<!--
Uncomment to see an alignment grid to help ensure your controls are
aligned on common boundaries. The image has a top margin of -32px to
account for the System Tray. Set this to 0 (or remove the margin altogether)
if the System Tray is hidden.
Before shipping remove this XAML and the image itself.
-->
<!--<Image Source="/Assets/AlignmentGrid.png" VerticalAlignment="Top" Height="800" Width="480" Margin="0,-32,0,0" Grid.Row="0" Grid.RowSpan="2" IsHitTestVisible="False" />-->
</Grid>
</phone:PhoneApplicationPage>
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using ParkingTagPicker.Resources;
using ParkingTagPicker.DAL;
using ParkingTagPicker.Models;
using ParkingTagPicker.ExtensionMethods;
using System.Windows.Input;
using System.Threading.Tasks;
using GalaSoft.MvvmLight.Command;
using ParkingTagPicker.Interfaces;
namespace ParkingTagPicker.ViewModels
{
public class MainViewModel : ViewModelBase, INotifyPropertyChanged
{
//Dependency Injection private instances
private IRepository<Zone> _zoneDataService = null;
private INavigationCallback _navCallBack = null;
public MainViewModel(IRepository<Zone> zoneDataService)
{
this.Items = new ObservableCollection<ItemViewModel>();
this._zoneDataService = zoneDataService;
Initialization = RefreshDataAsync();
}
/// <summary>
/// Creates and adds a few ItemViewModel objects into the Items collection.
/// </summary>
public void LoadCouncilNamesData()
{
//Load Council Names
this.Items.Add(new ItemViewModel() { ID = "0", CouncilAcronym = "DCC", CouncilFullName = "Dublin City Council" });
this.Items.Add(new ItemViewModel() { ID = "1", CouncilAcronym = "DLR", CouncilFullName = "Dún Laoghaire-Rathdown County Council"});
this.Items.Add(new ItemViewModel() { ID = "2", CouncilAcronym = "FCC", CouncilFullName = "Fingal County Council"});
this.Items.Add(new ItemViewModel() { ID = "3", CouncilAcronym = "SDC", CouncilFullName = "South Dublin County Council"});
this.Items.Add(new ItemViewModel() { ID = "4", CouncilAcronym = "ATC", CouncilFullName = "Arklow Town Council"});
this.Items.Add(new ItemViewModel() { ID = "5", CouncilAcronym = "DLH", CouncilFullName = "Dún Laoghaire Harbour Company" });
this.Items.Add(new ItemViewModel() { ID = "6", CouncilAcronym = "WTC", CouncilFullName = "Wicklow Town Council"});
this.Items.Add(new ItemViewModel() { ID = "7", CouncilAcronym = "TS", CouncilFullName = "Tallaght Stadium" });
this.Items.Add(new ItemViewModel() { ID = "8", CouncilAcronym = "GS", CouncilFullName = "Greystones" });
this.IsDataLoaded = true;
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
public ObservableCollection<ItemViewModel> Items { get; private set; }
public Task Initialization { get; private set; }
public bool IsDataLoaded { get; private set; }
private ObservableCollection<Zone> _zones;
public override ObservableCollection<Models.Zone> Zones
{
get
{
return _zones;
}
set
{
if (value != _zones)
{
_zones = value;
NotifyPropertyChanged("Zones");
}
}
}
private string _regNumber;
public override string RegNumber
{
get
{
return _regNumber;
}
set
{
if (value != _regNumber)
{
_regNumber = value;
NotifyPropertyChanged("RegNumber");
}
}
}
private Zone _selectedZone;
public override Models.Zone SelectedZone
{
get
{
return _selectedZone;
}
set
{
if (value != _selectedZone)
{
_selectedZone = value;
NotifyPropertyChanged("SelectedZone");
}
}
}
private ItemViewModel _selectedCouncilItem;
public ItemViewModel SelectedCouncilItem
{
get
{
return _selectedCouncilItem;
}
set
{
_selectedCouncilItem = value;
if (_selectedCouncilItem != null)
{
_navCallBack.NavigateTo(_selectedCouncilItem.ID);
}
NotifyPropertyChanged("SelectedCouncilAcronymItem");
}
}
public INavigationCallback NavigationCallback
{
get { return _navCallBack; }
set { _navCallBack = value; }
}
private string _selectedParkDuration;
public override string SelectedParkDuration
{
get
{
return _selectedParkDuration;
}
set
{
if (value != _selectedParkDuration)
{
_selectedParkDuration = value;
NotifyPropertyChanged("SelectedParkDuration");
}
}
}
private bool _isButtonEnabled;
public override bool IsButtonEnabled
{
get
{
return _isButtonEnabled;
}
set
{
if (value != _isButtonEnabled)
{
_isButtonEnabled = value;
NotifyPropertyChanged("IsButtonEnabled");
}
}
}
private RelayCommand sendParkinTagCommand;
public RelayCommand SendParkinTagCommand
{
get
{
if (sendParkinTagCommand == null)
{
sendParkinTagCommand = new RelayCommand( new Action (() =>
{
//Call SendParkingTagSMSRequest
SendParkingTagSMSRequest();
}));
}
return sendParkinTagCommand;
}
}
public async override System.Threading.Tasks.Task RefreshDataAsync()
{
var queryResult = await _zoneDataService.GetAllAsync(_selectedCouncilItem.CouncilFullName);
Zones = queryResult;
}
public async override System.Threading.Tasks.Task SendParkingTagSMSRequest()
{
await SMSTaskExtensions.SendParkingTagSMSRequest(SelectedZone, RegNumber, SelectedParkDuration);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment