点击标签崩溃应用程序

时间:2017-09-26 12:28:58

标签: c# email xamarin label call

我不知道我的代码有什么问题。有人可以检查我的代码是否有失败/错误?我正在尝试列出联系人列表。当我点击电子邮件时,我想发送邮件到我点击的地址。与电话号码相同。 When I tap Email or Tlfnr

我的xaml.CS代码:

    private void OnEmailTapped(object sender, EventArgs e)
    {
        var emailMessenger = CrossMessaging.Current.EmailMessenger;
        if (emailMessenger.CanSendEmail)
        {
            emailMessenger.SendEmail(to: ((Label)sender).Text); 
        }
    }

我的Xaml代码:

                                <StackLayout x:Name="EmailTapped">
                                    <Label x:Name="OnEmailTapped" Text="{Binding Email}" TextColor="Black" FontSize="Medium" />

                                    <Label.GestureRecognizers>
                                        <TapGestureRecognizer Tapped="OnEmailTapped" CommandParameter="{Binding Email}"/>
                                    </Label.GestureRecognizers>
                                </StackLayout>

我的联系人:

namespace App3
{
public class Kontakter
{
    public string Fuldenavn { get; set; }
    public string Tlfnr { get; set; }
    public string Email { get; set; }

    public List<Kontakter> GetKontakter()
    {
        List<Kontakter> kontakter = new List<Kontakter>
    {
        new Kontakter
        {
            Fuldenavn = "bacon (ANSE)",
            Email = "Random@gmail.com",
            Tlfnr = 12345678,
        },
        new Kontakter
        {
            Fuldenavn = "Anja (ANBI)",
            Email = "Random@hotmail.dk",
            Tlfnr = 87654321,
        },
        new Kontakter
        {
            Fuldenavn = "Benn (BMR)",
            Email = "Random@hotmail.com",
            Tlfnr = 12876534,
        },
        new Kontakter
        {
            Fuldenavn = "Christian (CBE)",
            Email = "Nothing@gmail.com",
            Tlfnr = 18273645,
        },
    };
        return kontakter;
    }
}
}

2 个答案:

答案 0 :(得分:0)

我有类似的问题,当我删除时它停止了崩溃

if (emailMessenger.CanSendEmail)

答案 1 :(得分:0)

您可以做的是将发件人视为控件。

Control castControl = (Control)sender

或设置断点并将值分配给随机变量并悬停发件人对象

enter image description here

然后对转折点悬停

中显示的特定类型进行强制转换

我认为发件人可能是TapGestureRecognizer而不是Label

虽然我的节目显示为Label,但它只是让您了解如何找到发件人对象的type