如何在iOS ViewCellRenderer中更改窗体ViewList的MenuItem颜色? [英] How to Change the MenuItem Colors of Forms ViewList in iOS ViewCellRenderer?

查看:80
本文介绍了如何在iOS ViewCellRenderer中更改窗体ViewList的MenuItem颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何方法可以更改Xamarin.Forms Xaml文件中添加的ContextAction菜单的颜色. IsDestructive="True"将菜单颜色设置为红色.但是我需要另一个菜单看起来像绿色或其他颜色.

Is there any way to Change the color of ContextAction Menus added in Xamarin.Forms Xaml file. IsDestructive="True" sets the menu color to Red. But i need another menu to look like Green or some other color.

这是我的Forms Xaml代码.

Here is my Forms Xaml code..

<ListView x:Name="planList" ItemsSource="{x:Static local:SampleData.PLAN_DATA}" RowHeight="150" HorizontalOptions="FillAndExpand">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <ViewCell.ContextActions>
                    <MenuItem Clicked="OnEditClick" Text="Edit" CommandParameter="{Binding .}"/> <!-- THIS HAS TO BE GREEN COLOR -->
                    <MenuItem Clicked="OnDeleteClick" Text="Delete" IsDestructive="True" />
                </ViewCell.ContextActions>
                <ViewCell.View>
                    <StackLayout Orientation="Vertical"  HorizontalOptions="Start" VerticalOptions="FillAndExpand">
                        <!--Non Editable State-->
                        <StackLayout Orientation="Horizontal" Spacing="28" IsVisible="{Binding isNotSaveState}">
                            <Frame  WidthRequest="130" HeightRequest="50" BackgroundColor="#151617" HorizontalOptions="Start">
                                <Label Text="{Binding from}" TextColor="#ff9600" FontSize="Medium" FontFamily="Helvetica"/>
                            </Frame>
                        </StackLayout>
                        <!--Editable State-->
                        <StackLayout Orientation="Horizontal" Spacing="0"  IsVisible="{Binding isSaveState}">
                            <StackLayout Orientation="Horizontal" Spacing="5">
                                <Label Text="From" TextColor="#838288"  FontSize="Medium" FontFamily="Helvetica"/>
                                <Entry Text="" BackgroundColor="Red"/>
                            </StackLayout>
                            <Button Text="Save" BackgroundColor="Green" CommandParameter="{Binding .}" Clicked="onSaveClick" />
                        </StackLayout>
                    </StackLayout>
                </ViewCell.View>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

这是我的渲染器.

[assembly: ExportRenderer(typeof(MyApp.Views.Cells.CustomViewCell), typeof(MyApp.iOS.Views.Cells.CustomViewCellRenderer))]
namespace MyApp.iOS.Views.Cells
{

    public class CustomViewCellRenderer : ViewCellRenderer
    {
        public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
        {
            UITableViewCell cell = base.GetCell(item, reusableCell, tv);

            // I have no Idea how to access the Swipe Menus from Renderer

            //cell.EditingAccessory
            //cell.EditingAccessoryView

            return cell;
        }        
    }
}

推荐答案

Xamarin通过本机单元ContextActionsCell子类化UITableViewCell来实现上下文菜单.

Xamarin implements the context menu by a native cell ContextActionsCell subclassing the UITableViewCell.

但是,到目前为止, Xamarin.Forms.Platform.iOS (1.4.0.6340-pre2)程序集,用于设置手势和按钮的ContextActionsCell仍然是internal类,该类不可更改,无法访问或继承.

However, as of today Xamarin.Forms.Platform.iOS (1.4.0.6340-pre2) assembly, the ContextActionsCell that setups the gesture and buttons is still an internal class, which is not accessible or inheritable for changing anything.

但是等待!

不过,您可以使用 Reflection 来更改internal之上的内容.

Still, you may use Reflection to change above internal stuff.

示例:

// Get UIImage with a green color fill
CGRect rect = new CGRect (0, 0, 1, 1);
CGSize size = rect.Size;
UIGraphics.BeginImageContext (size);
CGContext currentContext = UIGraphics.GetCurrentContext ();
currentContext.SetFillColor (0, 1, 0, 1);
currentContext.FillRect (rect);
var backgroundImage = UIGraphics.GetImageFromCurrentImageContext ();
currentContext.Dispose ();

// This is the assembly full name which may vary by the Xamarin.Forms version installed.
// NullReferenceException is raised if the full name is not correct.
var t = Type.GetType("Xamarin.Forms.Platform.iOS.ContextActionsCell, Xamarin.Forms.Platform.iOS, Version=1.3.1.0, Culture=neutral, PublicKeyToken=null");

// Now change the static field value!
var field = t.GetField ("destructiveBackground", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
field.SetValue (null, backgroundImage);

注1:这将更改所有上下文菜单的破坏性背景颜色.

Note 1: This will change the destructive background colors of all context menu.

注2:如果您还想更改常规颜色,则字段名称为normalBackground.

注3:Xamarin可以使这些类成为internal,其目的是将来改变API和行为.上面的示例可能会在以后的版本中中断.

Note 3: Xamarin may make these classes internal with a purpose that the API and behaviours may change in the future. Above example may break in coming releases.

这篇关于如何在iOS ViewCellRenderer中更改窗体ViewList的MenuItem颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆