如何将图像添加到电子邮件行为
Posted
技术标签:
【中文标题】如何将图像添加到电子邮件行为【英文标题】:How to add image to Email behaviour 【发布时间】:2017-12-31 03:30:47 【问题描述】:我想知道如何将image
添加到行为中,实际上我正在尝试编写能够更改条目背景的电子邮件行为,但同时我想为其添加图像。
我的xml:
<StackLayout>
<Entry Placeholder="Enter a System.Double">
<Entry.Behaviors>
<local:CustomBehavior />
</Entry.Behaviors>
</Entry>
</StackLayout>
我的行为类:
public class CustomBehavior: Behavior<Entry>
private const string digitRegex = @"^(?("")("".+?(?<!\\)""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\\\|~\w])*)(?<=[0-9a-z])@))" +
@"(?(\[)(\[(\d1,3\.)3\d1,3\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9][\-a-z0-9]0,22[a-z0-9]))$";
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)
Entry entry;
bool isValid;
entry =(Entry)sender;
isValid = Regex.IsMatch(e.NewTextValue, digitRegex);
entry.BackgroundColor = isValid ? Color.Default : Color.Red;
【问题讨论】:
如果您想使用电子邮件图标,您可以使用表情符号,否则您必须创建具有自己视图的自定义控件 我想在输入的文本为真时添加刻度线图像,并在文本格式不正确时添加十字标记。您可以在此处粘贴示例代码。 【参考方案1】:第一个。根据您的要求,您必须在布局中添加单独的图片:
<StackLayout
Orientation="Horizontal">
<Entry Placeholder="Enter a System.Double">
<Entry.Behaviors>
<local:CustomBehavior x:Name="customValidator"/>
</Entry.Behaviors>
</Entry>
<Image
HeightRequest="24"
WidthRequest="24"
Aspect="AspectFit"
IsVisible="Binding Source=x:Reference customValidator,
Path=IsVisible"
Style="Binding Source=x:Reference customValidator,
Path=IsValid,
Converter=StaticResource boolToStyleImage"/>
</StackLayout>
注意 x:Name="" 属性,因为这是在此 xaml 文件中引用该自定义行为所必需的
第二次。在您的“CustomBehavior”上为两个字段创建 BindableProperties,您将在这两个字段上绑定您的条目的图像状态
public class CustomBehavior: Behavior<Entry>
private const string digitRegex = @"^(?("")("".+?(?<!\\)""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\\\|~\w])*)(?<=[0-9a-z])@))" +
@"(?(\[)(\[(\d1,3\.)3\d1,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 (CustomBehavior), false);
public static readonly BindableProperty IsValidProperty = IsValidPropertyKey.BindableProperty;
static readonly BindablePropertyKey IsVisiblePropertyKey = BindableProperty.CreateReadOnly ("IsVisible", typeof (bool), typeof (CustomBehavior), false);
public static readonly BindableProperty IsVisibleProperty = IsVisiblePropertyKey.BindableProperty;
private const string FRIEND = "friend";
private const string FRIENDS = "friends";
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;
3d。创建一个简单的 ValueConverter 类,它将一个布尔值转换为我们的“T”类型的对象之一,具体取决于我们将此转换器附加到的对象。
namespace YourApp
public class BooleanToObjectConverter<T> : IValueConverter
public T FalseObject set; get;
public T TrueObject set; get;
public object Convert (object value, Type targetType,
object parameter, CultureInfo culture)
return (bool)value ? this.TrueObject : this.FalseObject;
public object ConvertBack (object value, Type targetType,
object parameter, CultureInfo culture)
return ((T)value).Equals (this.TrueObject);
4th. 将此样式添加到 App.xaml 文件的 ResourceDictionary 标记中,这将声明 TrueObject(有效电子邮件的样式)和 FalseObject(无效电子邮件的样式)。
将“your_wrong_image_here.png”和“your_correct_image_here.png”替换为您想要的图片
<Application
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="YourApp.App"
xmlns:statics="clr-namespace:YourApp">
<Application.Resources>
<!-- Application resource dictionary -->
<ResourceDictionary>
<statics:BooleanToObjectConverter x:Key="boolToStyleImage"
x:TypeArguments="Style">
<statics:BooleanToObjectConverter.FalseObject>
<Style TargetType="Image">
<Setter Property="HeightRequest" Value="24" />
<Setter Property="Source"
Value="your_wrong_image_here.png" />
</Style>
</statics:BooleanToObjectConverter.FalseObject>
<statics:BooleanToObjectConverter.TrueObject>
<Style TargetType="Image">
<Setter Property="HeightRequest" Value="24" />
<Setter Property="Source"
Value="your_correct_image_here.png" />
</Style>
</statics:BooleanToObjectConverter.TrueObject>
</statics:BooleanToObjectConverter>
</ResourceDictionary>
</Application.Resources>
</Application>
这应该可以满足您的需求,只是要小心复制粘贴错误,因为您的项目中有不同的类名!
【讨论】:
感谢您的详细解答。 @sahithi 不客气,如果对你有帮助,请选择正确答案 谢谢@MalokuS,您的回答对我来说非常有效。您能帮我了解如何比较电子邮件条目中的特定字符串,例如每个条目必须并且应该包含“朋友”或“朋友”,使用一般邮件地址,如 abc.friend@xyz.com 或 abc.friends@xyz.com。 再次欢迎您@sahithi,很高兴它对您有用。使用新检查检查我对您的“CustomBehavior”类所做的编辑。以上是关于如何将图像添加到电子邮件行为的主要内容,如果未能解决你的问题,请参考以下文章
使用 Python Win32 发送电子邮件。将图像添加到电子邮件正文不起作用