Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
4,919 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
> Model Class
public class Contacts
{
[PrimaryKey] [AutoIncrement]
public int Contact_ID { get; set; }
public string Contact_Name { get; set; }
public string Contact_Address { get; set; }
public string Contact_eMail { get; set; }
public string Contact_Mobile { get; set; }
public string Contact_DOB { get; set; }
public string Contact_Designation { get; set; }
public string Contact_JoiningDate { get; set; }
public string Contact_MaritalStatus { get; set; }
public string Contact_Method { get; set; }
public string Value { get; set; }
public int Key { get; set; }
public string ContactText { get; set;}
}
XAML Page
<StackLayout>
<Label x:Name="LblDisplay" Text="Select a Field" FontSize="Medium" TextColor="Blue" />
<Picker Title="Select --" ItemsSource="{Binding ListContacts}" ItemDisplayBinding="{Binding Value}" SelectedItem="{Binding SelectedContact}" />
<ListView x:Name="ContactList" HasUnevenRows="True" IsVisible="True">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Vertical" VerticalOptions="Center" >
<Label Text="{Binding Contact_Name}" TextColor="OrangeRed" />
<Label x:Name="LblField" Text="{Binding ContactText}" TextColor="OrangeRed" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
Code behind
public partial class Page2 : ContentPage
{
readonly SQLiteAsyncConnection _connection
= DependencyService.Get<ISQLite>().GetConnection();
public ObservableCollection<Contacts> CList { get; set; }
public Page2()
{
BindingContext = new Page2ViewModel();
InitializeComponent();
}
protected override void OnAppearing()
{
base.OnAppearing();
_connection.CreateTableAsync<Contacts>();
var list = _connection.Table<Contacts>().ToListAsync().Result;
CList = new ObservableCollection<Contacts>(list);
ContactList.ItemsSource = CList;
}
}
ViewModel
public class Page2ViewModel : BaseViewModel
{
public List<Contacts> ListContacts
{
get;
set;
}
public Page2ViewModel()
{
ListContacts = FieldPicker.GetContacts().OrderBy(c => c.Value).ToList();
}
private Contacts _selectedContact;
public Contacts SelectedContact
{
get
{
return _selectedContact;
}
set
{
SetProperty(ref _selectedContact, value);
ContactText = _selectedContact.Value; //"Contacts : " +
}
}
private string _contactText;
public string ContactText
{
get
{
return _contactText;
}
set
{
SetProperty(ref _contactText, value);
}
}
}
public class BaseViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
protected bool SetProperty<T>(ref T backField, T value,
[CallerMemberName] string propertyName = null)
{
if (EqualityComparer<T>.Default.Equals(backField, value))
{
return false;
}
backField = value;
OnPropertyChanged(propertyName);
return true;
}
}
Services Class
public class FieldPicker
{
public static List<Contacts> GetContacts()
{
var contacts = new List<Contacts>()
{
new Contacts() {Key = 1, Value = "Contact_Address"},
new Contacts() {Key = 2, Value = "Contact_eMail"},
new Contacts() {Key = 3, Value = "Contact_Mobile"},
};
return contacts;
}
}
Hi anonymous usere-6527 , I will check it and come back asap.
Hi @JessieZhang-MSFT , thank you so much for your time!
Sorry, I don't quite follow you, could you please elaborate on your requirement in detail?
Hi @JessieZhang-MSFT , there are 2 Labels on XAML Page, one displaying Contact_Name and 2nd I would like to display one of the field out of 8 given field at runtime (Contact_Address To Contact_Method) currently trying only with 3 fields Contact_Address, Contact_eMail. and Contact_Mobile, because unable to display these on the 2nd Label, if there is a way to select and display, then would like to retain the value on a different page while go there
Hi @JessieZhang-MSFT , Thank you so much for your valuable time to help me, your code displaying the 1st Label on the list but again 2nd Label not displaying by selecting any field, do I need to change Binding? or any other change?
Hi anonymous usere-6527 ,I'm not quite sure what you mean. When you select an item from the spinner (e.g. 'Contact_eMail'), do you want to display the value of field 'Contact_eMail' of the item(
Contacts
) of Listview on the second label of the list view?Hi @JessieZhang-MSFT , I want to display the value of Field as mentioned earlier, but unfortunately it showing the result, with only one Label (Contact_Name), not the field picked for second Label as you can see in the picture, thanks!
According to the image you posted, do you want to display the value of field
Contact_Address
to the second label after you select the field(Contact_Address
) of the Picker, right?Hi @JessieZhang-MSFT , just Contact_Address display is not a problem at all, as I mentioned I would like to display one field whichever user pick out of 8 at runtime on 2nd Label, and want to keep the that same field to display on another page when navigating. (Mobile, DOB, Designation, eMail, etc) thank you again!
Sorry, I really don't understand what you mean. What do you mean by words
pick out of 8 at runtime on 2nd Label
? And which field do you want to keep the same when navigating? Based on the code you posted and your description, I can only guess what you want to achieve.You can specify which step went wrong and which lines of code were involved.
Best Regards!
Hi @JessieZhang-MSFT , I am really sorry for unable to provide you the enough information, I thought you understood as you wrote the code to display one field at a time to display on 2nd Label, but unfortunately its not working at my end as I showed in both pictures, obviously I want to keep the same field which is displaying on 2nd Label aft while user select it to display. Thank you for your time to help me out but it didn't work. thanks again!
Here is the link for the project on GitHub https://github.com/EmDe-NJ/AppContacts
Sign in to comment
Hello,
Welcome to our Microsoft Q&A platform!
Do you want to achieve the following function?If yes, you can try the following steps:
To make the code structure clearer, we can create a new class for the picker (
DataType
):DataType.cs
And a enum
Type
for different types of contacts (Contact_Type
):FieldPicker.cs
Contacts.cs
Page2ViewModel.cs
MainPage.xaml
Best Regards,
Jessie Zhang
---
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Hello @JessieZhang-MSFT , Thank you so much for your time and valuable help, really appreciated!
Sign in to comment
1 additional answer
Sort by: Most helpful
Hello,
Welcome to our Microsoft Q&A platform!
Do you want to make field
Value
of theSelectedContact
to display in the second Label of all the listview items when you select the picker?If yes, you can do as follows:
Contacts
extends classBaseViewModel
, and do like this: public class Contacts:BaseViewModel{
public int Contact_ID { get; set; }
public string Contact_Name { get; set; } // public string ContactText { get; set; } }
Page2ViewModel
as follows:FieldPicker
as follows: public class FieldPicker{
public static List<Contacts> GetContacts()
{
var contacts = new List<Contacts>()
{
new Contacts() {Key = 1, Value = "Contact_Address", Contact_Name="Test_Name1",Contact_Address ="address1",Contact_eMail="testemail1@gmail.com",Contact_Mobile="Phone1:111111",ContactText="test1"},
new Contacts() {Key = 2, Value = "Contact_eMail", Contact_Name="Test_Name2",Contact_Address ="address2",Contact_eMail="testemail2@gmail.com",Contact_Mobile="Phone1:222222",ContactText="test2"},
new Contacts() {Key = 3, Value = "Contact_Mobile", Contact_Name="Test_Name3",Contact_Address ="address3",Contact_eMail="testemail3@gmail.com",Contact_Mobile="Phone1:333333",ContactText="test3"},
};
return contacts;
}
}
The result is:
Best Regards,
Jessie Zhang
---
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Sign in to comment
Activity