WeatherInformation: settings pages
authorGustavo Martin <gu.martinm@gmail.com>
Mon, 9 Jun 2014 06:24:14 +0000 (08:24 +0200)
committerGustavo Martin <gu.martinm@gmail.com>
Mon, 9 Jun 2014 06:24:14 +0000 (08:24 +0200)
WindowsPhone/WeatherInformation/WeatherInformation/App.xaml.cs
WindowsPhone/WeatherInformation/WeatherInformation/SampleData/TemperatureUnitsViewModelSampleData.xaml [new file with mode: 0644]
WindowsPhone/WeatherInformation/WeatherInformation/SettingsPage.xaml
WindowsPhone/WeatherInformation/WeatherInformation/SettingsPage.xaml.cs
WindowsPhone/WeatherInformation/WeatherInformation/SettingsTemperatureUnitsPage.xaml [new file with mode: 0644]
WindowsPhone/WeatherInformation/WeatherInformation/SettingsTemperatureUnitsPage.xaml.cs [new file with mode: 0644]
WindowsPhone/WeatherInformation/WeatherInformation/ViewModels/SettingsViewModel.cs
WindowsPhone/WeatherInformation/WeatherInformation/ViewModels/TemperatureUnitsViewModel.cs [new file with mode: 0644]
WindowsPhone/WeatherInformation/WeatherInformation/WeatherInformation.csproj

index fcf2361..6081f2a 100644 (file)
@@ -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;
 
         /// <summary>
         /// 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;
+            }
+        }
+
         /// <summary>
         /// Proporcionar acceso sencillo al marco raíz de la aplicación telefónica.
         /// </summary>
diff --git a/WindowsPhone/WeatherInformation/WeatherInformation/SampleData/TemperatureUnitsViewModelSampleData.xaml b/WindowsPhone/WeatherInformation/WeatherInformation/SampleData/TemperatureUnitsViewModelSampleData.xaml
new file mode 100644 (file)
index 0000000..31a02cc
--- /dev/null
@@ -0,0 +1,12 @@
+<vm:TemperatureUnitsViewModel
+    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+    xmlns:vm="clr-namespace:WeatherInformation.ViewModels"
+    SampleProperty="Valor de propiedad Sample Text">
+
+    <vm:TemperatureUnitsViewModel.TemperatureUnitsItems>
+        <vm:ItemViewModel LineOne="fahrenheit"/>
+        <vm:ItemViewModel LineOne="centigrade"/>
+    </vm:TemperatureUnitsViewModel.TemperatureUnitsItems>
+
+</vm:TemperatureUnitsViewModel>
\ No newline at end of file
index fa99662..0095553 100644 (file)
 
     <!--LayoutRoot es la cuadrícula raíz donde se coloca todo el contenido de la página-->
     <Grid x:Name="LayoutRoot" Background="Transparent">
-        <!--Control Pivot-->
+        <!-- Pivot -->
         <phone:Pivot Title="WEATHER INFORMATION">
-            <!--Elemento Pivot uno-->
+            <!-- Elemento Pivot uno -->
             <phone:PivotItem Header="settings">
-                <!--Lista de líneas dobles con ajuste de texto-->
+                <!-- de líneas dobles con ajuste de texto -->
                 <phone:LongListSelector x:Name="SettingsList" Margin="0,0,-12,0" ItemsSource="{Binding SettingsItems}" SelectionChanged="LongListSelector_SelectionChanged">
                     <phone:LongListSelector.ItemTemplate>
                         <DataTemplate>
                 </phone:LongListSelector>
             </phone:PivotItem>
 
-            <!--Elemento Pivot dos
+            <!-- Elemento Pivot dos -->
             <phone:PivotItem Header="notifications">
                 <Grid/>
             </phone:PivotItem>
-            -->
+            
         </phone:Pivot>
     </Grid>
-    
+
+    <phone:PhoneApplicationPage.Resources>
+        <DataTemplate x:Key="LongListGroupHeader">
+            <Grid Margin="12,0,0,0">
+                <Grid Width="75" Height="75" HorizontalAlignment="Left">
+                    <TextBlock Margin="12,0,1,7" TextWrapping="Wrap" d:LayoutOverrides="Width, Height" Style="{StaticResource PhoneTextLargeStyle}" Text="{Binding Title}" VerticalAlignment="Bottom"/>
+                    <Border BorderThickness="1">
+                        <Border.BorderBrush>
+                            <SolidColorBrush Color="{StaticResource PhoneAccentColor}"/>
+                        </Border.BorderBrush>
+                    </Border>
+                </Grid>
+            </Grid>
+        </DataTemplate>
+    </phone:PhoneApplicationPage.Resources>
+
 </phone:PhoneApplicationPage>
\ No newline at end of file
index 3a91202..194be6a 100644 (file)
@@ -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 (file)
index 0000000..9af9e90
--- /dev/null
@@ -0,0 +1,38 @@
+<phone:PhoneApplicationPage
+    x:Class="WeatherInformation.SettingsTemperatureUnitsPage"
+    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
+    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
+    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
+    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
+    mc:Ignorable="d"
+    d:DataContext="{d:DesignData SampleData/TemperatureUnitsViewModelSampleData.xaml}"
+    FontFamily="{StaticResource PhoneFontFamilyNormal}"
+    FontSize="{StaticResource PhoneFontSizeNormal}"
+    Foreground="{StaticResource PhoneForegroundBrush}"
+    SupportedOrientations="Portrait"  Orientation="Portrait"
+    shell:SystemTray.IsVisible="True">
+
+    <!--LayoutRoot es la cuadrícula raíz donde se coloca todo el contenido de la página-->
+    <Grid x:Name="LayoutRoot" Background="Transparent">
+        <!--Control Pivot-->
+        <phone:Pivot Title="WEATHER INFORMATION">
+            <!--Elemento Pivot uno-->
+            <phone:PivotItem Header="temperature">
+                <!-- de líneas dobles con ajuste de texto -->
+                <phone:LongListSelector x:Name="SettingsList" Margin="0,0,-12,0" ItemsSource="{Binding TemperatureUnitsItems}">
+                    <phone:LongListSelector.ItemTemplate>
+                        <DataTemplate>
+                            <StackPanel Orientation="Vertical" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="0,10">
+                                <TextBlock Text="{Binding LineOne}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}" HorizontalAlignment="Left" VerticalAlignment="Center" FontWeight="Bold"/>
+                                <TextBlock Text="{Binding LineTwo}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="24"/>
+                            </StackPanel>
+                        </DataTemplate>
+                    </phone:LongListSelector.ItemTemplate>
+                </phone:LongListSelector>
+            </phone:PivotItem>
+        </phone:Pivot>
+    </Grid>
+    
+</phone:PhoneApplicationPage>
\ 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 (file)
index 0000000..fbb95d6
--- /dev/null
@@ -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
index c238a6f..9a185ea 100644 (file)
@@ -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;
+        }
+
+        /// <summary>
+        /// Crear y agregar unos pocos objetos ItemViewModel a la colección Items.
+        /// </summary>
+        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 (file)
index 0000000..f316cbf
--- /dev/null
@@ -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<ItemViewModel>();
+        }
+
+        private string _sampleProperty = "Sample Runtime Property Value";
+
+        public ObservableCollection<ItemViewModel> TemperatureUnitsItems { get; private set; }
+        /// <summary>
+        /// Propiedad Sample ViewModel; 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;
+            private set;
+        }
+
+        /// <summary>
+        /// Crear y agregar unos pocos objetos ItemViewModel a la colección Items.
+        /// </summary>
+        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));
+            }
+        }
+    }
+}
index fbf314e..db4c845 100644 (file)
     <Compile Include="SettingsPage.xaml.cs">
       <DependentUpon>SettingsPage.xaml</DependentUpon>
     </Compile>
+    <Compile Include="SettingsTemperatureUnitsPage.xaml.cs">
+      <DependentUpon>SettingsTemperatureUnitsPage.xaml</DependentUpon>
+    </Compile>
     <Compile Include="ViewModels\ItemViewModel.cs" />
     <Compile Include="ViewModels\MainViewModel.cs" />
+    <Compile Include="ViewModels\TemperatureUnitsViewModel.cs" />
     <Compile Include="ViewModels\SettingsViewModel.cs" />
   </ItemGroup>
   <ItemGroup>
       <Generator>MSBuild:Compile</Generator>
       <SubType>Designer</SubType>
     </DesignData>
+    <DesignData Include="SampleData\TemperatureUnitsViewModelSampleData.xaml">
+      <Generator>MSBuild:Compile</Generator>
+      <SubType>Designer</SubType>
+    </DesignData>
     <Page Include="SettingsPage.xaml">
       <SubType>Designer</SubType>
       <Generator>MSBuild:Compile</Generator>
     </Page>
+    <Page Include="SettingsTemperatureUnitsPage.xaml">
+      <SubType>Designer</SubType>
+      <Generator>MSBuild:Compile</Generator>
+    </Page>
   </ItemGroup>
   <ItemGroup>
     <None Include="packages.config" />