Retrieve & Preview the Attribute Values stored in IEnumerable in c#

I want to retrieve & preview the name & values of attributes (credentials) stored in IEnumerable<CredentialPreviewAttribute> CredentialAttributesValues

By running a loop foreach (var item in _credential.CredentialAttributesValues), if I print it on console e.g. Console.WriteLine(), I get the name and value for all the attributes. But If I want to preview (output) it on screen (using xml), it only returns the last attribute name and value.

The mobile application received the credentials from web application, which is stored in

CredentialRecord.cs

public class CredentialRecord : RecordBase
{
    public CredentialRecord();

    public string CredentialDefinitionId { get; set; }
    public IEnumerable<CredentialPreviewAttribute> CredentialAttributesValues { get; set; }
    public string SchemaId { get; set; }
    public string ConnectionId { get; set; }
    public string CredentialId { get; set; }
    public CredentialState State { get; set; }
}

I’m able to retrieve the SchemaID, CredentialId e.t.c, except public IEnumerable<CredentialPreviewAttribute> CredentialAttributesValues { get; set; }, in which the Name, PassportNumber e.t.c is stored.

public class CredentialPreviewAttribute
{
    public CredentialPreviewAttribute();
    public CredentialPreviewAttribute(string name, string value);

    public string Name { get; set; }
    public string MimeType { get; set; }
    public object Value { get; set; }
}

UPDATE

I run the foreach loop, but it only returned the first item value.

CredentialPage.xml

        <ListView
            SeparatorVisibility="None"
            BackgroundColor="#004B86"
            ItemsSource="{Binding CredentialAttributes}"
            HasUnevenRows="true">
        </ListView>

CredentialViewModel.cs

namespace Osma.Mobile.App.ViewModels.Credentials
{
    public class CredentialViewModel : ABaseViewModel
    {
        private readonly CredentialRecord _credential;
        private readonly ICredentialService _credentialService;

        public CredentialViewModel(
            ICredentialService credentialService,
            CredentialRecord credential
        ) : base(
            nameof(CredentialViewModel)
        )
        {
            _credential = credential;

            // Loop to Preview the Credentials
            foreach (var item in _credential.CredentialAttributesValues)
            {
                _credentialAttributes = item.Value.ToString();
            }
        }

        #region Bindable Properties
        private string _credentialAttributes;
        public string CredentialAttributes
        {
            get => _credentialAttributes;
            set => this.RaiseAndSetIfChanged(ref _credentialAttributes, value);
        }
        #endregion
    }
}

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You are binding to a string, that’s why you only get the one item, try using an ObservableCollection instead:

public class CredentialViewModel : ABaseViewModel
{
    private readonly CredentialRecord _credential;
    private readonly ICredentialService _credentialService;

    public CredentialViewModel(
        ICredentialService credentialService,
        CredentialRecord credential
    ) : base(
        nameof(CredentialViewModel)
    )
    {
        _credential = credential;

        // Loop to Preview the Credentials
        foreach (var item in _credential.CredentialAttributesValues)
        {
            _credentialAttributes.Add(item.Value.ToString());
        }
    }

    private ObservableCollection<string> _credentialAttributes = new ObservableCollection<string>();
    public ObservableCollection<string> CredentialAttributes
    {
        get
        {
            return _credentialAttributes;
        }
        set
        {
            this.RaiseAndSetIfChanged(ref _credentialAttributes, value);
        }
    }
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x