我正在Xamarin CrossPlatform中构建GroceryListView应用程序,对此我还很陌生,无法找到传递条目值和更新我的Items listview的方法。
在我的 AddItem.cs
中Add1=NameEntry.Text;
Add2=CountEntry.Text;
layoutListView1.newItem(Name,Count)
new layoutListView1=layoutListView1;
在我的AddItem.xaml
中
使用此代码不会将任何值传递给我的列表视图。我添加页面的另一个提示不同于我的Itemspage(我的listView在哪里)…
非常感谢您
答案 0 :(得分:0)
假设列表在MainPage上,而Entrys在第1页上。
然后您可以尝试以下代码:
MainPage.xaml:
<StackLayout Orientation="Vertical">
<Button Text="Add Item" Clicked="Button_Clicked" />
<ListView x:Name="MyListView">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout BackgroundColor="#eee"
Orientation="Vertical">
<StackLayout Orientation="Horizontal">
<Label Text="{Binding Name}" />
<Label Text="{Binding Count}"/>
</StackLayout>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
page1.xaml:
<StackLayout Orientation="Vertical">
<Entry Text="new name" x:Name="NameEntry"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand" />
<Entry Text="new count" x:Name="CountEntry"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand" />
<Button Text="Add" Clicked="Button_Clicked" />
</StackLayout>
MainPage.xamal.cs:
public partial class MainPage : ContentPage
{
ObservableCollection<Employee> employees = new ObservableCollection<Employee>();
public MainPage()
{
InitializeComponent();
MyListView.ItemsSource = employees;
MessagingCenter.Subscribe<Object, Employee>(this, "add_click", (sender, arg) => {
employees.Add(arg);
});
}
async void Button_Clicked(object sender, EventArgs e)
{
await Navigation.PushAsync(new page1());
}
}
page1.xaml.cs:
public partial class page1 : ContentPage
{
public page1 ()
{
InitializeComponent ();
}
async void Button_Clicked(object sender, EventArgs e)
{
Employee employee = new Employee();
employee.Name = NameEntry.Text;
employee.Count = CountEntry.Text;
MessagingCenter.Send<Object, Employee>(this, "add_click", employee);
await Navigation.PopAsync();
}
您可以在github上获得演示。