From dab461d08307c044f1f15a80edd46c92540a3c10 Mon Sep 17 00:00:00 2001 From: Gustavo Martin Date: Wed, 18 Jun 2014 07:01:02 +0200 Subject: [PATCH] Working on WP8 WeatherInformation --- .../WeatherInformation/App.xaml.cs | 22 ++----- .../WeatherInformation/MainPage.xaml.cs | 8 +-- .../WeatherInformation/MapPage.xaml.cs | 11 ++-- .../SampleData/SettingsViewModelSampleData.xaml | 14 ++-- .../TemperatureUnitsViewModelSampleData.xaml | 12 ---- .../WeatherInformation/SettingsPage.xaml | 20 ++++-- .../WeatherInformation/SettingsPage.xaml.cs | 6 ++ .../WeatherInformation/ViewModels/ItemViewModel.cs | 10 +-- .../WeatherInformation/ViewModels/MainViewModel.cs | 2 +- .../ViewModels/SettingsViewModel.cs | 49 ++++---------- .../ViewModels/TemperatureUnitsViewModel.cs | 76 ---------------------- .../WeatherInformation/WeatherInformation.csproj | 6 +- 12 files changed, 61 insertions(+), 175 deletions(-) delete mode 100644 WindowsPhone/WeatherInformation/WeatherInformation/SampleData/TemperatureUnitsViewModelSampleData.xaml delete mode 100644 WindowsPhone/WeatherInformation/WeatherInformation/ViewModels/TemperatureUnitsViewModel.cs diff --git a/WindowsPhone/WeatherInformation/WeatherInformation/App.xaml.cs b/WindowsPhone/WeatherInformation/WeatherInformation/App.xaml.cs index 6081f2a..7b65355 100644 --- a/WindowsPhone/WeatherInformation/WeatherInformation/App.xaml.cs +++ b/WindowsPhone/WeatherInformation/WeatherInformation/App.xaml.cs @@ -15,13 +15,12 @@ namespace WeatherInformation { 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. + /// MainViewModel estático que usan las vistas con el que se van a enlazar. /// /// Objeto MainViewModel. - public static MainViewModel ViewModel + public static MainViewModel MainViewModel { get { @@ -45,19 +44,6 @@ namespace WeatherInformation } } - - 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. /// @@ -113,9 +99,9 @@ namespace WeatherInformation private void Application_Activated(object sender, ActivatedEventArgs e) { // Asegurarse de que el estado de la aplicación se restaura adecuadamente - if (!App.ViewModel.IsDataLoaded) + if (!App.MainViewModel.IsDataLoaded) { - App.ViewModel.LoadData(); + App.MainViewModel.LoadData(); } } diff --git a/WindowsPhone/WeatherInformation/WeatherInformation/MainPage.xaml.cs b/WindowsPhone/WeatherInformation/WeatherInformation/MainPage.xaml.cs index d07378d..e695bc7 100644 --- a/WindowsPhone/WeatherInformation/WeatherInformation/MainPage.xaml.cs +++ b/WindowsPhone/WeatherInformation/WeatherInformation/MainPage.xaml.cs @@ -19,18 +19,18 @@ namespace WeatherInformation InitializeComponent(); // Establecer el contexto de datos del control ListBox control en los datos de ejemplo - DataContext = App.ViewModel; + DataContext = App.MainViewModel; // Código de ejemplo para traducir ApplicationBar //BuildLocalizedApplicationBar(); } - // Cargar datos para los elementos ViewModel + // Cargar datos para los elementos MainViewModel protected override void OnNavigatedTo(NavigationEventArgs e) { - if (!App.ViewModel.IsDataLoaded) + if (!App.MainViewModel.IsDataLoaded) { - App.ViewModel.LoadData(); + App.MainViewModel.LoadData(); } } diff --git a/WindowsPhone/WeatherInformation/WeatherInformation/MapPage.xaml.cs b/WindowsPhone/WeatherInformation/WeatherInformation/MapPage.xaml.cs index 0dde759..3d37029 100644 --- a/WindowsPhone/WeatherInformation/WeatherInformation/MapPage.xaml.cs +++ b/WindowsPhone/WeatherInformation/WeatherInformation/MapPage.xaml.cs @@ -44,18 +44,17 @@ namespace WeatherInformation IsolatedStorageSettings.ApplicationSettings.Save(); } - this.GetLocation(); - } - - private async void GetLocation() - { - if ((bool)IsolatedStorageSettings.ApplicationSettings["LocationConsent"] != true) { // The user has opted out of Location. return; } + this.GetLocation(); + } + + private async void GetLocation() + { Geolocator geolocator = new Geolocator(); geolocator.DesiredAccuracyInMeters = 50; diff --git a/WindowsPhone/WeatherInformation/WeatherInformation/SampleData/SettingsViewModelSampleData.xaml b/WindowsPhone/WeatherInformation/WeatherInformation/SampleData/SettingsViewModelSampleData.xaml index 19b5b85..9e6fa96 100644 --- a/WindowsPhone/WeatherInformation/WeatherInformation/SampleData/SettingsViewModelSampleData.xaml +++ b/WindowsPhone/WeatherInformation/WeatherInformation/SampleData/SettingsViewModelSampleData.xaml @@ -1,12 +1,14 @@  + xmlns:vm="clr-namespace:WeatherInformation.ViewModels"> - - - - + + + + + + + \ No newline at end of file diff --git a/WindowsPhone/WeatherInformation/WeatherInformation/SampleData/TemperatureUnitsViewModelSampleData.xaml b/WindowsPhone/WeatherInformation/WeatherInformation/SampleData/TemperatureUnitsViewModelSampleData.xaml deleted file mode 100644 index 31a02cc..0000000 --- a/WindowsPhone/WeatherInformation/WeatherInformation/SampleData/TemperatureUnitsViewModelSampleData.xaml +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/WindowsPhone/WeatherInformation/WeatherInformation/SettingsPage.xaml b/WindowsPhone/WeatherInformation/WeatherInformation/SettingsPage.xaml index f75296e..599185a 100644 --- a/WindowsPhone/WeatherInformation/WeatherInformation/SettingsPage.xaml +++ b/WindowsPhone/WeatherInformation/WeatherInformation/SettingsPage.xaml @@ -14,6 +14,18 @@ Foreground="{StaticResource PhoneForegroundBrush}" SupportedOrientations="PortraitOrLandscape" Orientation="Portrait" shell:SystemTray.IsVisible="True"> + + + + + + + + + + + + @@ -31,16 +43,12 @@ - - - + - - - + diff --git a/WindowsPhone/WeatherInformation/WeatherInformation/SettingsPage.xaml.cs b/WindowsPhone/WeatherInformation/WeatherInformation/SettingsPage.xaml.cs index df7243f..61a8cb7 100644 --- a/WindowsPhone/WeatherInformation/WeatherInformation/SettingsPage.xaml.cs +++ b/WindowsPhone/WeatherInformation/WeatherInformation/SettingsPage.xaml.cs @@ -8,15 +8,21 @@ using System.Windows.Navigation; using Microsoft.Phone.Controls; using Microsoft.Phone.Shell; using WeatherInformation.ViewModels; +using System.IO.IsolatedStorage; namespace WeatherInformation { public partial class SettingsPage : PhoneApplicationPage { + // Our settings + private IsolatedStorageSettings _settings; + public SettingsPage() { InitializeComponent(); + _settings = IsolatedStorageSettings.ApplicationSettings; + // Establecer el contexto de datos del control ListBox control en los datos de ejemplo DataContext = App.SettingsViewModel; } diff --git a/WindowsPhone/WeatherInformation/WeatherInformation/ViewModels/ItemViewModel.cs b/WindowsPhone/WeatherInformation/WeatherInformation/ViewModels/ItemViewModel.cs index 7988bde..6b4349c 100644 --- a/WindowsPhone/WeatherInformation/WeatherInformation/ViewModels/ItemViewModel.cs +++ b/WindowsPhone/WeatherInformation/WeatherInformation/ViewModels/ItemViewModel.cs @@ -14,7 +14,7 @@ namespace WeatherInformation.ViewModels { private string _lineOne; /// - /// Propiedad Sample ViewModel; esta propiedad se usa en la vista para mostrar su valor mediante un enlace. + /// Propiedad Sample MainViewModel; esta propiedad se usa en la vista para mostrar su valor mediante un enlace. /// /// public string LineOne @@ -35,7 +35,7 @@ namespace WeatherInformation.ViewModels private string _lineTwo; /// - /// Propiedad Sample ViewModel; esta propiedad se usa en la vista para mostrar su valor mediante un enlace. + /// Propiedad Sample MainViewModel; esta propiedad se usa en la vista para mostrar su valor mediante un enlace. /// /// public string LineTwo @@ -56,7 +56,7 @@ namespace WeatherInformation.ViewModels private string _lineThree; /// - /// Propiedad Sample ViewModel; esta propiedad se usa en la vista para mostrar su valor mediante un enlace. + /// Propiedad Sample MainViewModel; esta propiedad se usa en la vista para mostrar su valor mediante un enlace. /// /// public string LineThree @@ -77,7 +77,7 @@ namespace WeatherInformation.ViewModels private string _lineFour; /// - /// Propiedad Sample ViewModel; esta propiedad se usa en la vista para mostrar su valor mediante un enlace. + /// Propiedad Sample MainViewModel; esta propiedad se usa en la vista para mostrar su valor mediante un enlace. /// /// public string LineFour @@ -98,7 +98,7 @@ namespace WeatherInformation.ViewModels private string _lineFive; /// - /// Propiedad Sample ViewModel; esta propiedad se usa en la vista para mostrar su valor mediante un enlace. + /// Propiedad Sample MainViewModel; esta propiedad se usa en la vista para mostrar su valor mediante un enlace. /// /// public string LineFive diff --git a/WindowsPhone/WeatherInformation/WeatherInformation/ViewModels/MainViewModel.cs b/WindowsPhone/WeatherInformation/WeatherInformation/ViewModels/MainViewModel.cs index 3deea79..d801a70 100644 --- a/WindowsPhone/WeatherInformation/WeatherInformation/ViewModels/MainViewModel.cs +++ b/WindowsPhone/WeatherInformation/WeatherInformation/ViewModels/MainViewModel.cs @@ -54,7 +54,7 @@ namespace WeatherInformation.ViewModels + "]}"; /// - /// Propiedad Sample ViewModel; esta propiedad se usa en la vista para mostrar su valor mediante un enlace + /// Propiedad Sample MainViewModel; esta propiedad se usa en la vista para mostrar su valor mediante un enlace /// /// public string SampleProperty diff --git a/WindowsPhone/WeatherInformation/WeatherInformation/ViewModels/SettingsViewModel.cs b/WindowsPhone/WeatherInformation/WeatherInformation/ViewModels/SettingsViewModel.cs index 9a185ea..6e82502 100644 --- a/WindowsPhone/WeatherInformation/WeatherInformation/ViewModels/SettingsViewModel.cs +++ b/WindowsPhone/WeatherInformation/WeatherInformation/ViewModels/SettingsViewModel.cs @@ -12,31 +12,12 @@ namespace WeatherInformation.ViewModels { public SettingsViewModel() { - this.SettingsItems = new ObservableCollection(); + this.TemperatureUnitsSelection = new List(); + this.LanguageSelection = new List(); } - private string _sampleProperty = "Sample Runtime Property Value"; - - public ObservableCollection SettingsItems { 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 List TemperatureUnitsSelection { get; private set; } + public List LanguageSelection { get; private set; } public bool IsDataLoaded { @@ -45,22 +26,18 @@ namespace WeatherInformation.ViewModels } /// - /// Crear y agregar unos pocos objetos ItemViewModel a la colección Items. + /// Crear y agregar unos pocos objetos 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" - }); - + // TODO: How to do the same using StaticResources? :/ What the translator should do with this :( + // There must be some way to refernce static resources from here or something like that, otherwise + // the translator is going to complain A LOT! + TemperatureUnitsSelection.Add("fahrenheit"); + TemperatureUnitsSelection.Add("centigrade"); + + LanguageSelection.Add("english"); + LanguageSelection.Add("spanish"); this.IsDataLoaded = true; } diff --git a/WindowsPhone/WeatherInformation/WeatherInformation/ViewModels/TemperatureUnitsViewModel.cs b/WindowsPhone/WeatherInformation/WeatherInformation/ViewModels/TemperatureUnitsViewModel.cs deleted file mode 100644 index f316cbf..0000000 --- a/WindowsPhone/WeatherInformation/WeatherInformation/ViewModels/TemperatureUnitsViewModel.cs +++ /dev/null @@ -1,76 +0,0 @@ -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 e5f2f97..0b14d44 100644 --- a/WindowsPhone/WeatherInformation/WeatherInformation/WeatherInformation.csproj +++ b/WindowsPhone/WeatherInformation/WeatherInformation/WeatherInformation.csproj @@ -25,6 +25,7 @@ true 11.0 true + 12.0.41212.0 true @@ -127,7 +128,6 @@ - @@ -151,10 +151,6 @@ MSBuild:Compile Designer - - MSBuild:Compile - Designer - Designer MSBuild:Compile -- 2.1.4