[WinRT/UWP] Fix Slider binding value incorrectly (#729)
authorPaul DiPietro <pauldipietro@users.noreply.github.com>
Tue, 31 Jan 2017 12:57:43 +0000 (06:57 -0600)
committerKangho Hur <kangho.hur@samsung.com>
Fri, 24 Mar 2017 04:16:43 +0000 (13:16 +0900)
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla41619.cs [new file with mode: 0644]
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems
Xamarin.Forms.Platform.WinRT/SliderRenderer.cs

diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla41619.cs b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla41619.cs
new file mode 100644 (file)
index 0000000..24c277d
--- /dev/null
@@ -0,0 +1,62 @@
+using Xamarin.Forms.CustomAttributes;
+using Xamarin.Forms.Internals;
+using System.ComponentModel;
+using System.Runtime.CompilerServices;
+
+#if UITEST
+using Xamarin.UITest;
+using NUnit.Framework;
+#endif
+
+namespace Xamarin.Forms.Controls.Issues
+{
+       [Preserve(AllMembers = true)]
+       [Issue(IssueTracker.Bugzilla, 41619, "[WinRT/UWP] Slider binding works incorrectly", PlatformAffected.WinRT)]
+       public class Bugzilla41619 : TestContentPage
+       {
+               protected override void Init()
+               {
+                       var vm = new Bugzilla41619ViewModel { SliderValue = 5 };
+                       BindingContext = vm;
+                       var label = new Label();
+                       label.SetBinding(Label.TextProperty, "SliderValue");
+                       var slider = new Slider
+                       {
+                               Maximum = 10,
+                               Minimum = 1,
+                       };
+                       slider.SetBinding(Slider.ValueProperty, "SliderValue", BindingMode.TwoWay);
+                       Content = new StackLayout
+                       {
+                               Children =
+                               {
+                                       label,
+                                       slider,
+                                       new Label { Text = "The initial slider value above should be 5." }
+                               }
+                       };
+               }
+
+               class Bugzilla41619ViewModel : INotifyPropertyChanged
+               {
+                       private double _sliderValue;
+
+                       public double SliderValue
+                       {
+                               get { return _sliderValue; }
+                               set
+                               {
+                                       _sliderValue = value;
+                                       OnPropertyChanged();
+                               }
+                       }
+
+                       public event PropertyChangedEventHandler PropertyChanged;
+
+                       protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
+                       {
+                               PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
+                       }
+               }
+       }
+}
\ No newline at end of file
index 5574bbc..b8e45b4 100644 (file)
     <Compile Include="$(MSBuildThisFileDirectory)Bugzilla41415.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Bugzilla41418.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Bugzilla41424.cs" />
+    <Compile Include="$(MSBuildThisFileDirectory)Bugzilla41619.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Bugzilla42069.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Bugzilla42069_Page.xaml.cs">
       <DependentUpon>Bugzilla42069_Page.xaml</DependentUpon>
index cf188f1..0fff149 100644 (file)
@@ -24,7 +24,11 @@ namespace Xamarin.Forms.Platform.WinRT
                                        var slider = new Windows.UI.Xaml.Controls.Slider();
                                        SetNativeControl(slider);
 
-                                       slider.ValueChanged += OnNativeValueCHanged;
+                                       Control.Minimum = e.NewElement.Minimum;
+                                       Control.Maximum = e.NewElement.Maximum;
+                                       Control.Value = e.NewElement.Value;
+
+                                       slider.ValueChanged += OnNativeValueChanged;
 
                                        // Even when using Center/CenterAndExpand, a Slider has an oddity where it looks
                                        // off-center in its layout by a smidge. The default templates are slightly different
@@ -49,10 +53,6 @@ namespace Xamarin.Forms.Platform.WinRT
                                double stepping = Math.Min((e.NewElement.Maximum - e.NewElement.Minimum) / 10, 1);
                                Control.StepFrequency = stepping;
                                Control.SmallChange = stepping;
-
-                               Control.Minimum = e.NewElement.Minimum;
-                               Control.Maximum = e.NewElement.Maximum;
-                               Control.Value = e.NewElement.Value;
                        }
                }
 
@@ -71,7 +71,7 @@ namespace Xamarin.Forms.Platform.WinRT
                        }
                }
 
-               void OnNativeValueCHanged(object sender, RangeBaseValueChangedEventArgs e)
+               void OnNativeValueChanged(object sender, RangeBaseValueChangedEventArgs e)
                {
                        ((IElementController)Element).SetValueFromRenderer(Slider.ValueProperty, e.NewValue);
                }