From 1bccef5ee03ed45a4163238103ba487a05a629bf Mon Sep 17 00:00:00 2001 From: Gustavo Martin Date: Thu, 3 Jul 2014 07:38:56 +0200 Subject: [PATCH] SettingsPage working just with XAML --- .../WeatherInformation/App.xaml.cs | 13 --- .../WeatherInformation/SettingsPage.xaml | 96 +++++++++++----------- .../WeatherInformation/SettingsPage.xaml.cs | 39 +++++---- .../WeatherInformation/ViewModels/MainViewModel.cs | 23 ------ .../ViewModels/SettingsViewModel.cs | 46 +++++++---- 5 files changed, 105 insertions(+), 112 deletions(-) diff --git a/WindowsPhone/WeatherInformation/WeatherInformation/App.xaml.cs b/WindowsPhone/WeatherInformation/WeatherInformation/App.xaml.cs index 754820d..3d50baf 100644 --- a/WindowsPhone/WeatherInformation/WeatherInformation/App.xaml.cs +++ b/WindowsPhone/WeatherInformation/WeatherInformation/App.xaml.cs @@ -21,7 +21,6 @@ namespace WeatherInformation // public static String appForceCulture = "qps-PLOC"; public static String appForceCulture = "en"; private static MainViewModel viewModel = null; - private static SettingsViewModel _settingsViewModel = null; /// /// MainViewModel estático que usan las vistas con el que se van a enlazar. @@ -39,18 +38,6 @@ 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; - } - } - /// /// Proporcionar acceso sencillo al marco raíz de la aplicación telefónica. /// diff --git a/WindowsPhone/WeatherInformation/WeatherInformation/SettingsPage.xaml b/WindowsPhone/WeatherInformation/WeatherInformation/SettingsPage.xaml index 53c373e..ea65478 100644 --- a/WindowsPhone/WeatherInformation/WeatherInformation/SettingsPage.xaml +++ b/WindowsPhone/WeatherInformation/WeatherInformation/SettingsPage.xaml @@ -11,57 +11,57 @@ mc:Ignorable="d" SupportedOrientations="PortraitOrLandscape" Orientation="Portrait" shell:SystemTray.IsVisible="True"> - - - - - - - - - - - - - + + + + + + + + + + + + + - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + - - - - - - - - - - - - + + + + + + + + + + + + - - - - + + + + - - - + + + \ No newline at end of file diff --git a/WindowsPhone/WeatherInformation/WeatherInformation/SettingsPage.xaml.cs b/WindowsPhone/WeatherInformation/WeatherInformation/SettingsPage.xaml.cs index 743aa4f..bd08d8a 100644 --- a/WindowsPhone/WeatherInformation/WeatherInformation/SettingsPage.xaml.cs +++ b/WindowsPhone/WeatherInformation/WeatherInformation/SettingsPage.xaml.cs @@ -10,35 +10,46 @@ using Microsoft.Phone.Shell; using WeatherInformation.ViewModels; using System.IO.IsolatedStorage; using System.Collections; +using System.Text; 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; } - protected override void OnNavigatedTo(NavigationEventArgs e) + /// + /// Update temperature units selection when change. + /// + /// + /// + private void TemperatureUnitsSelection_SelectionChanged(object sender, SelectionChangedEventArgs e) { + ListPicker listPicker = sender as ListPicker; + + // TODO: with LINQ :( + int index = listPicker.SelectedIndex; + var item = listPicker.Items[index]; + listPicker.SelectedItem = item; } - private void TemperatureUnitsSelection_SelectionChanged(object sender, SelectionChangedEventArgs e) + /// + /// Update language selection when change. + /// + /// + /// + private void LanguageSelection_SelectionChanged(object sender, SelectionChangedEventArgs e) { - IList units = e.AddedItems; - if (units.Count > 0) - { - var selectedUnit = units.Cast().First(); - } + ListPicker listPicker = sender as ListPicker; + + // TODO: with LINQ :( + int index = listPicker.SelectedIndex; + var item = listPicker.Items[index]; + listPicker.SelectedItem = item; } } } \ No newline at end of file diff --git a/WindowsPhone/WeatherInformation/WeatherInformation/ViewModels/MainViewModel.cs b/WindowsPhone/WeatherInformation/WeatherInformation/ViewModels/MainViewModel.cs index 2b0e1b8..4bf9dee 100644 --- a/WindowsPhone/WeatherInformation/WeatherInformation/ViewModels/MainViewModel.cs +++ b/WindowsPhone/WeatherInformation/WeatherInformation/ViewModels/MainViewModel.cs @@ -23,9 +23,6 @@ namespace WeatherInformation.ViewModels /// public ObservableCollection ForecastItems { get; private set; } public ObservableCollection CurrentItems { get; private set; } - //public string ForecastHeader { get; private set; } - - private string _sampleProperty = "Sample Runtime Property Value"; private readonly ServiceParser _serviceParser; @@ -53,26 +50,6 @@ namespace WeatherInformation.ViewModels + "{\"dt\":1398513600,\"temp\":{\"day\":282.75,\"min\":280.61,\"max\":282.75,\"night\":280.61,\"eve\":281.75,\"morn\":281.96},\"pressure\":1007.4,\"humidity\":0,\"weather\":[{\"id\":500,\"main\":\"Rain\",\"description\":\"light rain\",\"icon\":\"10d\"}],\"speed\":9.18,\"deg\":198,\"clouds\":35,\"rain\":0.55}" + "]}"; - /// - /// Propiedad Sample MainViewModel; 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; diff --git a/WindowsPhone/WeatherInformation/WeatherInformation/ViewModels/SettingsViewModel.cs b/WindowsPhone/WeatherInformation/WeatherInformation/ViewModels/SettingsViewModel.cs index c960820..ef6d94c 100644 --- a/WindowsPhone/WeatherInformation/WeatherInformation/ViewModels/SettingsViewModel.cs +++ b/WindowsPhone/WeatherInformation/WeatherInformation/ViewModels/SettingsViewModel.cs @@ -1,4 +1,5 @@ -using System; +using Microsoft.Phone.Controls; +using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; @@ -6,11 +7,12 @@ using System.IO.IsolatedStorage; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Windows; using WeatherInformation.Resources; namespace WeatherInformation.ViewModels { - public class SettingsViewModel : INotifyPropertyChanged + public class SettingsViewModel { // Settings private IsolatedStorageSettings settings; @@ -20,7 +22,9 @@ namespace WeatherInformation.ViewModels private const string _temperatureUnitsSelectionSettingKeyName = "TemperatureUnitsSelection"; // The default value of ListPicker settings - private const int _listPickerSettingDefault = 0; + private const int _languageSelectionSettingDefault = 0; + private const int _temperatureUnitsSelectionSettinDefault = 0; + public SettingsViewModel() { @@ -41,7 +45,7 @@ namespace WeatherInformation.ViewModels { get { - return GetValueOrDefault(_languageSelectionSettingKeyName, _listPickerSettingDefault); + return GetValueOrDefault(_languageSelectionSettingKeyName, _languageSelectionSettingDefault); } set { @@ -59,7 +63,7 @@ namespace WeatherInformation.ViewModels { get { - return GetValueOrDefault(_temperatureUnitsSelectionSettingKeyName, _listPickerSettingDefault); + return GetValueOrDefault(_temperatureUnitsSelectionSettingKeyName, _temperatureUnitsSelectionSettinDefault); } set { @@ -82,6 +86,14 @@ namespace WeatherInformation.ViewModels { T value; + // You need to add a check to DesignerProperties.IsInDesignTool to that code since accessing + // IsolatedStorageSettings in Visual Studio or Expression Blend is invalid. + // see: http://stackoverflow.com/questions/7294461/unable-to-determine-application-identity-of-the-caller + if (System.ComponentModel.DesignerProperties.IsInDesignTool) + { + return defaultValue; + } + // If the key exists, retrieve the value. if (settings.Contains(Key)) { @@ -106,6 +118,14 @@ namespace WeatherInformation.ViewModels { bool valueChanged = false; + // You need to add a check to DesignerProperties.IsInDesignTool to that code since accessing + // IsolatedStorageSettings in Visual Studio or Expression Blend is invalid. + // see: http://stackoverflow.com/questions/7294461/unable-to-determine-application-identity-of-the-caller + if (System.ComponentModel.DesignerProperties.IsInDesignTool) + { + return false; + } + // If the key exists if (settings.Contains(Key)) { @@ -131,17 +151,15 @@ namespace WeatherInformation.ViewModels /// private void Save() { - settings.Save(); - } - - public event PropertyChangedEventHandler PropertyChanged; - private void NotifyPropertyChanged(String propertyName) - { - PropertyChangedEventHandler handler = PropertyChanged; - if (null != handler) + // You need to add a check to DesignerProperties.IsInDesignTool to that code since accessing + // IsolatedStorageSettings in Visual Studio or Expression Blend is invalid. + // see: http://stackoverflow.com/questions/7294461/unable-to-determine-application-identity-of-the-caller + if (System.ComponentModel.DesignerProperties.IsInDesignTool) { - handler(this, new PropertyChangedEventArgs(propertyName)); + return; } + + settings.Save(); } } } -- 2.1.4