繁体   English   中英

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

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

所以使用 Xamarin Forms,出于某种原因我有一个关于操作栏的问题,当隐藏它会停止平板设备上的功能,但在移动设备上工作。 不知道为什么。

所以我目前正在寻找替代解决方案,一个是将操作栏合并到页面中,以防止发生任何进一步的问题。

但是,在设置操作栏样式后,我注意到会发生以下行为。

没有样式化的操作栏:

照片一(带样式的操作栏: 照片一(带样式的Action Bar


图二(带样式的操作栏) 图二(带样式的Action Bar

我的样式文件如下:

<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>

我不确定为什么会发生这种情况,在谷歌搜索试图找到一种解决方案以再次将我的开关样式从照片二调整为照片一之后,我找不到任何东西。

有没有一种方法可以严格地将样式设置为我的操作栏或更好,我将如何 go 关于将开关样式设置为其默认“外观”?

就像前驱一样 - 这是一个严格的 Android Xamarin.Forms 应用程序,不需要 iOS 解决方案。

谢谢

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

Switch还定义了OnColorThumbColor属性。 可以设置OnColor属性来定义切换到其 on state 时的开关颜色,可以设置ThumbColor属性来定义开关拇指的颜色。

请参考以下代码:

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

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

此外,您还可以使用自定义渲染器来实现这一点。

首先,在PCL中定义一个颜色为BindableProperty的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

[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);  
    }  
}  
}

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

暂无
暂无

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

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