Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 更改焦点上条目的边框颜色_C#_Android_Xamarin_Xamarin.forms - Fatal编程技术网

C# 更改焦点上条目的边框颜色

C# 更改焦点上条目的边框颜色,c#,android,xamarin,xamarin.forms,C#,Android,Xamarin,Xamarin.forms,我的Xamarin.Forms Android应用程序有一个自定义条目。目前,我正在使用自定义渲染器为条目提供带有边框的椭圆形 我还想在焦点上更改输入边框的颜色,并在取消焦点时恢复为原始颜色 我的自定义渲染器如下所示: public class EntryCellRenderer : EntryRenderer { protected override void OnElementChanged(ElementChangedEventArgs<Entry> e) {

我的Xamarin.Forms Android应用程序有一个自定义条目。目前,我正在使用自定义渲染器为条目提供带有边框的椭圆形

我还想在焦点上更改输入边框的颜色,并在取消焦点时恢复为原始颜色

我的自定义渲染器如下所示:

public class EntryCellRenderer : EntryRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
    {
        base.OnElementChanged(e);
        var UIEntry = (e.NewElement != null) ? (EntryCell)e.NewElement : (EntryCell)e.OldElement;

        if (this.Control != null)
        {
            Control.Gravity = Android.Views.GravityFlags.CenterVertical;
            Control.SetPadding(30, 30, 30, 31);
            GradientDrawable gd = new GradientDrawable();
            gd.SetShape(ShapeType.Rectangle);

            var BackgroundColor = ColorHelper.FromHex(CoreTheme.COLOR_DEFAULT_CLEAR);
            var ColorRef = Android.Graphics.Color.Argb(
                (byte)(BackgroundColor.A * 255),
                (byte)(BackgroundColor.R * 255),
                (byte)(BackgroundColor.G * 255),
                (byte)(BackgroundColor.B * 255));


            gd.SetColor((int)ColorRef);

            UIEntry.BackgroundColor = Xamarin.Forms.Color.Transparent;
            gd.SetStroke(7, Android.Graphics.Color.LightGray);
            gd.SetCornerRadius(20.0f);
            Control.SetBackground(gd);

        }
    }
}
公共类EntryCellRenderer:EntryRenderer
{
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
var UIEntry=(e.NewElement!=null)?(EntryCell)e.NewElement:(EntryCell)e.OldElement;
if(this.Control!=null)
{
Control.Gravity=Android.Views.GravityFlags.CenterVertical;
控制。设置填充(30,30,30,31);
GradientDrawable gd=新的GradientDrawable();
gd.SetShape(ShapeType.Rectangle);
var BackgroundColor=ColorHelper.FromHex(CoreTheme.COLOR\u DEFAULT\u CLEAR);
var ColorRef=Android.Graphics.Color.Argb(
(字节)(BackgroundColor.A*255),
(字节)(BackgroundColor.R*255),
(字节)(BackgroundColor.G*255),
(字节)(BackgroundColor.B*255));
gd.SetColor((int)ColorRef);
UIEntry.BackgroundColor=Xamarin.Forms.Color.Transparent;
gd.SetStroke(7,Android.Graphics.Color.LightGray);
gd.设置转弯半径(20.0f);
对照组:立根点(gd);
}
}
}

我不确定如何继续进行焦点事件,以完成我想在上面使用此自定义条目执行的操作

您可以连接一个
操作
,以便在调用
焦点
事件时运行。因此,您可以将新的
操作
属性添加到自定义的
入口单元格
,并连接
焦点

public class CustomEntryCell : EntryCell {

    public Action OnFocusEventAction { get; set; }

    public CustomEntryCell() { Focused += OnFocused }

    private void OnFocused(object sender, FocusEventArgs focusEventArgs) { OnFocusEventAction?.Invoke(); }

    ///Or if you are not at C# 6 yet:
    //private void OnFocused(object sender, FocusEventArgs focusEventArgs) {
    //    Action action = OnFocusEventAction;
    //    if(action != null) { action.Invoke(); }
    //}
}
然后,自定义渲染器将为控件的
操作
分配一些内容:

public class EntryCellRenderer : EntryRenderer {
    protected override void OnElementChanged(ElementChangedEventArgs<Entry> e) {
        ....
        UIEntry.OnFocusEventAction = () => //Your custom border color change code here
        ....
    }
}
公共类EntryCellRenderer:EntryRenderer{
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e){
....
UIEntry.OnFocusEventAction=()=>//此处是自定义边框颜色更改代码
....
}
}

写这些都是出于记忆,在这个文本框,所以如果有什么不工作,让我知道,我会花更多的时间在它

在您的
OnElementChanged
中,您可以将事件连接到
聚焦的
事件:

e.NewElement.Unfocused += (sender, evt) =>
{
    // unfocused, set color
};
e.NewElement.Focused += (sender, evt) =>
{
    // focus, set color
};
仅供参考:
OnNativeFocusChanged
将是通过覆盖完成此操作的完美场所,但它不是公共的

internal virtual void OnNativeFocusChanged(bool hasFocus)
{
}

好的,很好,但当我尝试取消对焦时,它不会让我取消对焦或改变颜色。有什么原因吗?@Euridice01我不太明白。当视图焦点更改为not Focused(未聚焦)时,您没有收到
Focused
事件调用?或i、 e.这条线路没有被呼叫?所以,在页面加载。。。条目没有聚焦。然后我轻触它,它就聚焦了。当我在条目内输入完毕,然后在条目外轻按。。。。它不会变得不集中。此页面继承自ContentPage(不确定是否调用了ViewRenderer?)。我怎样才能最好地调试它?顺便说一句,我正在使用Xamarin.Forms 1.5.6。。。不确定它是否是一个bug。我创建了一个单独的unFocus事件,它可以工作。看起来将其添加到焦点事件不会触发取消焦点。。。。我想可能是Xamarin的错误。表单?@Euridice01你是对的,我更新了我的答案。在Android中,通过实现一个
IOnFocusChangeListener
,它是一个单一的事件,我忘记了
表单
将其分解为两个单独的事件,这是令人困惑的(至少对我来说),因为事件包含一个
IsFocused
属性……似乎可以工作,但当我点击条目外部时,条目不会变得不聚焦。该条目位于contentpage上。我创建了一个单独的unFocus事件,它可以正常工作。看起来将其添加到焦点事件不会触发取消焦点。。。。我想这可能是Xamarin的一个错误。表格?