Не получается добавить значение в список
Создаю приложение список дел. Приложение из двух страниц. Первая страница - сам список. Вторая страница - страница ввода значения. Но когда ввожу значение оно не появляется на главном экране.
//код с первой страницы
public partial class MainPage : ContentPage
{
private DotoListPage _todoListPage;
public MainPage()
{
InitializeComponent();
_todoListPage = new DotoListPage();
BindingContext = _todoListPage;
}
private async void OnAddClicked(object sender, EventArgs e)
{
await Navigation.PushAsync(new DotoListPage());
}
}
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyTodoList.MainPage"
Title="Заметки">
<StackLayout>
<ListView ItemsSource="{Binding TodoList}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Padding="16,12" Orientation="Horizontal">
<CheckBox IsChecked="{Binding Done}"
Color="Gray"/>
<Label Text="{Binding Text}"
TextColor="Black"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Button Text="+"
TextColor="Black"
HorizontalOptions="Center"
VerticalOptions="End"
Margin="15"
Clicked="OnAddClicked">
</Button>
</StackLayout>
//код со второй страницы
public partial class DotoListPage : ContentPage
{
private ObservableCollection<TodoModel> _todoList;
public ObservableCollection<TodoModel> TodoList
{
get { return _todoList; }
set { _todoList = value; }
}
public DotoListPage()
{
InitializeComponent();
TodoList = new ObservableCollection<TodoModel>();
TodoList.Add(new TodoModel
{
Text = "test",
Done = false
});//Тестовый текст, он выводится на первой странице
}
private async void OnSaveClicked(object sender, EventArgs e)
{
string result = placeholdetText.Text;
AddItem(result);
}
private void AddItem(string text)
{
TodoList.Add(new TodoModel
{
Text = text,
Done = false
});
}
}
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyTodoList.DotoListPage"
Title="Добавление заметки">
<ContentPage.Content>
<StackLayout Margin="15">
<Entry x:Name="placeholdetText"
Placeholder="Enter your text"
HeightRequest="50"/>
<Grid>
<Button Text="Save"
Clicked="OnSaveClicked"/>
</Grid>
<Label x:Name="labelText" />
</StackLayout>
</ContentPage.Content>
Ответы (1 шт):
Автор решения: aepot
→ Ссылка
await Navigation.PushAsync(new DotoListPage()); вы каждый раз при переходе на страницу создаете ее заново.
Надо так
await Navigation.PushAsync(_todoListPage);