Xamarin Forms "...DisplayAlert does not exist in the current context." - c#

I'm working with Xamarin, still new to it, but I'm having a problem that I get the feeling I shouldn't be. Here's my problem:
using System;
using Xamarin.Forms;
namespace DataBinding_Lists
{
public class App
{
public static Page GetMainPage ()
{
var listView = new ListView { RowHeight = 40 };
listView.ItemsSource = new Person []
{
new Person { FirstName = "Abe", LastName = "Lincoln" },
new Person { FirstName = "Groucho", LastName = "Marks" },
new Person { FirstName = "Carl", LastName = "Marks" },
};
listView.ItemTemplate = new DataTemplate(typeof(TextCell));
listView.ItemTemplate.SetBinding(TextCell.TextProperty, "FirstName");
listView.ItemSelected += async (sender, e) => {
await DisplayAlert ("Tapped!", e.SelectedItem + " was tapped.", "OK", "");
};
return new ContentPage {
Content = new StackLayout
{
Padding = new Thickness (5,20,5,5),
Spacing = 10,
Children = { listView }
}
};
}
}
}
Obviously, I've got a class on another page called "Person." This class has two properties: "FirstName" and "LastName." When I try and put all of this together like so in Xamarin, I get the error saying: "The name 'DisplayAlert' does not exist in the current context."

or just try to use :
await App.Current.MainPage.DisplayAlert("Alert", "your message", "OK");

There are two ways to solve this and I lean toward the second one. Close to what Edward L. has said.
DisplayAlert is a method on a Xamarin.Forms Page... and you are inside of a static method that is returning that page, so you have no reference to it.
So you could do this:
using System;
using Xamarin.Forms;
namespace DataBinding_Lists
{
public class App
{
private static Page page;
public static Page GetMainPage ()
{
var listView = new ListView { RowHeight = 40 };
listView.ItemsSource = new Person []
{
new Person { FirstName = "Abe", LastName = "Lincoln" },
new Person { FirstName = "Groucho", LastName = "Marks" },
new Person { FirstName = "Carl", LastName = "Marks" },
};
listView.ItemTemplate = new DataTemplate(typeof(TextCell));
listView.ItemTemplate.SetBinding(TextCell.TextProperty, "FirstName");
listView.ItemSelected += async (sender, e) => {
await page.DisplayAlert ("Tapped!", e.SelectedItem + " was tapped.", "OK", "");
};
page = new ContentPage {
Content = new StackLayout
{
Padding = new Thickness (5,20,5,5),
Spacing = 10,
Children = { listView }
}
};
return page;
}
}
}
What you should really do is create a new class that is your page.
Your App.cs turns into this:
using System;
using Xamarin.Forms;
namespace DataBinding_Lists
{
public class App
{
public static Page GetMainPage ()
{
return new PeoplePage();
}
}
}
Then you create a new class that inherits from Page:
using System;
using Xamarin.Forms;
namespace DataBinding_Lists
{
public class PeoplePage : Page
{
public PeoplePage()
{
var listView = new ListView { RowHeight = 40 };
listView.ItemsSource = new Person []
{
new Person { FirstName = "Abe", LastName = "Lincoln" },
new Person { FirstName = "Groucho", LastName = "Marks" },
new Person { FirstName = "Carl", LastName = "Marks" },
};
listView.ItemTemplate = new DataTemplate(typeof(TextCell));
listView.ItemTemplate.SetBinding(TextCell.TextProperty, "FirstName");
listView.ItemSelected += async (sender, e) => {
await DisplayAlert ("Tapped!", e.SelectedItem + " was tapped.", "OK", "");
};
Content = new ContentPage {
Content = new StackLayout
{
Padding = new Thickness (5,20,5,5),
Spacing = 10,
Children = { listView }
}
};
}
}
}

DisplayAlert() is a method of the Page class.
In order for your class to be able to use it, it needs to either instantiate a Page object and invoke it using that object or directly inherit from it.
Since you stated that your Person class is actually also a Page class, you could also invoke it using one of your Person objects i.e. personObj.DisplayAlert(...)
Perhaps something similar to the following:
var personObj = new Person();
personObj.DisplayAlert ("Tapped!", e.SelectedItem + " was tapped.", "OK", "");
This of course, assumes that your Person class declaration goes something like the following:
public class Person : Page
{
...
}
Obviously, the exact implementation will depend on how you structure your code. I am just going by what I can see in your question and assuming a few things.

Related

IOS contact postal address in Xamarin.Forms

I am writing a C# app in a Xamarin.Forms project that displays a contact name, and street address. I am having trouble pulling the address from the CNContact and assigning the contacts address to a string.
Its going to be something obvious, but i'm stuck!
public List<Contact> GetContacts()
{
contactList = new List<Contact>();
var store = new Contacts.CNContactStore();
var ContainerId = new CNContactStore().DefaultContainerIdentifier;
var predicate = CNContact.GetPredicateForContactsInContainer(ContainerId);
var fetchKeys = new NSString[] { CNContactKey.Identifier, CNContactKey.GivenName, CNContactKey.FamilyName, CNContactKey.Birthday, CNContactKey.PostalAddresses, CNContactKey.ImageData };
NSError error;
var IPhoneContacts = store.GetUnifiedContacts(predicate, fetchKeys, out error);
foreach(var c in IPhoneContacts)
{
var contact = new Contact();
contact.FirstName = c.GivenName;
contact.FamilyName = c.FamilyName;
if(c.PostalAddresses.Length !=0)
{
contact.StreetAddress = CNPostalAddressFormatter.GetStringFrom(c.PostalAddresses, CNPostalAddressFormatterStyle.MailingAddress);
};
contactList.Add(contact);
}
return contactList;
}
The problem is that CNPostalAddressFormatter.GetStringFrom() method expects a single CNPostalAddress object as a parameter but you're passing all addresses of a single contact since the PostalAddresses property is an array of CNLabeledValue<ValueType> objects.
What you should do is iterate over all addresses, or perhaps just take the first one by default. Really depends on what you want to achieve.
For example, this would get the first CNPostalAddress:
contact.StreetAddress = CNPostalAddressFormatter.GetStringFrom(c.PostalAddresses[0].Value, CNPostalAddressFormatterStyle.MailingAddress);
Also, if you want to know the label of the address (Home, Work etc), you can get it like this:
c.PostalAddresses[0].Label
Then the actual CNPostalAddress object is again this:
c.PostalAddresses[0].Value
Fetching Existing Contacts in iOS :
First , you need to add follow permission in Info.plist :
<key>NSContactsUsageDescription</key>
<string>This app requires contacts access to function properly.</string>
Second , you can create a model contains of needs contact info as follow :
public class ContactModel
{
public IList PhoneNumbers { get; set; }
public string GivenName { get; set; }
public string FamilyName { get; set; }
}
Third , create a func to fetch info :
public List<ContactModel> ReadContacts()
{
var response = new List<ContactModel>();
try
{
//We can specify the properties that we need to fetch from contacts
var keysToFetch = new[] {
CNContactKey.PhoneNumbers, CNContactKey.GivenName, CNContactKey.FamilyName,CNContactKey.PostalAddresses,CNContactKey.PhoneNumbers
};
//Get the collections of containers
var containerId = new CNContactStore().DefaultContainerIdentifier;
//Fetch the contacts from containers
using (var predicate = CNContact.GetPredicateForContactsInContainer(containerId))
{
CNContact[] contactList;
using (var store = new CNContactStore())
{
contactList = store.GetUnifiedContacts(predicate, keysToFetch, out
var error);
}
//Assign the contact details to our view model objects
response.AddRange(from item in contactList
where item?.EmailAddresses != null
select new ContactModel
{
PhoneNumbers = item.PhoneNumbers,
PostalAddresses = CNPostalAddressFormatter.GetStringFrom(item.PostalAddresses[0].Value, CNPostalAddressFormatterStyle.MailingAddress),
GivenName = item.GivenName,
FamilyName = item.FamilyName
});
}
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
return response;
}
Fourth , invoke func :
List<ContactModel> contacts = ReadContacts();
ContactModel contactVm;
for (int i = 0; i < contacts.Count; i++)
{
contactVm = contacts[i];
Console.WriteLine("Contact is : " + contactVm.FamilyName);
Console.WriteLine("Contact is : " + contactVm.GivenName);
Console.WriteLine("Contact is : " + contactVm.PostalAddresses);
}
...
Contact is : Taylor
Contact is : David
Contact is : 1747 Steuart Street
Tiburon CA 94920
USA
Fifth , the screenshot as follow :
===================================Udate=====================================
Your code should be modified as follow :
public List<Contact> GetContacts()
{
contactList = new List<Contact>();
var store = new Contacts.CNContactStore();
var ContainerId = new CNContactStore().DefaultContainerIdentifier;
var predicate = CNContact.GetPredicateForContactsInContainer(ContainerId);
var fetchKeys = new NSString[] { CNContactKey.Identifier, CNContactKey.GivenName, CNContactKey.FamilyName, CNContactKey.Birthday, CNContactKey.PostalAddresses, CNContactKey.ImageData };
NSError error;
var IPhoneContacts = store.GetUnifiedContacts(predicate, fetchKeys, out error);
foreach(var c in IPhoneContacts)
{
var contact = new Contact();
contact.FirstName = c.GivenName;
contact.FamilyName = c.FamilyName;
if(c.PostalAddresses.Length !=0)
{
contact.StreetAddress = CNPostalAddressFormatter.GetStringFrom(c.PostalAddresses[0].Value, CNPostalAddressFormatterStyle.MailingAddress);
};
contactList.Add(contact);
}
return contactList;
}
The property postalAddress of Method CNPostalAddressFormatter.GetStringFrom
is a type of object(Contacts.CNPostalAddress) , however c.PostalAddresses is a type of Array.
public static string GetStringFrom (Contacts.CNPostalAddress postalAddress, Contacts.CNPostalAddressFormatterStyle style);

Implementing Xamarin Forms context actions

I am trying to implement context actions on my list in Xamarin Forms but can't get it to work.
I am not using XAML, but instead creating my layout in code.
I am trying to follow the steps in https://developer.xamarin.com/guides/xamarin-forms/user-interface/listview/interactivity/#Context_Actions and I want to push a new page when "Edit" is clicked.
I cleaned up my code and removed my feeble attempts to make things work.
So this is my custom list cell:
public class PickerListCell : TextCell
{
public PickerListCell ()
{
var moreAction = new MenuItem { Text = App.Translate ("Edit") };
moreAction.SetBinding (MenuItem.CommandParameterProperty, new Binding ("."));
moreAction.Clicked += async (sender, e) => {
var mi = ((MenuItem)sender);
var option = (PickerListPage.OptionListItem)mi.CommandParameter;
var recId = new Guid (option.Value);
// This is where I want to call a method declared in my page to be able to push a page to the Navigation stack
};
ContextActions.Add (moreAction);
}
}
And here is my model:
public class OptionListItem
{
public string Caption { get; set; }
public string Value { get; set; }
}
And this is the page:
public class PickerPage : ContentPage
{
ListView listView { get; set; }
public PickerPage (OptionListItem [] items)
{
listView = new ListView () ;
Content = new StackLayout {
Children = { listView }
};
var cell = new DataTemplate (typeof (PickerListCell));
cell.SetBinding (PickerListCell.TextProperty, "Caption");
cell.SetBinding (PickerListCell.CommandParameterProperty, "Value");
listView.ItemTemplate = cell;
listView.ItemsSource = items;
}
// This is the method I want to activate when the context action is called
void OnEditAction (object sender, EventArgs e)
{
var cell = (sender as Xamarin.Forms.MenuItem).BindingContext as PickerListCell;
await Navigation.PushAsync (new RecordEditPage (recId), true);
}
}
As you can see by my comments in the code, I have indicated where I believe things are missing.
Please assist guys!
Thanks!
Probably is too late for you, but can help others. The way i've found to do this is passing the instance of page on creation the ViewCell.
public class PickerListCell : TextCell
{
public PickerListCell (PickerPage myPage)
{
var moreAction = new MenuItem { Text = App.Translate ("Edit") };
moreAction.SetBinding (MenuItem.CommandParameterProperty, new Binding ("."));
moreAction.Clicked += async (sender, e) => {
var mi = ((MenuItem)sender);
var option = (PickerListPage.OptionListItem)mi.CommandParameter;
var recId = new Guid (option.Value);
myPage.OnEditAction();
};
ContextActions.Add (moreAction);
}
}
So, in your page:
public class PickerPage : ContentPage
{
ListView listView { get; set; }
public PickerPage (OptionListItem [] items)
{
listView = new ListView () ;
Content = new StackLayout {
Children = { listView }
};
var cell = new DataTemplate(() => {return new PickerListCell(this); });
cell.SetBinding (PickerListCell.TextProperty, "Caption");
cell.SetBinding (PickerListCell.CommandParameterProperty, "Value");
listView.ItemTemplate = cell;
listView.ItemsSource = items;
}
void OnEditAction (object sender, EventArgs e)
{
var cell = (sender as Xamarin.Forms.MenuItem).BindingContext as PickerListCell;
await Navigation.PushAsync (new RecordEditPage (recId), true);
}
}
Ok, so with the help of some posts, specifically this one https://forums.xamarin.com/discussion/27881/best-practive-mvvm-navigation-when-command-is-not-available, I came to the following solution, though I'm not perfectly satisfied with the way it looks.
My custom cell now announces when a command is being executed using MessagingCenter:
public class PickerListCell : TextCell
{
public PickerListCell ()
{
var moreAction = new MenuItem { Text = App.Translate ("Edit") };
moreAction.SetBinding (MenuItem.CommandParameterProperty, new Binding ("."));
moreAction.Clicked += async (sender, e) => {
var mi = ((MenuItem)sender);
var option = (PickerListPage.OptionListItem)mi.CommandParameter;
var recId = new Guid (option.Value);
// HERE I send a request to open a new page. This looks a
// bit crappy with a magic string. It will be replaced with a constant or enum
MessagingCenter.Send<OptionListItem, Guid> (this, "PushPage", recId);
};
ContextActions.Add (moreAction);
}
}
And in my PickerPage constructor I added this subscription to the Messaging service:
MessagingCenter.Subscribe<OptionListItem, Guid> (this, "PushPage", (sender, recId) => {
Navigation.PushAsync (new RecordEditPage (recId), true);
});
All this works just find, but I'm not sure if this is the way it was intended to. I feel like the binding should be able to solve this without the Messaging Service, but I can't find out how to bind to a method on the page, only to a model, and I don't want to pollute my model with methods that have dependencies on XF.

How to add multiple values to list C#

Hi I have the following code when I am adding values to a list.
var NoLiftingList = new List<SQLFields>();
SQLFields nolifting = new SQLFields();
nolifting.Field1 = "No lifting";
NoLiftingList.Add(nolifting);
SQLFields extremelifting = new SQLFields();
extremelifting.Field1 = "Up to 100 lbs (extreme lifting)";
NoLiftingList.Add(extremelifting);
How can I simplify this? Instead of initializing a new object all the time.
This is the code for the whole class updated below.
Thanks
You can add to a list, and set properties on a class by using this inline constructor syntax (working example):
using System.Collections.Generic;
public class Program
{
public static void Main()
{
var NoLiftingList = new List<SQLFields>
{
new SQLFields
{
Field1 = "No Lifting"
},
new SQLFields
{
Field1 = "Up to 100lbs (extreme lifting)"
}
};
}
}
public class SQLFields
{
public string Field1 { get; set; }
}
Use Object Initializers with anonymous types
var NoLiftingList = new List<SQLFields>(){
new SQLFields() { Field1 = "No lifting"},
new SQLFields() { Field1 = "Up to 100 lbs (extreme lifting)"}
};
Ref: MSDN Link
Try this
var NoLiftingList = new List<SQLFields>()
{
new SQLFields()
{
Field1 = "No lifting"
},
new SQLFields()
{
Field1 = "Up to 100 lbs (extreme lifting)"
}
};

It is possible by using automapper?

I have a set of nested classes from datasource (VSO) and I would like to mapped to contracts. The point of mapping is the next:
the mapped object (contract) will have a new property, called AreaPathFull, which will have the concatenated AreaPath values of all nodes above it in the tree.
Why I want this?:
it is for displaying purposes on UI
it can be solved on UI side by dealing with javascript is pain in the bottom for me, C# much easier, might not be the best idea ever, but still...
Question:
it is possible to achieve this by using Automapper?
Unfortunately, I have almost nothing experience with Automapper and if it is not possible then I would not like to spend my time with it. If so, then I think, CustomValueResolve will be my friend.
Source:
public class Class1ToBeMapped
{
public string AreaPath = "Level1";
public List<Class1ToBeMapped> Children = new List<Class1ToBeMapped>()
{
{new Class1ToBeMapped()
{
AreaPath = "Level21",
Children = new List<Class1ToBeMapped>(){}
}},
{new Class1ToBeMapped()
{
AreaPath = "Level22",
Children = new List<Class1ToBeMapped>(){}
}}
};
}
Mapped object, desired result:
public class Class2Mapped
{
public string AreaPath = "Level1";
public string AreaPathFull = "Level1";
public List<Class2Mapped> Children = new List<Class2Mapped>()
{
{
new Class2Mapped()
{
AreaPath = "Level21",
AreaPathFull = "Level1/Level21",
Children = new List<Class2Mapped>()
{
{
new Class2Mapped()
{
AreaPath = "Level31",
AreaPathFull = "Level1/Level21/Level31",
Children = new List<Class2Mapped>()
{
{
new Class2Mapped()
{
AreaPath = "Level41",
AreaPathFull = "Level1/Level21/Level31/Level41",
Children = null
}
}
}
}
},
{
new Class2Mapped()
{
AreaPath = "Level22",
AreaPathFull = "Level1/Level22",
Children = new List<Class2Mapped>()
{
new Class2Mapped()
{
AreaPath = "Level32",
AreaPathFull = "Level1/Level22/Level32",
Children = null
}
}
}
}
}
},
}
};
}

why GetChanges returns something (when bound to a property) even if there are no changes on datatable?

Bound GetChanges always returns something when bound to a UserControl's property (even on a simple one)
I have made a UserControl, for some reason unbeknownst to me, when I bound a DataColumn to my control's property the dataSet1.GetChanges() always return something, even the column bound to my control was not changed.
What's the possible cause why GetChanges always return something?
Here's a simple snippet to reproduce the binding/GetChanges problem:
using System;
using System.Data;
using System.Windows.Forms;
namespace BindingBug
{
public partial class Form1 : Form
{
DataSet _ds = new DataSet();
void GetData()
{
var t = new DataTable
{
TableName = "beatles",
Columns =
{
{"lastname", typeof(string)},
{"firstname", typeof(string)},
{"middlename", typeof(string)}
}
};
t.Rows.Add("Lennon", "John", "Winston");
t.Rows.Add("McCartney", "James", "Paul");
_ds.Tables.Add(t);
}
public string Hey { set; get; }
public Form1()
{
InitializeComponent();
var tLastname = new TextBox { Top = 100 };
var tFirstname = new TextBox { Top = 130 };
this.Controls.Add(tLastname);
this.Controls.Add(tFirstname);
GetData();
tLastname.DataBindings.Add("Text", _ds.Tables["beatles"], "lastname");
tFirstname.DataBindings.Add("Text", _ds.Tables["beatles"], "firstname");
// if the following line is commented 2nd:Has Changes == false
this.DataBindings.Add("Hey", _ds.Tables["beatles"], "middlename");
_ds.AcceptChanges();
MessageBox.Show("1st:Has Changes = " + _ds.HasChanges().ToString());
var bDetectChanges = new Button { Top = 160, Text = "Detect Changes" };
bDetectChanges.Click +=
delegate
{
this.BindingContext[_ds.Tables["beatles"]].EndCurrentEdit();
MessageBox.Show("2nd:Has Changes = " + (_ds.GetChanges() != null).ToString());
};
this.Controls.Add(bDetectChanges);
}
}
} //namespace BindingBug
i was able to solve the problem today, the key thing is to make the BindingContext's EndCurrentEdit aware if the value really changed. for this, we must implement System.ComponentModel.INotifypropertychanged on our control. i just saw the solution from here: http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.propertychanged.aspx
hope this can help others implementing their own controls which flag wrong change status on GetChanges()
public partial class Form1 : Form, System.ComponentModel.INotifyPropertyChanged
{
//----------- implements INotifyPropertyChanged -----------
// wish C# has this VB.NET's syntactic sugar
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged; // implements INotifyPropertyChanged.PropertyChanged
//----------- start of Form1 ----------
DataSet _ds = new DataSet();
void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
void GetData()
{
var t = new DataTable
{
TableName = "beatles",
Columns =
{
{"lastname", typeof(string)},
{"firstname", typeof(string)},
{"middlename", typeof(string)}
}
};
t.Rows.Add("Lennon", "John", "Winston");
t.Rows.Add("McCartney", "James", "Paul");
t.Columns["middlename"].DefaultValue = "";
_ds.Tables.Add(t);
}
string _hey = "";
public string Hey
{
set
{
if (value != _hey)
{
_hey = value;
NotifyPropertyChanged("Hey");
}
}
get
{
return _hey;
}
}
public Form1()
{
InitializeComponent();
var tLastname = new TextBox { Top = 100 };
var tFirstname = new TextBox { Top = 130 };
this.Controls.Add(tLastname);
this.Controls.Add(tFirstname);
GetData();
tLastname.DataBindings.Add("Text", _ds.Tables["beatles"], "lastname");
tFirstname.DataBindings.Add("Text", _ds.Tables["beatles"], "firstname");
this.DataBindings.Add("Hey", _ds.Tables["beatles"], "middlename");
_ds.AcceptChanges();
MessageBox.Show("1st:Has Changes = " + _ds.HasChanges().ToString());
var bDetectChanges = new Button { Top = 160, Text = "Detect Changes" };
bDetectChanges.Click +=
delegate
{
this.BindingContext[_ds.Tables["beatles"]].EndCurrentEdit();
MessageBox.Show("2nd:Has Changes = " + (_ds.GetChanges() != null).ToString());
};
this.Controls.Add(bDetectChanges);
}
}

Categories

Resources