'How to create a button clicked EventHandler in Xamarin.Forms with code-behind?
I want to allow a user to click on an Add Term button on my HomePage to create a new term page. I want a button to be created for navigating to that term. I have created the button to add terms but I can't figure out how to give the newly created button an EventHandler for navigation. This is what I have so far:
private string crrntTerm;
private List<Term> termList;
private void addTermBtnForTest()
{
Term termTest = new Term()
{
TermTitle = "Term 1",
CreateDate = System.DateTime.Now
};
using (SQLiteConnection conn = new SQLiteConnection(App.FilePath))
{
conn.CreateTable<Term>();
conn.Insert(termTest);
crrntTerm = termTest.TermID.ToString();
termList = conn.Table<Term>().ToList();
}
Button testBtn = new Button()
{
TextColor = Color.Black,
FontAttributes = FontAttributes.Bold,
FontSize = 20,
Margin = 30,
BackgroundColor = Color.White,
Clicked += GoToTermButton_Clicked //Error: Invalid initializer member declarator
};
testBtn.BindingContext = termTest;
testBtn.SetBinding(Button.TextProperty, "TermTitle");
layout.Children.Add(testBtn);
}//end addTermBtnForTest
private async void GoToTermButton_Clicked(object sender, EventArgs e)
{
// Code to go to appropriate page
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
