根据documentation IFunction
继承public class EmailValidatorBehavior : Behavior<Entry>
{
const string digitRegex = @"^(?("")("".+?(?<!\\)""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" +
@"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9][\-a-z0-9]{0,22}[a-z0-9]))$";
static readonly BindablePropertyKey IsValidPropertyKey = BindableProperty.CreateReadOnly("IsValid", typeof(bool), typeof(EmailValidatorBehavior), false);
public static readonly BindableProperty IsValidProperty = IsValidPropertyKey.BindableProperty;
static readonly BindablePropertyKey IsVisiblePropertyKey = BindableProperty.CreateReadOnly("IsVisible", typeof(bool), typeof(EmailValidatorBehavior), false);
public static readonly BindableProperty IsVisibleProperty = IsVisiblePropertyKey.BindableProperty;
public bool IsValid
{
get { return (bool)base.GetValue(IsValidProperty); }
private set { base.SetValue(IsValidPropertyKey, value); }
}
public bool IsVisible
{
get { return (bool)base.GetValue(IsVisibleProperty); }
private set { base.SetValue(IsVisiblePropertyKey, value); }
}
protected override void OnAttachedTo(Entry entry)
{
entry.TextChanged += OnEntryTextChanged;
base.OnAttachedTo(entry);
}
protected override void OnDetachingFrom(Entry entry)
{
entry.TextChanged -= OnEntryTextChanged;
base.OnDetachingFrom(entry);
}
void OnEntryTextChanged(object sender, TextChangedEventArgs e)
{
if (e.NewTextValue.Length > 0)
{
IsVisible = true;
Entry entry = (Entry)sender;
IsValid = Regex.IsMatch(e.NewTextValue, digitRegex);
if (IsValid) // Check only if we have a valid email
{
// Here we validate if the email contains our requirements
String email = entry.Text;
int pos = email.IndexOf("@"); // Exclude the domain
string username = email.Substring(0, pos);
if (username.Contains(FRIEND) || username.Contains(FRIENDS))
{
IsValid = true;
}
else
IsValid = false;
}
}
else
IsVisible = false;
}
public class BooleanToObjectConverter<Image> : IValueConverter
{
public Image FalseObject { set; get; }
public Image TrueObject { set; get; }
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
return (bool)value ? this.TrueObject : this.FalseObject;
}
//public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
//{
// throw new NotImplementedException();
//}
public object ConvertBack(object value, Type targetType,
object parameter, CultureInfo culture)
{
return ((Image)value).Equals(this.TrueObject);
}
//public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
//{
// throw new NotImplementedException();
//}
}
:<Application
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="CoTraveller.RegistrationPage"
xmlns:local="clr-namespace:CoTraveller">
<Application.Resources>
<!-- Application resource dictionary -->
<ResourceDictionary>
<local:BooleanToObjectConverter x:Key="boolToStyleImage"
x:TypeArguments="Style">
<local:BooleanToObjectConverter.FalseObject>
<Style TargetType="Image">
<Setter Property="HeightRequest" Value="24" />
<Setter Property="Source"
Value="your_wrong_image_here.png" />
</Style>
</local:BooleanToObjectConverter.FalseObject>
<x:BooleanToObjectConverter.TrueObject>
<Style TargetType="Image">
<Setter Property="HeightRequest" Value="24" />
<Setter Property="Source"
Value="your_correct_image_here.png" />
</Style>
</x:BooleanToObjectConverter.TrueObject>
</local:BooleanToObjectConverter>
</ResourceDictionary>
</Application.Resources>
</Application>
-> io.BufferedReader
-> io.IOBase
。因此,应该实现io.IOBase
和io.BufferedIOBase
,但事实并非如此,因为我得到了io.BufferedReader
。一个最小的例子:
数据样本:
readline
测试代码:
readinto
例外:
AttributeError
我想念什么吗?
更新
有趣的是,您可以从$ printf 'foo bar\nspam ham\n' | gzip -c > compressed_file.gz
中手动获取一个import gzip
import io
with io.BufferedReader(gzip.open('compressed_file.gz', 'rt')) as buffer:
buffer.readline()
来包装字节流,并且---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-2-215a0d9b4feb> in <module>()
3
4 with io.BufferedReader(gzip.open('compressed_file.gz', 'rt')) as buffer:
----> 5 buffer.readline()
AttributeError: '_io.TextIOWrapper' object has no attribute 'readinto'
方法将起作用。
io.TextIOWrapper
似乎io.BufferedReader
返回的readline
实例与import gzip
import io
with io.TextIOWrapper(io.BufferedReader(gzip.open('compressed_file.gz'))) as buffer:
buffer.readline()
实例不同,尽管后者是is directly imported from _io
。
答案 0 :(得分:0)
是的,您的确确实缺少关键细节:BufferedReader
包装了一个现有的RawIOBase
对象:
io.BufferedReader类(原始,buffer_size = DEFAULT_BUFFER_SIZE)
提供对可读的顺序
RawIOBase
对象的更高级别访问的缓冲区。
并且因为您已经以 text 模式打开了gzip文件,所以它不是RawIOBase
实例:
>>> f = gzip.open('compressed_file.gz', 'rt')
>>> isinstance(f, io.RawIOBase)
False
换句话说,这是用户错误。
如果将BufferedReader
包裹在 binary 文件中,则会发现readline
方法可以正常工作:
>>> io.BufferedReader(io.BytesIO(b'foo\nbar')).readline()
b'foo\n'