Skip to content
Permalink
Browse files

Can use Effect to attach platform specific

  • Loading branch information...
samhouts committed Jul 15, 2016
1 parent a75157c commit 63a924d77e10a4d637b1b2c2709f4613f4a4e532
@@ -0,0 +1,72 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ImAVendor.Forms.PlatformConfiguration.iOS;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.ControlGallery.iOS;
using Xamarin.Forms.Platform.iOS;
using Xamarin.Forms.PlatformConfiguration.iOS;

[assembly: ExportEffect(typeof(NavigationPageTranslucentEffect), "NavigationPageTranslucentEffect")]
namespace Xamarin.Forms.ControlGallery.iOS
{
public class NavigationPageTranslucentEffect : PlatformEffect
{
UINavigationBar _navigationBar;

protected override void OnAttached()
{
UpdateTranslucent();
}

protected override void OnDetached()
{

}

protected override void OnElementPropertyChanged(PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(e);

try
{
if (e.PropertyName == FakeVendorConfig.IsNavigationBarTranslucentProperty.PropertyName)
UpdateTranslucent();
}
catch (Exception ex)
{
Console.WriteLine("Cannot set property on attached control. Error: ", ex.Message);
}
}

void UpdateTranslucent()
{
var navBar = GetNavigationBar();
if (navBar == null)
return;

//if (!Forms.IsiOS7OrNewer)
//{
// return;
//}
navBar.Translucent = ((NavigationPage)Element).On<IConfigIOS>().GetNavigationBarIsTranslucentVendor();
}

UINavigationBar GetNavigationBar()
{
if (_navigationBar == null)
{
foreach (var subView in Container.Subviews)
{
if (subView is UINavigationBar)
_navigationBar = subView as UINavigationBar;
}
}
return _navigationBar;
}
}
}
@@ -34,3 +34,4 @@
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion ("1.0.0.0")]
[assembly: AssemblyFileVersion ("1.0.0.0")]
[assembly: Xamarin.Forms.ResolutionGroupName("XamControl")]
@@ -162,6 +162,7 @@
<Compile Include="AppDelegate.cs" />
<None Include="app.config" />
<None Include="Info.plist" />
<Compile Include="NavigationPageTranslucentEffect.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<None Include="packages.config" />
<Compile Include="CustomRenderers.cs" />
@@ -1,5 +1,6 @@
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;
using ImAVendor.Forms.PlatformConfiguration.iOS;
using Xamarin.Forms.PlatformConfiguration.iOS;

namespace Xamarin.Forms.Controls
@@ -14,7 +15,8 @@ protected override void Init()

var button = new Button { Text = "Toggle Translucent", BackgroundColor = Color.Yellow };

button.Clicked += (sender, args) => On<IConfigIOS>().SetNavigationBarIsTranslucent(!On<IConfigIOS>().GetNavigationBarIsTranslucent());
button.Clicked += (sender, args) => On<IConfigIOS>()
.SetNavigationBarIsTranslucentVendor(!On<IConfigIOS>().GetNavigationBarIsTranslucentVendor());

var content = new ContentPage
{
@@ -6,7 +6,7 @@
using Xamarin.Forms.Controls.Issues;
using Xamarin.Forms.PlatformConfiguration.Android;
using Xamarin.Forms.PlatformConfiguration.iOS;
using ImAVendor.Forms.iOS;
using ImAVendor.Forms.PlatformConfiguration.iOS;

namespace Xamarin.Forms.Controls
{
@@ -1,7 +1,7 @@
using NUnit.Framework;
using Xamarin.Forms.PlatformConfiguration.Android;
using Xamarin.Forms.PlatformConfiguration.iOS;
using ImAVendor.Forms.iOS;
using ImAVendor.Forms.PlatformConfiguration.iOS;

namespace Xamarin.Forms.Core.UnitTests
{
@@ -254,6 +254,16 @@ void IElementController.SetValueFromRenderer(BindablePropertyKey property, objec
SetValueCore(property, value);
}

bool IElementController.EffectIsAttached(string name)
{
foreach (var effect in Effects)
{
if (effect.ResolveId == name)
return true;
}
return false;
}

object INameScope.FindByName(string name)
{
INameScope namescope = GetNameScope();
@@ -6,6 +6,8 @@ public interface IElementController
{
IEffectControlProvider EffectControlProvider { get; set; }

bool EffectIsAttached(string name);

void SetValueFromRenderer(BindableProperty property, object value);
void SetValueFromRenderer(BindablePropertyKey propertyKey, object value);
ReadOnlyCollection<Element> LogicalChildren { get; }
@@ -171,11 +171,12 @@ namespace Xamarin.Forms.PlatformConfiguration.iOS
{
public interface IConfigIOS : IConfigPlatform { }

public static class NavigationPageiOSpecifics
public static class NavigationPageConfig
{
public static readonly BindableProperty IsNavigationBarTranslucentProperty =
BindableProperty.Create("IsNavigationBarTranslucent", typeof(bool),
typeof(NavigationPage), false);
typeof(NavigationPageConfig), false);

public static bool GetNavigationBarIsTranslucent(this IPlatformElementConfiguration<IConfigIOS, NavigationPage> config)
{
return (bool)config.Element.GetValue(IsNavigationBarTranslucentProperty);
@@ -188,13 +189,15 @@ public static bool GetNavigationBarIsTranslucent(this IPlatformElementConfigurat
}
}

namespace ImAVendor.Forms.iOS
namespace ImAVendor.Forms.PlatformConfiguration.iOS
{
using Xamarin.Forms.PlatformConfiguration;
using Xamarin.Forms.PlatformConfiguration.iOS;

public static class FakeVendorExtensions
public static class FakeVendorConfig
{
const string NavBarTranslucentEffectName = "XamControl.NavigationPageTranslucentEffect";

public static readonly BindableProperty FooProperty = BindableProperty.Create("VendorFoo", typeof(bool), typeof(IPlatformElementConfiguration<IConfigIOS, MasterDetailPage>), true);

public static void SetVendorFoo(this IPlatformElementConfiguration<IConfigIOS, MasterDetailPage> mdp, bool value)
@@ -206,5 +209,31 @@ public static bool GetVendorFoo(this IPlatformElementConfiguration<IConfigIOS, M
{
return (bool)mdp.Element.GetValue(FooProperty);
}


public static readonly BindableProperty IsNavigationBarTranslucentProperty =
BindableProperty.Create("IsNavigationBarTranslucent", typeof(bool),
typeof(FakeVendorConfig), false);

public static bool GetNavigationBarIsTranslucentVendor(this IPlatformElementConfiguration<IConfigIOS, NavigationPage> config)
{
AttachEffect(config);
return (bool)config.Element.GetValue(IsNavigationBarTranslucentProperty);
}
public static IPlatformElementConfiguration<IConfigIOS, NavigationPage> SetNavigationBarIsTranslucentVendor(this IPlatformElementConfiguration<IConfigIOS, NavigationPage> config, bool value)
{
AttachEffect(config);
config.Element.SetValue(IsNavigationBarTranslucentProperty, value);
return config;
}

static void AttachEffect(IPlatformElementConfiguration<IConfigIOS, NavigationPage> config)
{
IElementController controller = config.Element;
if (controller.EffectIsAttached(NavBarTranslucentEffectName))
return;

config.Element.Effects.Add(Effect.Resolve(NavBarTranslucentEffectName));
}
}
}
@@ -465,7 +465,7 @@ void HandlePropertyChanged(object sender, PropertyChangedEventArgs e)
UpdateBackgroundColor();
else if (e.PropertyName == NavigationPage.CurrentPageProperty.PropertyName)
Current = ((NavigationPage)Element).CurrentPage;
else if (e.PropertyName == NavigationPageiOSpecifics.IsNavigationBarTranslucentProperty.PropertyName)
else if (e.PropertyName == NavigationPageConfig.IsNavigationBarTranslucentProperty.PropertyName)
UpdateTranslucent();
}

0 comments on commit 63a924d

Please sign in to comment.
You can’t perform that action at this time.