更改Xamarin.forms中单击并按住按钮的背景色
在我的Xamarin.forms应用程序中,我有两个按钮。我需要实现以下目标更改Xamarin.forms中单击并按住按钮的背景色,xamarin,xamarin.forms,xamarin.ios,xamarin.android,Xamarin,Xamarin.forms,Xamarin.ios,Xamarin.android,在我的Xamarin.forms应用程序中,我有两个按钮。我需要实现以下目标 默认情况下,两个按钮都有白色背景 当用户点击一个按钮时,它的背景应该变成蓝色 若用户按住,则颜色应变为蓝色,当用户释放时,颜色应恢复为白色 显然,只有一个按钮可以有蓝色背景 样品 为了实现这一点,您必须设计一个自定义渲染器 在Core/PCL项目中创建按钮类: public class CustomButton : Button { public event EventHandler OnPressed;
为了实现这一点,您必须设计一个自定义渲染器 在Core/PCL项目中创建按钮类:
public class CustomButton : Button
{
public event EventHandler OnPressed;
public event EventHandler OnReleased;
public void OnPressed()
{
OnPressed?.Invoke(this, EventArgs.Empty);
}
public void OnReleased()
{
OnReleased?.Invoke(this, EventArgs.Empty);
}
}
然后在iOS和Android中创建渲染器:
例如:安卓:
[assembly: ExportRenderer(typeof(CustomButton), typeof(CustomButtonRenderer))]
namespace yourproject.Droid.Renderer
{
public class CustomButtonRenderer : ButtonRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e)
{
base.OnElementChanged(e);
var customRendererButton = e.NewElement as CustomButton;
var control = Control as Android.Widget.Button;
control.Touch += (object sender, TouchEventArgs args) =>
{
if (args.Event.Action == MotionEventActions.Up)
{
customRendererButton.OnReleased();
}
else if (args.Event.Action == MotionEventActions.Down)
{
customRendererButton.OnPressed();
}
};
}
}
}
希望这有帮助 要实现这一点,您必须设计自定义渲染器 在Core/PCL项目中创建按钮类:
public class CustomButton : Button
{
public event EventHandler OnPressed;
public event EventHandler OnReleased;
public void OnPressed()
{
OnPressed?.Invoke(this, EventArgs.Empty);
}
public void OnReleased()
{
OnReleased?.Invoke(this, EventArgs.Empty);
}
}
然后在iOS和Android中创建渲染器:
例如:安卓:
[assembly: ExportRenderer(typeof(CustomButton), typeof(CustomButtonRenderer))]
namespace yourproject.Droid.Renderer
{
public class CustomButtonRenderer : ButtonRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e)
{
base.OnElementChanged(e);
var customRendererButton = e.NewElement as CustomButton;
var control = Control as Android.Widget.Button;
control.Touch += (object sender, TouchEventArgs args) =>
{
if (args.Event.Action == MotionEventActions.Up)
{
customRendererButton.OnReleased();
}
else if (args.Event.Action == MotionEventActions.Down)
{
customRendererButton.OnPressed();
}
};
}
}
}
希望这有帮助 您可以在xaml中使用
x:Name=“myButton”
,然后:
myButton.Touch += (object sender, TouchEventArgs args) =>
{
if (args.Event.Action == MotionEventActions.Up)
{
myButton.BackgroundColor = Color.Blue;
}
else if (args.Event.Action == MotionEventActions.Down)
{
myButton.BackgroundColor = Color.Red;
}
};
您可以在xaml中使用
x:Name=“myButton”
,然后:
myButton.Touch += (object sender, TouchEventArgs args) =>
{
if (args.Event.Action == MotionEventActions.Up)
{
myButton.BackgroundColor = Color.Blue;
}
else if (args.Event.Action == MotionEventActions.Down)
{
myButton.BackgroundColor = Color.Red;
}
};
此解决方案将如何处理以下场景。。。。1.当用户点击按钮(即短点击)时,其背景应变为蓝色2.如果用户按住按钮(即长点击),则颜色应变为蓝色,当用户释放时,颜色应在事件
OnPressed
中恢复为白色,在OnReleased
中恢复为白色,您对上面的代码有什么问题?此解决方案将如何处理以下场景。。。。1.当用户点击按钮(即短点击)时,其背景应变为蓝色2.如果用户按住按钮(即长点击),则颜色应变为蓝色,当用户释放时,颜色应在事件OnPressed
中恢复为白色,在OnReleased
中恢复为白色,上面的代码有什么问题?
myButton.Touch += (object sender, TouchEventArgs args) =>
{
if (args.Event.Action == MotionEventActions.Up)
{
myButton.BackgroundColor = Color.Blue;
}
else if (args.Event.Action == MotionEventActions.Down)
{
myButton.BackgroundColor = Color.Red;
}
};