【发布时间】:2018-07-23 16:20:22
【问题描述】:
有人有 Xamarin Forms android 按钮自定义渲染器的完整示例吗?或者这甚至可能吗?
我基本上是想制作圆角按钮。
我尝试从这个例子开始: https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/custom-renderer/entry/
使用http://taoffi.isosoft.org/post/2016/03/26/Xamarin-forms-so-you-lost-your-rounded-buttons
但我在编译时遇到很多错误:
using System;
using System.Collections.Generic;
using System.Linq;
//using UXDivers.Artina.Shared;
using Xamarin.Forms;
using Android.Graphics.Drawables;
using System.ComponentModel;
using Xamarin.Forms.Platform.Android;
using Android.Graphics;
using Android.Views;
using System.Runtime.Remoting.Contexts;
using CustomRenderer.Android;
[assembly: ExportRenderer(typeof(Button), typeof(CustomButtonCompatRenderer))]
namespace CustomRenderer.Android
{
public class CustomButtonCompatRenderer : ButtonRenderer
{
public CustomButtonCompatRenderer(Context context) : base(context)
{
SetWillNotDraw(false);
}
private GradientDrawable _normal,
_pressed;
// resolves: button text alignment lost after click or IsEnabled change
//public override void ChildDrawableStateChanged(Android.Views.View child)
//{
// base.ChildDrawableStateChanged(child);
// Control.Text = Control.Text;
//}
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e)
{
base.OnElementChanged(e);
if (Control != null)
{
SetAlignment();
var density = Math.Max(1, Resources.DisplayMetrics.Density);
var button = e.NewElement;
var mode = MeasureSpec.GetMode((int)button.BorderRadius);
var borderRadius = button.BorderRadius * density;
var borderWidth = button.BorderWidth * density;
// Create a drawable for the button's normal state
_normal = new Android.Graphics.Drawables.GradientDrawable();
if (button.BackgroundColor.R == -1.0 && button.BackgroundColor.G == -1.0 && button.BackgroundColor.B == -1.0)
_normal.SetColor(Android.Graphics.Color.ParseColor("#ff2c2e2f"));
else
_normal.SetColor(button.BackgroundColor.ToAndroid());
_normal.SetStroke((int)borderWidth, button.BorderColor.ToAndroid());
_normal.SetCornerRadius(borderRadius);
// Create a drawable for the button's pressed state
_pressed = new Android.Graphics.Drawables.GradientDrawable();
var highlight = Context.ObtainStyledAttributes(new int[]
{
Android.Resource.Attribute.ColorAccent // .ColorActivatedHighlight
}).GetColor(0, Android.Graphics.Color.Gray);
_pressed.SetColor(highlight);
_pressed.SetStroke((int)borderWidth, button.BorderColor.ToAndroid());
_pressed.SetCornerRadius(borderRadius);
// Add the drawables to a state list and assign the state list to the button
var sld = new StateListDrawable();
sld.AddState(new int[] { Android.Resource.Attribute.StatePressed }, _pressed);
sld.AddState(new int[] { }, _normal);
Control.SetBackground(sld); //.SetBackgroundDrawable(sld); // deprecated
}
}
private void SetAlignment()
{
var element = this.Element as Button;
if (element == null || this.Control == null)
{
return;
}
this.Control.Gravity = GravityFlags.CenterHorizontal | GravityFlags.CenterVertical;
//element.VerticalAlignment.ToDroidVerticalGravity() |
//element.HorizontalAlignment.ToDroidHorizontalGravity();
}
void DrawCustom(Button targetButton)
{
if (Control == null || targetButton == null)
return;
}
}
}
错误:
CustomButtonCompatRenderer.cs(20,62,20,66): error CS1729: 'ButtonRenderer' does not contain a constructor that takes 1 arguments
CustomButtonCompatRenderer.cs(52,39,52,47): error CS0234: The type or namespace name 'Graphics' does not exist in the namespace 'CustomRenderer.Android' (are you missing an assembly reference?)
CustomButtonCompatRenderer.cs(55,38,55,54): error CS0234: The type or namespace name 'Graphics' does not exist in the namespace 'CustomRenderer.Android' (are you missing an assembly reference?)
CustomButtonCompatRenderer.cs(63,40,63,48): error CS0234: The type or namespace name 'Graphics' does not exist in the namespace 'CustomRenderer.Android' (are you missing an assembly reference?)
CustomButtonCompatRenderer.cs(66,68,66,79): error CS0117: 'Resource.Attribute' does not contain a definition for 'ColorAccent'
CustomButtonCompatRenderer.cs(67,52,67,68): error CS0234: The type or namespace name 'Graphics' does not exist in the namespace 'CustomRenderer.Android' (are you missing an assembly reference?)
CustomButtonCompatRenderer.cs(75,69,75,81): error CS0117: 'Resource.Attribute' does not contain a definition for 'StatePressed'
我的环境:
Visual Studio for mac:7.3.3(内部版本 12)
Android SDK 工具 26.1.1
Android SDK 构建工具 26.0.3
Android SDK 构建工具 25.0.3
Android 目标版本:Android 7.1 (API 25)
最低 Android 版本:Android 4.0.3 (API 15)
PS:我是 Xamarin 和 C# 的新手
【问题讨论】:
标签: c# xamarin.forms xamarin.android