【问题标题】:Xamarin Forms Displaying the list of email recipientsXamarin Forms 显示电子邮件收件人列表
【发布时间】:2017-10-19 23:40:54
【问题描述】:

我对 Xamarin 表单和 C# 还很陌生。

我正在尝试在 Xamarin Forms 应用中显示电子邮件收件人列表。

模型类有:

public class EmailAddress3
{

    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("address")]
    public string Address { get; set; }
}

public class ToRecipient
{

    [JsonProperty("emailAddress")]
    public EmailAddress3 EmailAddress { get; set; }
}

public class MailItem
{
 ........
    [JsonProperty("toRecipients")]
    public ToRecipient[] ToRecipients { get; set; }
 .........
 }

后面的C#代码

 public partial class EmailDetailPage : ContentPage
 {
    public EmailDetailPage(MailItem sentMailDetail)
    {
        if (sentMailDetail == null)
            throw new ArgumentNullException();

        BindingContext = sentMailDetail;

        InitializeComponent();
    }
}

和 XAML 文件

    <StackLayout>
        <Label Text="{Binding CcRecipients.EmailAddress.Name}" />
    </StackLayout>

当我在设备上运行它时,我不断收到以下错误消息:

Binding: 'EmailAddress' property not found on 'ISPulse.Model.CcRecipient[]', target property: 'Xamarin.Forms.Label.Text'

我尝试了标签和模型类的不同组合,但没有成功。

任何关于我如何实现这一点的想法都将不胜感激。

【问题讨论】:

  • 如果 CcRecipients 是一个集合(你没有显示代码,所以我不能确定),那么你不能这样绑定它。您必须指定您所指的集合中的哪个项目,例如 CcRecipients[0].EmailAddress.Name

标签: c# xamarin xamarin.forms


【解决方案1】:

如果 ccRecipients 是一个集合,那么您可以将 ListView 绑定到该集合,类似于:

<ContentPage ... x:Name="MyPage">

    <ListView x:Name="ListviewRecipients" 
        ItemsSource="{Binding ccRecipients, Source={x:Reference MyPage}}"
                      VerticalOptions="Fill">
        <Label Text="{Binding EmailAddress.Name}" />
    </ListView>

</ContentPage>

MyPage 已更新以匹配 XAML 文件中根元素的 x:Name 属性。这应该会显示一个标签列表,每个收件人都有一个标签,名称会输出到屏幕上。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-24
    • 2015-05-11
    • 1970-01-01
    • 2016-07-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多