TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Sreedevi v
NA
166
5.2k
Save Edit Update Xamarin
Mar 13 2019 11:20 PM
I have 2 pages one is master other detail page.
detailpage.xaml
<
ContentPage.ToolbarItems
>
<
ToolbarItem
Icon
=
"plus.png"
Text
=
"Add"
Activated
=
"OnAdd"
Order
=
"Primary"
/>
</
ContentPage.ToolbarItems
>
<
ListView
x:Name
=
"iList"
ItemSelected
=
"IList_ItemSelected"
>
<
ListView.ItemTemplate
>
<
DataTemplate
>
<
TextCell
Text
=
"{Binding ExpenseCategory}"
Detail
=
"{Binding ExId}"
>
<
TextCell.ContextActions
>
<
MenuItem
Text
=
"Delete"
IsDestructive
=
"True"
Clicked
=
"MenuItem_Clicked"
CommandParameter
=
"{Binding .}"
/>
</
TextCell.ContextActions
>
</
TextCell
>
</
DataTemplate
>
</
ListView.ItemTemplate
>
</
ListView
>
detailpage.xaml.cs
public
event
EventHandler<Expense> ExpenseAdded;
public
event
EventHandler<Expense> ExpenseUpdated;
private
SQLiteAsyncConnection _connection;
public
ExpenseDetail (Expense addexpense)
{
if
(addexpense ==
null
)
throw
new
ArgumentNullException(nameof(addexpense));
InitializeComponent();
_connection = DependencyService.Get<ISQLiteDb>().GetConnection();
BindingContext =
new
Expense
{
ExId = addexpense.ExId,
ExpenseCategory = addexpense.ExpenseCategory,
Amount=addexpense.Amount,
ItemName=addexpense.ItemName,
Exdate=addexpense.Exdate
};
}
protected
async
override
void
OnAppearing()
{
var pick = await _connection.Table<ExpenseCatMaster>().ToListAsync();
expenseList.ItemsSource = pick;
expenseList.ItemDisplayBinding =
new
Binding(
"ExCategory"
);
base
.OnAppearing();
}
private
async
void
OnSave(
object
sender, EventArgs e)
{
var expense =
new
Expense
{
ExpenseCategory = expenseList.Items[expenseList.SelectedIndex].ToString(),
SelectedIndex = expenseList.SelectedIndex,
// SubCategory= subList.Items[subList.SelectedIndex].ToString(),
ItemName=itemName.Text,
Amount = Convert.ToDecimal(amount.Text),
Exdate=datepick.Date
};
await _connection.InsertAsync(expense);
await Navigation.PopAsync();
}
after saving the detils .. it will appear in master page
when i click the iteam we want to change ie,In edit mode , picker doesnot display the selected item
and now when i click save button i ahve to update existing data . Dont create new fields in table.
masterpage.xaml
<
ContentPage.ToolbarItems
>
<
ToolbarItem
Icon
=
"plus.png"
Text
=
"Add"
Activated
=
"ToolbarItem_Activated"
Order
=
"Primary"
/>
</
ContentPage.ToolbarItems
>
<
ListView
x:Name
=
"iList"
ItemSelected
=
"ListView_ItemSelected"
>
<
ListView.ItemTemplate
>
<
DataTemplate
>
<
TextCell
Text
=
"{Binding Amount}"
Detail
=
"{Binding InId}"
>
<
TextCell.ContextActions
>
<
MenuItem
Text
=
"Delete"
IsDestructive
=
"True"
Clicked
=
"MenuItem_Clicked"
CommandParameter
=
"{Binding .}"
/>
</
TextCell.ContextActions
>
</
TextCell
>
</
DataTemplate
>
</
ListView.ItemTemplate
>
</
ListView
>
masterpage.xaml.cs
public
partial
class
AddExpense : ContentPage
{
private
ObservableCollection<Expense> _income;
private
SQLiteAsyncConnection _connection;
private
bool
_isDataLoaded;
public
AddExpense ()
{
InitializeComponent ();
_connection = DependencyService.Get<ISQLiteDb>().GetConnection();
}
protected
async
override
void
OnAppearing()
{
if
(_isDataLoaded)
return
;
_isDataLoaded =
true
;
await LoadData();
base
.OnAppearing();
}
private
async Task LoadData()
{
await _connection.CreateTableAsync<Expense>();
var incomeTab = await _connection.Table<Expense>().ToListAsync();
_income =
new
ObservableCollection<Expense>(incomeTab);
iList.ItemsSource = _income;
}
private
async
void
OnAdd(
object
sender, EventArgs e)
{
var page =
new
ExpenseDetail(
new
Expense());
page.ExpenseAdded += (source, expense) =>
{
_income.Add(expense);
};
await Navigation.PushAsync(page);
}
private
async
void
IList_ItemSelected(
object
sender, SelectedItemChangedEventArgs e)
{
if
(iList.SelectedItem ==
null
)
return
;
var selectIncome = e.SelectedItem
as
Expense;
iList.SelectedItem =
null
;
var page =
new
ExpenseDetail(selectIncome);
//page.ExpenseUpdated += (source, expense) =>
//{
// selectIncome.ExId = expense.ExId;
// selectIncome.ExpenseCategory = expense.ExpenseCategory;
// selectIncome.ItemName = expense.ItemName;
// selectIncome.Exdate = expense.Exdate;
// selectIncome.Amount = expense.Amount;
//};
await Navigation.PushAsync(page);
}
private
async
void
MenuItem_Clicked(
object
sender, EventArgs e)
{
var income = (sender
as
MenuItem).CommandParameter
as
Expense;
if
(await DisplayAlert(
"Warning"
, $
"Are you sure you want to delete {income.ExpenseCategory}?"
,
"differentiate
{
_income.Remove(income);
await _connection.DeleteAsync(income);
}
}
}
Please Help. How will i differenciate update and save
Reply
Answers (
0
)
Xamarin Simple MasterDetail Page
login with sqlite in xamarin forms