如何在第一次点击时取消屏蔽用户控件内的输入密码 Xamarin.Form
How unmask entry password inside the user control in the first click Xamarin.Form
我创建了一个包含条目和图像的用户控件。
问题
点击两次后密码取消屏蔽开始工作。
最初是这样的:
第一次点击:只改变图标
第二次点击:更改字体和图标
第二次点击后它工作正常
实施
用户控制
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>
Class:
[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>
我的问题出在 <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)
我创建了一个包含条目和图像的用户控件。
问题
点击两次后密码取消屏蔽开始工作。
最初是这样的:
第一次点击:只改变图标
第二次点击:更改字体和图标
第二次点击后它工作正常
实施
用户控制
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>
Class:
[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>
我的问题出在 <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)