Xamarin.forms 是否可以在Android自定义样式中使用AppThemeBinding?

Xamarin.forms 是否可以在Android自定义样式中使用AppThemeBinding?,xamarin.forms,xamarin.android,Xamarin.forms,Xamarin.android,我正试图通过使用新的。它适用于Android和iOS,但我不知道如何为自定义渲染器或自定义样式实现它 例如,我得到了一个定制的步进渲染器,它看起来像这样: protected override void OnElementChanged(ElementChangedEventArgs<Stepper> e) { base.OnElementChanged(e); if (Control != null) { Android.Widget.But

我正试图通过使用新的。它适用于Android和iOS,但我不知道如何为自定义渲染器或自定义样式实现它

例如,我得到了一个定制的步进渲染器,它看起来像这样:

protected override void OnElementChanged(ElementChangedEventArgs<Stepper> e)
{
    base.OnElementChanged(e);
    if (Control != null)
    {
        Android.Widget.Button buttonDown = (Android.Widget.Button)Control.GetChildAt(0);
        Android.Widget.Button buttonUp = (Android.Widget.Button)Control.GetChildAt(1);
        if(e.NewElement != null)
        {
            //Button Down
            buttonDown.SetBackgroundResource(Resource.Drawable.button_bg_left);
            buttonDown.LayoutParameters = new LinearLayout.LayoutParams(DpToPixel(50), DpToPixel(33));
            buttonDown.SetPadding(0,0,0,0);
            buttonDown.SetTextColor(Android.Graphics.Color.ParseColor("#007bff"));
            //Button Up
            buttonUp.SetBackgroundResource(Resource.Drawable.button_bg_right);
            buttonUp.LayoutParameters = new LinearLayout.LayoutParams(DpToPixel(50), DpToPixel(33));
            buttonUp.SetPadding(0, 0, 0, 0);
            buttonUp.SetTextColor(Android.Graphics.Color.ParseColor("#007bff"));
                  
        }
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#FFFFFF"/>
    <stroke android:width="1dp" android:color="#007bff" />
    <corners
            android:topLeftRadius="5dp"
            android:bottomLeftRadius="5dp"/>
</shape>
由于AppThemeBind是一个标记扩展,在
.xaml
文件中工作,我不知道如何为android特定渲染器实现颜色更改

如何更改灯光/暗模式的自定义形状的颜色?

您可以在渲染器中设置相应的颜色:

class MyStepperRenderer : StepperRenderer
{
    public MyStepperRenderer(Context context) : base(context)
    {
    }

    protected override void OnElementChanged(ElementChangedEventArgs<Stepper> e)
    {
        base.OnElementChanged(e);

        OSAppTheme currentTheme = Xamarin.Forms.Application.Current.RequestedTheme;

        if (currentTheme == OSAppTheme.Light)
        {
            //
        }
        else
        {
            //
        }
    }
}
类mystepperrender:stepperrender
{
公共mystepperrender(上下文):基(上下文)
{
}
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
OSAppTheme currentTheme=Xamarin.Forms.Application.Current.RequestedTheme;
if(currentTheme==OSAppTheme.Light)
{
//
}
其他的
{
//
}
}
}

Cool,这适用于我的自定义渲染器!但有一件事我还是很好奇。我得到了
styles.xml
文件,在该文件中我定义了主题颜色,该颜色在
MainActivity.cs
中引用。如何更改这些颜色以响应AppTheme的更改?您的意思是无法从渲染器中的
style.xml
获取颜色?否-我的意思是设置主题,如
theme=“@style/MainTheme”
颜色,如
colorPrimary
colorAccent
,用于Xamarin中的某些元素。表单,例如,如果我得到一个TableView,那么一个部分的标题将自动使用这些颜色中的一种,而无需自定义渲染器。是否必须创建自定义渲染器以根据当前的
OSAppTheme
更改颜色?否,如果是在Xamarin.forms中,则可以通过以下方式直接设置颜色:
。就像中的代码一样。很抱歉混淆了,它不是直接在表单中。以一个简单的选择器为例——在Android中,选择器下面有一条彩色的线,只要你点击它,它就会改变。我使用在
MainActivity
中设置的
主题来控制这些颜色,但是这些颜色仅在
.xml
文件中定义,而不是在自定义渲染器中定义。