我创建了一个包含条目和图像的用户控件。
问题
两次单击后,密码解密功能开始起作用。
最初是这样的:
首次点击:仅更改图标
第二次单击后它可以正常工作
实施
用户控制
xml:
<?xml version="1.0" encoding="utf-8" ?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
xmlns:control="clr-namespace:Projecro_3.ControlCostumiado"
prism:ViewModelLocator.AutowireViewModel="True"
Padding="10"
x:Class="Projecro_3.Controls.EntryControl">
<AbsoluteLayout>
<control:CustomEntry x:Name="entry"> </control:CustomEntry>
<Image x:Name="imgFinal" >
<Image.GestureRecognizers>
<TapGestureRecognizer
Tapped="ImagemFinal_Tapped"
NumberOfTapsRequired="1" />
</Image.GestureRecognizers>
</Image>
</AbsoluteLayout>
</ContentView>
班级:
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class EntryControl : ContentView
{
public bool IsPassword
{
get
{
return (bool)GetValue(IsPasswordProperty);
}
set
{
SetValue(IsPasswordProperty, value);
}
}
public static readonly BindableProperty IsPasswordProperty = BindableProperty.Create(
propertyName: "IsPassword",
returnType: typeof(bool),
declaringType: typeof(EntryControl),
defaultValue: false,
defaultBindingMode: BindingMode.TwoWay,
propertyChanged: IsPasswordPropertyChanged);
public EntryControl()
{
InitializeComponent();
entry.BindingContext = this;
}
private static void IsPasswordPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
var control = (EntryControl)bindable;
if (control == null) return;
bool isPassword = (bool)newValue;
control.entry.IsPassword = isPassword;
control.imgFinal.Source = new FileImageSource { File = isPassword ? imageEyePassaword : imageEyeOffPassaword };
}
private void ImagemFinal_Tapped(object sender, EventArgs e)
{
IsPassword = !IsPassword;
}
private const string imageEyePassaword = "eye.png";
private const string imageEyeOffPassaword = "eye_off.png";
}
主页
<control:EntryControl IsPassword="True"></control:EntryControl>
答案 0 :(得分:0)
我的问题出在<control:CustomEntry x:Name="entry"> </control:CustomEntry>
,CustomEntry
,这是我自定义的条目。
public class CustomEntry : Entry
{
}
Android
[assembly: ExportRenderer(typeof(CustomEntry), typeof(CustomEntryRenderer))]
namespace Projecro_3.Droid
{
public class CustomEntryRenderer : EntryRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if (Control != null)
{
GradientDrawable gd = new GradientDrawable();
gd.SetColor(global::Android.Graphics.Color.Transparent);
this.Control.SetBackgroundDrawable(gd);
this.Control.SetRawInputType(InputTypes.TextFlagNoSuggestions);
}
}
}
}
解决方案
我刚刚删除了这一行
this.Control.SetRawInputType(InputTypes.TextFlagNoSuggestions)