如何使用 xamarin 形式的十六进制值以编程方式更改汉堡包菜单图标的颜色?

How do I programmatically change the color of the hamburger menu icon using hex values in xamarin forms?

我正在开发一个 mvvm light xamarin 表单应用程序,我允许用户在应用程序本身中创建自己的主题。截至目前,除了发送按钮(图像图​​标)、添加按钮(图像图​​标)和汉堡菜单图标外,我的一切都按预期工作。

用户选择 image/name,然后该颜色的十六进制值将应用于 control/font。但是我在尝试以编程方式设置汉堡包菜单时遇到了 运行 问题 color.I 从 things.I 的 android 侧开始可以使用 styles.xml 设置一次颜色.

但这还不足以满足我的要求。我还没有弄清楚如何在不创建新引用和提供可绘制对象的情况下获取对 toggledrawer 的引用以更改颜色。

我还需要在 iOS(可变颜色汉堡菜单)上执行相同的操作。我不想提供图像,因为用户会根据十六进制值设置颜色……我最好的选择是什么?在这一点上,我正在尝试做的事情是否可行?

   public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        protected ActionBarDrawerToggle drawerToggle;
        protected override void OnCreate(Bundle savedInstanceState)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;

            base.OnCreate(savedInstanceState);
            Rg.Plugins.Popup.Popup.Init(this, savedInstanceState);
            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
            LoadApplication(new App());

    drawerToggle.getDrawerArrowDrawable().setColor(getResources().getColor(R.color.white));
        }
    }

我在这里找到这段代码:

drawerToggle.getDrawerArrowDrawable().setColor(getResources().getColor(R.color.white));

这将是理想的,但我没有对 drawerToggle 的引用...(对象为空)

我也尝试使用 styles.xml 进行设置,如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<resources>

  <style name="MainTheme" parent="MainTheme.Base">
  </style>
  <!-- Base theme applied no matter what API -->
  <style name="MainTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
    <!--If you are using revision 22.1 please use just windowNoTitle. Without android:-->
    <item name="windowNoTitle">true</item>
    <!--We will be using the toolbar so no need to show ActionBar-->
    <item name="windowActionBar">false</item>
    <!-- Set theme colors from http://www.google.com/design/spec/style/color.html#color-color-palette -->
    <!-- colorPrimary is used for the default action bar background -->
    <item name="colorPrimary">#2196F3</item>
    <!-- colorPrimaryDark is used for the status bar -->
    <item name="colorPrimaryDark">#1976D2</item>
    <!-- colorAccent is used as the default value for colorControlActivated
         which is used to tint widgets -->
    <item name="colorAccent">#FF4081</item>
    <!-- You can also set colorControlNormal, colorControlActivated
         colorControlHighlight and colorSwitchThumbNormal. -->
    <item name="windowActionModeOverlay">true</item>

    <item name="android:datePickerDialogTheme">@style/AppCompatDialogStyle</item>
  <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
  </style>

  <style name="DrawerArrowStyle" parent="@style/Widget.AppCompat.DrawerArrowToggle">
    <item name="spinBars">true</item>
    <item name="color">@android:color/green</item>
  </style>

  <style name="AppCompatDialogStyle" parent="Theme.AppCompat.Light.Dialog">
    <item name="colorAccent">#FF4081</item>
  </style>
</resources>

这段代码确实可以设置一次,但我需要通过单击按钮在应用程序中重置它。所以这对于我正在尝试做的事情来说是不够的。

  1. 将汉堡菜单图标添加到可绘制文件夹

  2. 在您希望更改的地方添加代码。

var toolbar = FindViewById<AndroidToolbar>(Resource.Id.toolbar);
var view = (AppCompatImageButton)toolbar.GetChildAt(2); 
    // 1. actionmenuview (primary/secondary menu icons), 
    // 2. appcompattextview (title), 
    // 3. appcompatimagebutton (hamburger icon)
view.SetImageDrawable(d);
           // view.SetImageDrawable(d);

在我的例子中,我有菜单图标,所以我 select GetChildAt(2) [第三项]