[Core] Propagate BindingContext changes to TitleView (#3823)
authorShane Neuville <shane94@hotmail.com>
Thu, 20 Sep 2018 04:07:05 +0000 (22:07 -0600)
committerJason Smith <jas@microsoft.com>
Thu, 20 Sep 2018 04:07:05 +0000 (21:07 -0700)
* [Core] Propagate BindingContext changes to TitleView

* [Tests] Move UI test to Core Unit Test

Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems
Xamarin.Forms.Core.UnitTests/TitleViewUnitTests.cs [new file with mode: 0644]
Xamarin.Forms.Core.UnitTests/Xamarin.Forms.Core.UnitTests.csproj
Xamarin.Forms.Core/NavigationPage.cs
Xamarin.Forms.Core/Page.cs

index f812eca..a2874c6 100644 (file)
@@ -9,7 +9,7 @@
     <Import_RootNamespace>Xamarin.Forms.Controls.Issues</Import_RootNamespace>
   </PropertyGroup>
   <ItemGroup>
-         <Compile Include="$(MSBuildThisFileDirectory)Issue2894.cs" />
+    <Compile Include="$(MSBuildThisFileDirectory)Issue2894.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Issue3524.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Issue2004.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Issue3333.cs" />
       <Generator>MSBuild:UpdateDesignTimeXaml</Generator>
     </EmbeddedResource>
   </ItemGroup>
-</Project>
+</Project>
\ No newline at end of file
diff --git a/Xamarin.Forms.Core.UnitTests/TitleViewUnitTests.cs b/Xamarin.Forms.Core.UnitTests/TitleViewUnitTests.cs
new file mode 100644 (file)
index 0000000..d5e3156
--- /dev/null
@@ -0,0 +1,69 @@
+using System.Collections;
+using System.Linq;
+using NUnit.Framework;
+
+
+namespace Xamarin.Forms.Core.UnitTests
+{
+       [TestFixture]
+       public class TitleViewUnitTests : BaseTestFixture
+       {
+               [SetUp]
+               public override void Setup()
+               {
+                       base.Setup();
+                       Device.PlatformServices = new MockPlatformServices();
+               }
+
+               [TearDown]
+               public override void TearDown()
+               {
+                       base.TearDown();
+                       Device.PlatformServices = null;
+               }
+
+               [Test]
+               public void BindingContextPropagatesFromParent()
+               {
+                       NavigationPage navigationPage = new NavigationPage();
+                       var image1 = new Image();
+                       image1.SetBinding(Image.SourceProperty, "ImageSource");
+                       var page = new ContentPage()
+                       {
+                               Content = new Label()
+                       };
+
+                       var title = new Label() { Text = "Failed" };
+                       title.SetBinding(Label.TextProperty, "Title");
+
+                       var layout = new StackLayout()
+                       {
+                               Orientation = StackOrientation.Horizontal,
+                               Children =
+                                       {
+                                               title,
+                                               image1
+                                       }
+                       };
+
+                       page.SetValue(NavigationPage.TitleViewProperty, layout);
+                       navigationPage.PushAsync(page);
+
+                       var model = new Model();
+                       navigationPage.BindingContext = new Model();
+                       Assert.AreEqual(model.Title, title.Text);
+
+                       string success = "Success";
+                       page.BindingContext = new Model() { Title = success };
+                       Assert.AreEqual(success, title.Text);
+                       navigationPage.BindingContext = new Model() { Title = "Failed" };
+                       Assert.AreEqual(success, title.Text);
+               }
+
+               public class Model
+               {
+                       public string Title { get; set; } = "Binding Working";
+                       public string ImageSource { get; } = "coffee.png";
+               }
+       }
+}
index 3832e3c..9e4b59d 100644 (file)
@@ -1,4 +1,4 @@
-<?xml version="1.0" encoding="utf-8"?>
+<?xml version="1.0" encoding="utf-8"?>
 <Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
   <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
   <PropertyGroup>
@@ -80,6 +80,7 @@
     <Compile Include="MenuUnitTests.cs" />
     <Compile Include="RegionTests.cs" />
     <Compile Include="SpanTests.cs" />
+    <Compile Include="TitleViewUnitTests.cs" />
     <Compile Include="TemplatedViewUnitTests.cs" />
     <Compile Include="TemplatedPageUnitTests.cs" />
     <Compile Include="ContentFormUnitTests.cs" />
       <LogicalName>Images/crimson.jpg</LogicalName>
     </EmbeddedResource>
   </ItemGroup>
-</Project>
+</Project>
\ No newline at end of file
index 8e3f97b..ee25b1a 100644 (file)
@@ -110,16 +110,14 @@ namespace Xamarin.Forms
                        if (oldValue == newValue)
                                return;
 
-                       if (oldValue != null)
+                       if(bindable is Page page)
                        {
-                               var oldElem = (View)oldValue;
-                               oldElem.Parent = null;
+                               page.SetTitleView((View)oldValue, (View)newValue);
                        }
-
-                       if (newValue != null && bindable != null)
+                       else if (oldValue != null)
                        {
-                               var newElem = (View)newValue;
-                               newElem.Parent = (Page)bindable;
+                               var oldElem = (View)oldValue;
+                               oldElem.Parent = null;
                        }
                }
 
index 8a4efb5..e69b4ff 100644 (file)
@@ -43,6 +43,8 @@ namespace Xamarin.Forms
 
                ReadOnlyCollection<Element> _logicalChildren;
 
+               View _titleView;
+
                public Page()
                {
                        var toolbarItems = new ObservableCollection<ToolbarItem>();
@@ -214,6 +216,9 @@ namespace Xamarin.Forms
                        {
                                SetInheritedBindingContext(toolbarItem, BindingContext);
                        }
+
+                       if(_titleView != null)
+                               SetInheritedBindingContext(_titleView, BindingContext);
                }
 
                protected virtual void OnChildMeasureInvalidated(object sender, EventArgs e)
@@ -421,5 +426,16 @@ namespace Xamarin.Forms
                {
                        return _platformConfigurationRegistry.Value.On<T>();
                }
+
+               internal void SetTitleView(View oldTitleView, View newTitleView)
+               {
+                       if (oldTitleView != null)
+                               oldTitleView.Parent = null;
+
+                       if (newTitleView != null)
+                               newTitleView.Parent = this;
+
+                       _titleView = newTitleView;
+               }
        }
 }