Skip to content

Instantly share code, notes, and snippets.

@jgable
Created March 23, 2011 22:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgable/884190 to your computer and use it in GitHub Desktop.
Save jgable/884190 to your computer and use it in GitHub Desktop.
PivotVM's and MainPage Pivot View for Hackatopia Demo
using System;
using System.Windows;
using System.Windows.Input;
using JSONPhoneApp1.Common;
using JSONPhoneApp1.Service;
public class HelloWorldVM : ViewModelBase
{
private HelloWorldService _helloServ = new HelloWorldService();
private string message;
public string Message
{
get { return message; }
set
{
if (message != value)
{
message = value;
OnPropertyChanged("Message");
}
}
}
private DateTime serverTime;
public DateTime ServerTime
{
get { return serverTime; }
set
{
if (serverTime != value)
{
serverTime = value;
OnPropertyChanged("ServerTime");
}
}
}
private bool success;
public bool Success
{
get { return success; }
set
{
if (success != value)
{
success = value;
OnPropertyChanged("Success");
}
}
}
public ICommand HelloCmd { get; private set; }
public HelloWorldVM()
{
HelloCmd = new DelegateCommand<object>(x => HandleHelloCommand());
}
private void HandleHelloCommand()
{
_helloServ.GetHello(
resp =>
{
this.Success = resp.Success;
this.Message = resp.Message;
this.ServerTime = resp.Time;
},
err =>
{
MessageBox.Show("Not cool, bro", "Error'd", MessageBoxButton.OK);
});
}
}
<Grid
x:Name="LayoutRoot"
Background="Transparent">
<!--Pivot Control -->
<!-- xmlns:controls="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls" -->
<controls:Pivot
x:Name="pivot"
Title="HACKATOPIA DEMO">
<!-- Hello World Service -->
<controls:PivotItem
DataContext="{Binding HelloWorld}"
Header="hello">
<Grid>
<TextBlock
HorizontalAlignment="Left"
VerticalAlignment="Top"
Style="{StaticResource PhoneTextSubtleStyle}"
Text="{Binding ServerTime}" />
<TextBlock
HorizontalAlignment="Center"
VerticalAlignment="Center"
Style="{StaticResource PhoneTextLargeStyle}"
Text="{Binding Message}" />
<Button
VerticalAlignment="Bottom"
Content="Say Hello"
cmd:SingleEventCommand.RoutedEventName="Click"
cmd:SingleEventCommand.TheCommandToRun="{Binding HelloCmd}" />
</Grid>
</controls:PivotItem>
<!-- Team List -->
<controls:PivotItem
DataContext="{Binding TeamList}"
Header="list">
<Grid>
<ListBox
x:Name="TeamList"
SelectionChanged="TeamList_SelectionChanged"
ItemsSource="{Binding Results}"
Margin="0 0 0 80">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock
Style="{StaticResource PhoneTextLargeStyle}"
Text="{Binding Name}" />
<TextBlock
Margin="15 5 10 10"
Style="{StaticResource PhoneTextSmallStyle}"
TextWrapping="Wrap"
Text="{Binding Description}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button
Content="Load Teams"
VerticalAlignment="Bottom"
cmd:SingleEventCommand.TheCommandToRun="{Binding Retrieve}"
cmd:SingleEventCommand.RoutedEventName="Click" />
</Grid>
</controls:PivotItem>
<!-- Team Id -->
<controls:PivotItem
x:Name="teamPivot"
DataContext="{Binding TeamInfo}"
Header="team">
<Grid>
<StackPanel>
<TextBlock
Text="{Binding Name}"
Style="{StaticResource PhoneTextLargeStyle}" />
<TextBlock
Margin="15 5 10 10"
Style="{StaticResource PhoneTextSmallStyle}"
TextWrapping="Wrap"
Text="{Binding Description}" />
<TextBlock
Text="MEMBERS"
FontWeight="Bold"
Margin="0 10 0 5"></TextBlock>
<ItemsControl
ItemsSource="{Binding Members}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock
Text="{Binding}"
Style="{StaticResource PhoneTextSubtleStyle}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</Grid>
</controls:PivotItem>
<!-- Search -->
<controls:PivotItem
DataContext="{Binding TeamSearch}"
Header="search">
<Grid>
<TextBox
x:Name="searchTxt"
VerticalAlignment="Top"
HorizontalAlignment="Stretch"
Margin="0 0 160 0"></TextBox>
<Button
Content="Search"
Width="150"
VerticalAlignment="Top"
HorizontalAlignment="Right"
cmd:SingleEventCommand.TheCommandToRun="{Binding Search}"
cmd:SingleEventCommand.CommandParameter="{Binding ElementName=searchTxt, Path=Text}"
cmd:SingleEventCommand.RoutedEventName="Click" />
<ListBox
x:Name="SearchList"
SelectionChanged="TeamList_SelectionChanged"
ItemsSource="{Binding Results}"
Margin="0 75 0 0">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock
Style="{StaticResource PhoneTextLargeStyle}"
Text="{Binding Name}" />
<TextBlock
Margin="15 5 10 10"
Style="{StaticResource PhoneTextSmallStyle}"
TextWrapping="Wrap"
Text="{Binding Description}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</controls:PivotItem>
</controls:Pivot>
</Grid>
using JSONPhoneApp1.Common;
using JSONPhoneApp1.Service;
using JSONPhoneApp1.ViewModel;
using Microsoft.Phone.Controls;
public partial class MainPage : PhoneApplicationPage
{
private MainPageVM ViewModel;
// Constructor
public MainPage()
{
InitializeComponent();
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
ViewModel = new MainPageVM();
this.DataContext = ViewModel;
}
private void TeamList_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
if (e.AddedItems == null || e.AddedItems.Count < 1)
return;
var selected = e.AddedItems[0] as Team;
pivot.SelectedItem = teamPivot;
ViewModel.TeamInfo.GetInfo.Execute(new SCommandArgs(null, e, selected.Id));
}
}
using JSONPhoneApp1.Common;
public class MainPageVM : ViewModelBase
{
public HelloWorldVM HelloWorld { get; set; }
public TeamListVM TeamList { get; set; }
public TeamInfoVM TeamInfo { get; set; }
public TeamSearchVM TeamSearch { get; set; }
public MainPageVM()
{
HelloWorld = new HelloWorldVM();
TeamList = new TeamListVM();
TeamInfo = new TeamInfoVM();
TeamSearch = new TeamSearchVM();
}
}
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Input;
using JSONPhoneApp1.Common;
using JSONPhoneApp1.Service;
public class TeamListVM : ViewModelBase
{
private TeamListService _listServ = new TeamListService();
private ObservableCollection<Team> results;
public ObservableCollection<Team> Results
{
get { return results; }
set
{
if (results != value)
{
results = value;
OnPropertyChanged("Results");
}
}
}
public ICommand Retrieve { get; private set; }
public TeamListVM()
{
Retrieve = new DelegateCommand<object>(x => HandleRetrieve());
Results = new ObservableCollection<Team>();
}
private void HandleRetrieve()
{
Results.Clear();
_listServ.GetTeams(
resp =>
{
resp.Teams.ForEach(t => Results.Add(t));
},
err =>
{
MessageBox.Show("Not cool, bro", "Error'd", MessageBoxButton.OK);
});
}
}
public class TeamInfoVM : ViewModelBase
{
private TeamGetService _teamServ = new TeamGetService();
private string name;
public string Name
{
get { return name; }
set
{
if (name != value)
{
name = value;
OnPropertyChanged("Name");
}
}
}
private string description;
public string Description
{
get { return description; }
set
{
if (description != value)
{
description = value;
OnPropertyChanged("Description");
}
}
}
private string captain;
public string Captain
{
get { return captain; }
set
{
if (captain != value)
{
captain = value;
OnPropertyChanged("Captain");
}
}
}
private ObservableCollection<string> members;
public ObservableCollection<string> Members
{
get { return members; }
set
{
if (members != value)
{
members = value;
OnPropertyChanged("Members");
}
}
}
public ICommand GetInfo { get; private set; }
public TeamInfoVM()
{
GetInfo = new DelegateCommand<SCommandArgs>(args => HandleGetInfo(args.CommandParameter.ToString()));
Members = new ObservableCollection<string>();
}
private void HandleGetInfo(string id)
{
int teamId;
if(!Int32.TryParse(id, out teamId))
return;
_teamServ.GetTeam(teamId,
resp =>
{
if (resp == null || !resp.Success || resp.Team == null)
{
MessageBox.Show("Team Not Found", "Invalid", MessageBoxButton.OK);
return;
}
this.Name = resp.Team.Name;
this.Description = resp.Team.Description;
this.Captain = resp.Team.Captain;
this.Members.Clear();
resp.Team.MemberNames.ForEach(n => this.Members.Add(n));
},
err =>
{
MessageBox.Show("Not cool, bro", "Error'd", MessageBoxButton.OK);
});
}
}
public class TeamSearchVM : ViewModelBase
{
private TeamSearchService _searchServ = new TeamSearchService();
private ObservableCollection<Team> results;
public ObservableCollection<Team> Results
{
get { return results; }
set
{
if (results != value)
{
results = value;
OnPropertyChanged("Results");
}
}
}
public ICommand Search { get; private set; }
public TeamSearchVM()
{
Search = new DelegateCommand<SCommandArgs>(args => HandleSearch(args.CommandParameter.ToString()));
Results = new ObservableCollection<Team>();
}
private void HandleSearch(string term)
{
if (String.IsNullOrEmpty(term))
return;
Results.Clear();
_searchServ.SearchTeams(term,
resp =>
{
if (resp == null || !resp.Success || resp.Teams == null)
{
MessageBox.Show("Nothing found", "Empty", MessageBoxButton.OK);
return;
}
resp.Teams.ForEach(t => Results.Add(t));
},
err =>
{
MessageBox.Show("Not cool, bro", "Error'd", MessageBoxButton.OK);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment