2016-08-17 2 views

答えて

2

スイッチのカスタムレンダリング(iOSのUISwitch)を作成し、そのOnTintColorプロパティを設定します。 PCLで

:iOS版で

public class CustomSwitch : Switch 
{ 
} 

[assembly: ExportRenderer(typeof(CustomSwitch), typeof(CustomSwitchRenderer))] 

namespace xyz.iOS.CustomControlRenderers 
{ 
    public class CustomSwitchRenderer : SwitchRenderer 
    { 
     protected override void OnElementChanged (ElementChangedEventArgs<Switch> e) 
     { 
      base.OnElementChanged (e); 

      if (Control != null) 
      { 
       // do whatever you want to the UISwitch here! 
       Control.OnTintColor = UIColor.FromRGB (204, 153, 255); 
      } 
     } 
    } 
} 

あなたがAndroidプラットフォームでカスタムレンダリングを作成する必要はありませんアンドロイドのための任意のカスタマイズを必要としないので。

フォームの実装に対応するすべてのネイティブレンダリングのリストをhereから取得できます。 カスタムレンダリングの例はhereです。

+1

ありがとう! – sylmz

+0

ようこそ。 :-) –

0

@Rohitの答えに間違いはありませんが、間違いなく動作します。しかし、より良い解決法(この特定の場合)は、Effectsを使用することであることを指摘したいと思います。 Why?

あなたはそれで行くことに決めた場合、あなたの実装は次のようなものになります:

のiOSプロジェクトコード:

Constants.ResolutionGroupNameが定義されたばかりの文字列(例えば "MyCompanyの")、である
using UIKit; 
using VisualStimulation.iOS.Effects; 
using VisualStimulation.Utilities; 
using Xamarin.Forms; 
using Xamarin.Forms.Platform.iOS; 

[assembly: ResolutionGroupName(Constants.ResolutionGroupName)] 
[assembly: ExportEffect(typeof(SwitchEffects), nameof(SwitchEffects))] 
namespace VisualStimulation.iOS.Effects 
{ 
    public class SwitchEffects : PlatformEffect 
    { 
     protected override void OnAttached() 
     { 
      var switchControl = Control as UISwitch; 
      if (switchControl != null) 
      { 
       switchControl.OnTintColor = UIColor.FromRGB(204, 153, 255); 
      } 
     } 
     protected override void OnDetached() 
     { 

     } 
    } 
} 

を別のクラスで。

PCLコード:ContentPageレベルで定義されeffect名前空間に

<Switch> 
    <Switch.Effects> 
     <effects:SwitchEffects></effects:SwitchEffects> 
    </Switch.Effects> 
</Switch> 

using VisualStimulation.Utilities; 
using Xamarin.Forms; 

namespace VisualStimulation.Effects 
{ 
    public class SwitchEffects : RoutingEffect 
    { 
     public SwitchEffects() : base($"{Constants.ResolutionGroupName}.{nameof(SwitchEffects)}") 
     { 
     } 
    } 
} 

そして最後に、XAMLコード

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
      xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"    
      xmlns:effects="clr-namespace:<effect.class.namespace>;assembly=<nameOfYourAssembly>" 
0

あなたはそれを変更したい場合はアプリ全体の (iOSのプロジェクトで)それはAppDelegate.csにFinishedLaunching方法であるファイルを実行する方法をトン方法:

UISwitch.Appearance.OnTintColor = UIColor.FromRGB(0xFF, 0x00, 0x00); // RED 

全例:

public override bool FinishedLaunching (UIApplication app, NSDictionary options) 
{ 
    // switch 
    UISwitch.Appearance.OnTintColor = UIColor.FromRGB(0xFF, 0x00, 0x00); 
    // required Xamarin.Forms code 
    Forms.Init(); 
    LoadApplication (new App()); 
    return base.FinishedLaunching (app, options); 
} 

出典:https://developer.xamarin.com/guides/xamarin-forms/platform-features/ios/theme/#UISwitch

関連する問題