简体   繁体   English

如何更改 Xamarin.Forms 中的开关样式

[英]How to change style of switches in Xamarin.Forms

so using Xamarin Forms, for some reason I had an issue regarding the Action Bar, when hidden it would stop the functionality on a tablet device, but worked on a mobile device.所以使用 Xamarin Forms,出于某种原因我有一个关于操作栏的问题,当隐藏它会停止平板设备上的功能,但在移动设备上工作。 Not sure why.不知道为什么。

So I'm currently looking at alternative solutions, one is incorporating the Action Bar into the page, to prevent any further issues from occurring.所以我目前正在寻找替代解决方案,一个是将操作栏合并到页面中,以防止发生任何进一步的问题。

However, after styling the action bar, I have noticed that the following behavior happens.但是,在设置操作栏样式后,我注意到会发生以下行为。

Without styled Action Bar:没有样式化的操作栏:

Photo one (With styled Action Bar:照片一(带样式的操作栏: 照片一(带样式的Action Bar


Photo two (With styled Action Bar)图二(带样式的操作栏) 图二(带样式的Action Bar

My styling file is as follows:我的样式文件如下:

<style name="ScanCaptureTheme" parent="@android:style/Theme.Holo.Light">
    <item name="android:actionBarStyle">@style/ScanCaptureTheme.ActionBarStyle</item>
</style>

<style name="ScanCaptureTheme.ActionBarStyle" parent="@android:style/Widget.Holo.Light.ActionBar">
    <item name="android:titleTextStyle">@style/ScanCaptureTheme.TitleTextStyle</item>
</style>

<style name="ScanCaptureTheme.TitleTextStyle" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">
    <item name="android:textColor">#214478</item>
</style>

I am not exactly sure why this is happening and after googling to attempt to find a solution to adjust the styling of my switches from Photo two to Photo one again, I couldn't find anything.我不确定为什么会发生这种情况,在谷歌搜索试图找到一种解决方案以再次将我的开关样式从照片二调整为照片一之后,我找不到任何东西。

Is there a way to strictly have the styling set to my action bar or better yet, how I would go about styling the switches to their default "look"?有没有一种方法可以严格地将样式设置为我的操作栏或更好,我将如何 go 关于将开关样式设置为其默认“外观”?

Just as a precusor - this is a strictly Android Xamarin.Forms application, no need for an iOS solution.就像前驱一样 - 这是一个严格的 Android Xamarin.Forms 应用程序,不需要 iOS 解决方案。

Thanks谢谢

How to change style of switches in Xamarin.Forms如何更改 Xamarin.Forms 中的开关样式

Switch also defines OnColor and ThumbColor properties. Switch还定义了OnColorThumbColor属性。 The OnColor property can be set to define the Switch color when it is toggled to its on state, and the ThumbColor property can be set to define the Color of the switch thumb.可以设置OnColor属性来定义切换到其 on state 时的开关颜色,可以设置ThumbColor属性来定义开关拇指的颜色。

Please refer to the following code:请参考以下代码:

<Switch OnColor="Orange"
        ThumbColor="Green" />

For more, check: https://learn.microsoft.com/en-us/xamarin/xamarin.forms/user-interface/switch#switch-appearance .有关更多信息,请查看: https://learn.microsoft.com/en-us/xamarin/xamarin.forms/user-interface/switch#switch-appearance

Besides ,you can also achieve this using custom renderer.此外,您还可以使用自定义渲染器来实现这一点。

First, define a CustomSwitch in PCL with color BindableProperty(with it you can set value and use on different platforms)首先,在PCL中定义一个颜色为BindableProperty的CustomSwitch(通过它你可以设置值并在不同的平台上使用)

CustomSwitch自定义开关

public class CustomSwitch : Switch  
{  
   public static readonly BindableProperty SwitchOffColorProperty =  
     BindableProperty.Create(nameof(SwitchOffColor),  
         typeof(Color), typeof(CustomSwitch),  
         Color.Default);  

   public Color SwitchOffColor  
   {  
       get { return (Color)GetValue(SwitchOffColorProperty); }  
       set { SetValue(SwitchOffColorProperty, value); }  
   }  

   public static readonly BindableProperty SwitchOnColorProperty =  
     BindableProperty.Create(nameof(SwitchOnColor),  
         typeof(Color), typeof(CustomSwitch),  
         Color.Default);  

   public Color SwitchOnColor  
   {  
       get { return (Color)GetValue(SwitchOnColorProperty); }  
       set { SetValue(SwitchOnColorProperty, value); }  
   }  

   public static readonly BindableProperty SwitchThumbColorProperty =  
     BindableProperty.Create(nameof(SwitchThumbColor),  
         typeof(Color), typeof(CustomSwitch),  
         Color.Default);  

   public Color SwitchThumbColor  
   {  
       get { return (Color)GetValue(SwitchThumbColorProperty); }  
       set { SetValue(SwitchThumbColorProperty, value); }  
   }  

   public static readonly BindableProperty SwitchThumbImageProperty =  
     BindableProperty.Create(nameof(SwitchThumbImage),  
         typeof(string),  
         typeof(CustomSwitch),  
         string.Empty);  

   public string SwitchThumbImage  
   {  
       get { return (string)GetValue(SwitchThumbImageProperty); }  
       set { SetValue(SwitchThumbImageProperty, value); }  
   }  
} 

Android Android

[assembly: ExportRenderer(typeof(CustomSwitch), typeof(CustomSwitchRenderer))]
namespace FormsApp.Android
{
public class CustomSwitchRenderer : SwitchRenderer  
{  
    private CustomSwitch view;  
    protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Switch> e)  
    {  
        base.OnElementChanged(e);  
        if (e.OldElement != null || e.NewElement == null)  
            return;  
        view = (CustomSwitch)Element;  
        if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.JellyBean)  
        {  
            if (this.Control != null)  
            {  
                if (this.Control.Checked)  
                {  
                    this.Control.TrackDrawable.SetColorFilter(view.SwitchOnColor.ToAndroid(), PorterDuff.Mode.SrcAtop);  
                }  
                else  
                {  
                    this.Control.TrackDrawable.SetColorFilter(view.SwitchOffColor.ToAndroid(), PorterDuff.Mode.SrcAtop);  
                }  
               this.Control.CheckedChange += this.OnCheckedChange;  
                UpdateSwitchThumbImage(view);  
            }  
            //Control.TrackDrawable.SetColorFilter(view.SwitchBGColor.ToAndroid(), PorterDuff.Mode.Multiply);  
        }  
    }  
      
    private void UpdateSwitchThumbImage(CustomSwitch view)  
    {  
        if (!string.IsNullOrEmpty(view.SwitchThumbImage))  
        {  
            view.SwitchThumbImage = view.SwitchThumbImage.Replace(".jpg", "").Replace(".png", "");  
            int imgid = (int)typeof(Resource.Drawable).GetField(view.SwitchThumbImage).GetValue(null);  
            Control.SetThumbResource(Resource.Drawable.icon);  
        }  
        else  
        {  
            Control.ThumbDrawable.SetColorFilter(view.SwitchThumbColor.ToAndroid(), PorterDuff.Mode.Multiply);  
            // Control.SetTrackResource(Resource.Drawable.track);  
        }  
    }  

  private void OnCheckedChange(object sender, CompoundButton.CheckedChangeEventArgs e)  
    {  
        if (this.Control.Checked)  
        {  
            this.Control.TrackDrawable.SetColorFilter(view.SwitchOnColor.ToAndroid(), PorterDuff.Mode.SrcAtop);  
        }  
        else  
        {  
            this.Control.TrackDrawable.SetColorFilter(view.SwitchOffColor.ToAndroid(), PorterDuff.Mode.SrcAtop);  
        }  
    }  
    protected override void Dispose(bool disposing)  
    {  
        this.Control.CheckedChange -= this.OnCheckedChange;  
        base.Dispose(disposing);  
    }  
}  
}

Refer: https://stackoverflow.com/a/48516159/10308336 .参考: https://stackoverflow.com/a/48516159/10308336

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM