SettingsPage working just with XAML
authorGustavo Martin <gu.martinm@gmail.com>
Thu, 3 Jul 2014 05:38:56 +0000 (07:38 +0200)
committerGustavo Martin <gu.martinm@gmail.com>
Thu, 3 Jul 2014 05:38:56 +0000 (07:38 +0200)
WindowsPhone/WeatherInformation/WeatherInformation/App.xaml.cs
WindowsPhone/WeatherInformation/WeatherInformation/SettingsPage.xaml
WindowsPhone/WeatherInformation/WeatherInformation/SettingsPage.xaml.cs
WindowsPhone/WeatherInformation/WeatherInformation/ViewModels/MainViewModel.cs
WindowsPhone/WeatherInformation/WeatherInformation/ViewModels/SettingsViewModel.cs

index 754820d..3d50baf 100644 (file)
@@ -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;
 
         /// <summary>
         /// 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;
-            }
-        }
-
         /// <summary>
         /// Proporcionar acceso sencillo al marco raíz de la aplicación telefónica.
         /// </summary>
index 53c373e..ea65478 100644 (file)
     mc:Ignorable="d"
     SupportedOrientations="PortraitOrLandscape"  Orientation="Portrait"
     shell:SystemTray.IsVisible="True">
-    
-       <phone:PhoneApplicationPage.Resources>
-               <ViewModels:SettingsViewModel x:Key="SettingsViewModelDataSource" d:IsDataSource="True"/>
-       </phone:PhoneApplicationPage.Resources>
-       <phone:PhoneApplicationPage.FontFamily>
-               <StaticResource ResourceKey="PhoneFontFamilyNormal"/>
-       </phone:PhoneApplicationPage.FontFamily>
-       <phone:PhoneApplicationPage.FontSize>
-               <StaticResource ResourceKey="PhoneFontSizeNormal"/>
-       </phone:PhoneApplicationPage.FontSize>
-       <phone:PhoneApplicationPage.Foreground>
-               <StaticResource ResourceKey="PhoneForegroundBrush"/>
-       </phone:PhoneApplicationPage.Foreground>
+
+    <phone:PhoneApplicationPage.Resources>
+        <ViewModels:SettingsViewModel x:Key="SettingsViewModelDataSource" d:IsDataSource="True"/>
+    </phone:PhoneApplicationPage.Resources>
+    <phone:PhoneApplicationPage.FontFamily>
+        <StaticResource ResourceKey="PhoneFontFamilyNormal"/>
+    </phone:PhoneApplicationPage.FontFamily>
+    <phone:PhoneApplicationPage.FontSize>
+        <StaticResource ResourceKey="PhoneFontSizeNormal"/>
+    </phone:PhoneApplicationPage.FontSize>
+    <phone:PhoneApplicationPage.Foreground>
+        <StaticResource ResourceKey="PhoneForegroundBrush"/>
+    </phone:PhoneApplicationPage.Foreground>
     <!--LayoutRoot es la cuadrícula raíz donde se coloca todo el contenido de la página-->
-       <Grid x:Name="LayoutRoot" Background="Transparent" DataContext="{Binding Source={StaticResource SettingsViewModelDataSource}}">
-               <!-- Pivot -->
-               <phone:Pivot Title="WEATHER INFORMATION">
-                       <!-- Elemento Pivot uno -->
-                       <phone:PivotItem Header="settings">
-                               <Grid VerticalAlignment="Top">
-                                       <Grid.RowDefinitions>
-                                               <RowDefinition Height="*" />
-                                               <RowDefinition Height="*" />
-                                       </Grid.RowDefinitions>
-                                       <Grid.ColumnDefinitions>
-                                               <ColumnDefinition Width="*" />
-                                       </Grid.ColumnDefinitions>
-                                       <StackPanel Grid.Column="0" Grid.Row="0" Orientation="Vertical" VerticalAlignment="Center">
-                                               <TextBox x:Name="TemperatureUnitsHeader" Text="{Binding LocalizedResources.SettingsTemperatureUnitsHeader, Mode=OneWay, Source={StaticResource LocalizedStrings}}" HorizontalAlignment="Left" Background="Transparent" BorderBrush="Transparent" Foreground="#FFA4A2A2"/>
-                        <toolkit:ListPicker x:Name="TemperatureUnitsSelection" SelectedIndex="{Binding TemperaruteUnitsSelectionSetting, Mode=TwoWay}">
+    <Grid x:Name="LayoutRoot" Background="Transparent" DataContext="{Binding Source={StaticResource SettingsViewModelDataSource}}">
+        <!-- Pivot -->
+        <phone:Pivot Title="WEATHER INFORMATION">
+            <!-- Elemento Pivot uno -->
+            <phone:PivotItem Header="settings">
+                <Grid VerticalAlignment="Top">
+                    <Grid.RowDefinitions>
+                        <RowDefinition Height="*" />
+                        <RowDefinition Height="*" />
+                    </Grid.RowDefinitions>
+                    <Grid.ColumnDefinitions>
+                        <ColumnDefinition Width="*" />
+                    </Grid.ColumnDefinitions>
+                    <StackPanel Grid.Column="0" Grid.Row="0" Orientation="Vertical" VerticalAlignment="Center">
+                        <TextBox x:Name="TemperatureUnitsHeader" Text="{Binding LocalizedResources.SettingsTemperatureUnitsHeader, Mode=OneWay, Source={StaticResource LocalizedStrings}}" HorizontalAlignment="Left" Background="Transparent" BorderBrush="Transparent" Foreground="#FFA4A2A2"/>
+                        <toolkit:ListPicker x:Name="TemperatureUnitsSelection" SelectedIndex="{Binding Source={StaticResource SettingsViewModelDataSource}, Path=TemperaruteUnitsSelectionSetting, Mode=TwoWay}" SelectionChanged="TemperatureUnitsSelection_SelectionChanged">
                             <toolkit:ListPickerItem Content="{Binding LocalizedResources.SettingsTemperatureUnitsSelectionFahrenheit, Mode=OneWay, Source={StaticResource LocalizedStrings}}" />
-                                                       <toolkit:ListPickerItem Content="{Binding LocalizedResources.SettingsTemperatureUnitsSelectionCentigrade, Mode=OneWay, Source={StaticResource LocalizedStrings}}" />
-                                               </toolkit:ListPicker>
-                                       </StackPanel>
-                                       <StackPanel Grid.Column="0" Grid.Row="1" Orientation="Vertical" VerticalAlignment="Center">
-                                               <TextBox x:Name="LanguageHeader" Text="{Binding LocalizedResources.SettingsLanguageHeader, Mode=OneWay, Source={StaticResource LocalizedStrings}}" HorizontalAlignment="Left" Background="Transparent" BorderBrush="Transparent" Foreground="#FFA4A2A2" />
-                                               <toolkit:ListPicker x:Name="LanguageSelection" SelectedIndex="{Binding LanguageSelectionSetting, Mode=TwoWay}">
-                                                       <toolkit:ListPickerItem Content="{Binding LocalizedResources.SettingsLanguageSelectionEnglish, Mode=OneWay, Source={StaticResource LocalizedStrings}}"/>
-                                                       <toolkit:ListPickerItem Content="{Binding LocalizedResources.SettingsLanguageSelectionSpanish, Mode=OneWay, Source={StaticResource LocalizedStrings}}"/>
-                                               </toolkit:ListPicker>
-                                       </StackPanel>
-                               </Grid>
-                       </phone:PivotItem>
+                            <toolkit:ListPickerItem Content="{Binding LocalizedResources.SettingsTemperatureUnitsSelectionCentigrade, Mode=OneWay, Source={StaticResource LocalizedStrings}}" />
+                        </toolkit:ListPicker>
+                    </StackPanel>
+                    <StackPanel Grid.Column="0" Grid.Row="1" Orientation="Vertical" VerticalAlignment="Center">
+                        <TextBox x:Name="LanguageHeader" Text="{Binding LocalizedResources.SettingsLanguageHeader, Mode=OneWay, Source={StaticResource LocalizedStrings}}" HorizontalAlignment="Left" Background="Transparent" BorderBrush="Transparent" Foreground="#FFA4A2A2" />
+                        <toolkit:ListPicker x:Name="LanguageSelection" SelectedIndex="{Binding Source={StaticResource SettingsViewModelDataSource}, Path=LanguageSelectionSetting, Mode=TwoWay}" SelectionChanged="LanguageSelection_SelectionChanged">
+                            <toolkit:ListPickerItem Content="{Binding LocalizedResources.SettingsLanguageSelectionEnglish, Mode=OneWay, Source={StaticResource LocalizedStrings}}"/>
+                            <toolkit:ListPickerItem Content="{Binding LocalizedResources.SettingsLanguageSelectionSpanish, Mode=OneWay, Source={StaticResource LocalizedStrings}}"/>
+                        </toolkit:ListPicker>
+                    </StackPanel>
+                </Grid>
+            </phone:PivotItem>
 
-                       <!-- Elemento Pivot dos -->
-                       <phone:PivotItem Header="notifications">
-                               <Grid/>
-                       </phone:PivotItem>
+            <!-- Elemento Pivot dos -->
+            <phone:PivotItem Header="notifications">
+                <Grid/>
+            </phone:PivotItem>
 
-               </phone:Pivot>
-               <phone:LongListSelector HorizontalAlignment="Left" Height="100" Margin="825,108,-445,0" VerticalAlignment="Top" Width="100"/>
-       </Grid>
+        </phone:Pivot>
+        <phone:LongListSelector HorizontalAlignment="Left" Height="100" Margin="825,108,-445,0" VerticalAlignment="Top" Width="100"/>
+    </Grid>
 
 </phone:PhoneApplicationPage>
\ No newline at end of file
index 743aa4f..bd08d8a 100644 (file)
@@ -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)
+        /// <summary>
+        /// Update temperature units selection when change.
+        /// </summary>
+        /// <param name="sender"></param>
+        /// <param name="e"></param>
+        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)
+        /// <summary>
+        /// Update language selection when change.
+        /// </summary>
+        /// <param name="sender"></param>
+        /// <param name="e"></param>
+        private void LanguageSelection_SelectionChanged(object sender, SelectionChangedEventArgs e)
         {
-            IList units = e.AddedItems;
-            if (units.Count > 0)
-            {
-                var selectedUnit = units.Cast<string>().First<string>();
-            }
+            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
index 2b0e1b8..4bf9dee 100644 (file)
@@ -23,9 +23,6 @@ namespace WeatherInformation.ViewModels
         /// </summary>
         public ObservableCollection<ItemViewModel> ForecastItems { get; private set; }
         public ObservableCollection<ItemViewModel> 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}"
     + "]}";
 
-        /// <summary>
-        /// Propiedad Sample MainViewModel; esta propiedad se usa en la vista para mostrar su valor mediante un enlace
-        /// </summary>
-        /// <returns></returns>
-        public string SampleProperty
-        {
-            get
-            {
-                return _sampleProperty;
-            }
-            set
-            {
-                if (value != _sampleProperty)
-                {
-                    _sampleProperty = value;
-                    NotifyPropertyChanged("SampleProperty");
-                }
-            }
-        }
-
         public bool IsDataLoaded
         {
             get;
index c960820..ef6d94c 100644 (file)
@@ -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<int>(_languageSelectionSettingKeyName, _listPickerSettingDefault);
+                return GetValueOrDefault<int>(_languageSelectionSettingKeyName, _languageSelectionSettingDefault);
             }
             set
             {
@@ -59,7 +63,7 @@ namespace WeatherInformation.ViewModels
         {
             get
             {
-                return GetValueOrDefault<int>(_temperatureUnitsSelectionSettingKeyName, _listPickerSettingDefault);
+                return GetValueOrDefault<int>(_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
         /// </summary>
         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();
         }
     }
 }