From: Gustavo Martin Date: Mon, 9 Jun 2014 06:24:14 +0000 (+0200) Subject: WeatherInformation: settings pages X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=3cfad8767dc38de60e027a119dfbf887e523f706;p=CSharpForFun%2F.git WeatherInformation: settings pages --- diff --git a/WindowsPhone/WeatherInformation/WeatherInformation/App.xaml.cs b/WindowsPhone/WeatherInformation/WeatherInformation/App.xaml.cs index fcf2361..6081f2a 100644 --- a/WindowsPhone/WeatherInformation/WeatherInformation/App.xaml.cs +++ b/WindowsPhone/WeatherInformation/WeatherInformation/App.xaml.cs @@ -14,6 +14,8 @@ namespace WeatherInformation public partial class App : Application { private static MainViewModel viewModel = null; + private static SettingsViewModel _settingsViewModel = null; + private static TemperatureUnitsViewModel _temperatureUnitsViewModel = null; /// /// ViewModel estático que usan las vistas con el que se van a enlazar. @@ -31,6 +33,31 @@ namespace WeatherInformation } } + public static SettingsViewModel SettingsViewModel + { + get + { + // Retrasar la creación del modelo de vista hasta que sea necesario + if (_settingsViewModel == null) + _settingsViewModel = new SettingsViewModel(); + + return _settingsViewModel; + } + } + + + public static TemperatureUnitsViewModel TemperatureUnitsViewModel + { + get + { + // Retrasar la creación del modelo de vista hasta que sea necesario + if (_temperatureUnitsViewModel == null) + _temperatureUnitsViewModel = new TemperatureUnitsViewModel(); + + return _temperatureUnitsViewModel; + } + } + /// /// Proporcionar acceso sencillo al marco raíz de la aplicación telefónica. /// diff --git a/WindowsPhone/WeatherInformation/WeatherInformation/SampleData/TemperatureUnitsViewModelSampleData.xaml b/WindowsPhone/WeatherInformation/WeatherInformation/SampleData/TemperatureUnitsViewModelSampleData.xaml new file mode 100644 index 0000000..31a02cc --- /dev/null +++ b/WindowsPhone/WeatherInformation/WeatherInformation/SampleData/TemperatureUnitsViewModelSampleData.xaml @@ -0,0 +1,12 @@ + + + + + + + + \ No newline at end of file diff --git a/WindowsPhone/WeatherInformation/WeatherInformation/SettingsPage.xaml b/WindowsPhone/WeatherInformation/WeatherInformation/SettingsPage.xaml index fa99662..0095553 100644 --- a/WindowsPhone/WeatherInformation/WeatherInformation/SettingsPage.xaml +++ b/WindowsPhone/WeatherInformation/WeatherInformation/SettingsPage.xaml @@ -16,11 +16,11 @@ - + - + - + @@ -33,12 +33,27 @@ - - --> + - + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/WindowsPhone/WeatherInformation/WeatherInformation/SettingsPage.xaml.cs b/WindowsPhone/WeatherInformation/WeatherInformation/SettingsPage.xaml.cs index 3a91202..194be6a 100644 --- a/WindowsPhone/WeatherInformation/WeatherInformation/SettingsPage.xaml.cs +++ b/WindowsPhone/WeatherInformation/WeatherInformation/SettingsPage.xaml.cs @@ -7,6 +7,7 @@ using System.Windows.Controls; using System.Windows.Navigation; using Microsoft.Phone.Controls; using Microsoft.Phone.Shell; +using WeatherInformation.ViewModels; namespace WeatherInformation { @@ -15,11 +16,31 @@ namespace WeatherInformation public SettingsPage() { InitializeComponent(); + + // Establecer el contexto de datos del control ListBox control en los datos de ejemplo + DataContext = App.SettingsViewModel; + } + + protected override void OnNavigatedTo(NavigationEventArgs e) + { + if (!App.SettingsViewModel.IsDataLoaded) + { + App.SettingsViewModel.LoadData(); + } } private void LongListSelector_SelectionChanged(object sender, SelectionChangedEventArgs e) { - object lol = this.SettingsList.SelectedItem; + LongListSelector selector = sender as LongListSelector; + ItemViewModel item = selector.SelectedItem as ItemViewModel; + switch(item.LineOne) + { + case "Temperature units": + NavigationService.Navigate(new Uri("/SettingsTemperatureUnitsPage.xaml", UriKind.Relative)); + break; + default: + break; + } } private void StackPanel_Tap(object sender, System.Windows.Input.GestureEventArgs e) diff --git a/WindowsPhone/WeatherInformation/WeatherInformation/SettingsTemperatureUnitsPage.xaml b/WindowsPhone/WeatherInformation/WeatherInformation/SettingsTemperatureUnitsPage.xaml new file mode 100644 index 0000000..9af9e90 --- /dev/null +++ b/WindowsPhone/WeatherInformation/WeatherInformation/SettingsTemperatureUnitsPage.xaml @@ -0,0 +1,38 @@ + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/WindowsPhone/WeatherInformation/WeatherInformation/SettingsTemperatureUnitsPage.xaml.cs b/WindowsPhone/WeatherInformation/WeatherInformation/SettingsTemperatureUnitsPage.xaml.cs new file mode 100644 index 0000000..fbb95d6 --- /dev/null +++ b/WindowsPhone/WeatherInformation/WeatherInformation/SettingsTemperatureUnitsPage.xaml.cs @@ -0,0 +1,31 @@ +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; + +namespace WeatherInformation +{ + public partial class SettingsTemperatureUnitsPage : PhoneApplicationPage + { + public SettingsTemperatureUnitsPage() + { + InitializeComponent(); + + // Establecer el contexto de datos del control ListBox control en los datos de ejemplo + DataContext = App.TemperatureUnitsViewModel; + } + + protected override void OnNavigatedTo(NavigationEventArgs e) + { + if (!App.TemperatureUnitsViewModel.IsDataLoaded) + { + App.TemperatureUnitsViewModel.LoadData(); + } + } + } +} \ No newline at end of file diff --git a/WindowsPhone/WeatherInformation/WeatherInformation/ViewModels/SettingsViewModel.cs b/WindowsPhone/WeatherInformation/WeatherInformation/ViewModels/SettingsViewModel.cs index c238a6f..9a185ea 100644 --- a/WindowsPhone/WeatherInformation/WeatherInformation/ViewModels/SettingsViewModel.cs +++ b/WindowsPhone/WeatherInformation/WeatherInformation/ViewModels/SettingsViewModel.cs @@ -8,7 +8,7 @@ using System.Threading.Tasks; namespace WeatherInformation.ViewModels { - class SettingsViewModel : INotifyPropertyChanged + public class SettingsViewModel : INotifyPropertyChanged { public SettingsViewModel() { @@ -37,8 +37,33 @@ namespace WeatherInformation.ViewModels } } } - + public bool IsDataLoaded + { + get; + private set; + } + + /// + /// Crear y agregar unos pocos objetos ItemViewModel a la colección Items. + /// + public void LoadData() + { + // TODO: How to do the same using StaticResources? :/ + this.SettingsItems.Add(new ItemViewModel() + { + LineOne = "Temperature units", + LineTwo = "fahrenheit" + }); + this.SettingsItems.Add(new ItemViewModel() + { + LineOne = "Language", + LineTwo = "spanish" + }); + + + this.IsDataLoaded = true; + } public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged(String propertyName) diff --git a/WindowsPhone/WeatherInformation/WeatherInformation/ViewModels/TemperatureUnitsViewModel.cs b/WindowsPhone/WeatherInformation/WeatherInformation/ViewModels/TemperatureUnitsViewModel.cs new file mode 100644 index 0000000..f316cbf --- /dev/null +++ b/WindowsPhone/WeatherInformation/WeatherInformation/ViewModels/TemperatureUnitsViewModel.cs @@ -0,0 +1,76 @@ +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WeatherInformation.ViewModels +{ + public class TemperatureUnitsViewModel : INotifyPropertyChanged + { + public TemperatureUnitsViewModel() + { + this.TemperatureUnitsItems = new ObservableCollection(); + } + + private string _sampleProperty = "Sample Runtime Property Value"; + + public ObservableCollection TemperatureUnitsItems { get; private set; } + /// + /// Propiedad Sample ViewModel; esta propiedad se usa en la vista para mostrar su valor mediante un enlace + /// + /// + public string SampleProperty + { + get + { + return _sampleProperty; + } + set + { + if (value != _sampleProperty) + { + _sampleProperty = value; + NotifyPropertyChanged("SampleProperty"); + } + } + } + + public bool IsDataLoaded + { + get; + private set; + } + + /// + /// Crear y agregar unos pocos objetos ItemViewModel a la colección Items. + /// + public void LoadData() + { + // TODO: How to do the same using StaticResources? :/ + this.TemperatureUnitsItems.Add(new ItemViewModel() + { + LineOne = "fahrenheit" + }); + this.TemperatureUnitsItems.Add(new ItemViewModel() + { + LineOne = "centigrade" + }); + + + this.IsDataLoaded = true; + } + + public event PropertyChangedEventHandler PropertyChanged; + private void NotifyPropertyChanged(String propertyName) + { + PropertyChangedEventHandler handler = PropertyChanged; + if (null != handler) + { + handler(this, new PropertyChangedEventArgs(propertyName)); + } + } + } +} diff --git a/WindowsPhone/WeatherInformation/WeatherInformation/WeatherInformation.csproj b/WindowsPhone/WeatherInformation/WeatherInformation/WeatherInformation.csproj index fbf314e..db4c845 100644 --- a/WindowsPhone/WeatherInformation/WeatherInformation/WeatherInformation.csproj +++ b/WindowsPhone/WeatherInformation/WeatherInformation/WeatherInformation.csproj @@ -125,8 +125,12 @@ SettingsPage.xaml + + SettingsTemperatureUnitsPage.xaml + + @@ -150,10 +154,18 @@ MSBuild:Compile Designer + + MSBuild:Compile + Designer + Designer MSBuild:Compile + + Designer + MSBuild:Compile +