【发布时间】:2020-06-04 09:13:23
【问题描述】:
我希望有人可以帮助我了解它是如何工作的.. 我在 Xamarin Forms 中有一个项目
我有一个page1,下面有代码
Page1.xaml
<StackLayout BindableLayout.ItemsSource="{Binding List1}">
<BindableLayout.ItemTemplate>
<DataTemplate>
<AbsoluteLayout>
<Button Text="{Binding NameP} Clicked="Button_Clicked"/>
</AbsoluteLayout>
</DataTemplate>
</BindableLayout.ItemTemplate>
</StackLayout>
Page1.cs
using System.Collections.Generic;
using Xamarin.Forms;
namespace app.Views
{
public partial class Page1 : ContentPage
{
public Page1()
{
InitializeComponent();
ListPrograms1 = new List<Programmes1>();
{
ListPrograms1.Add(new Programmes1() { NameP = "Tomato", Detail = "xxxxx" });
ListPrograms1.Add(new Programmes1() { NameP = "Pepperoni", Detail = "yyyyy" });
}
BindingContext = this;
}
public List<Programmes1> ListPrograms1
{
get; set;
}
public class Programmes1
{
public string NameP { get; set; }
public string Detail { get; set; }
}
public async void Button_Clicked(object sender, System.EventArgs e)
{
await Navigation.PushAsync(new Page2());
}
}
}
在第二页 Page2.xaml
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="app.Views.Page2">
<StackLayout VerticalOptions="Start" HorizontalOptions="Center">
<Label Text={Binding NameP}" />
<Label Text={Binding Detail}" />
</StackLayout>
</ContentPage>
所以当我点击按钮时,我想去Page2并从前一页或另一个Modelpage传递数据。
当您有数据列表并想在多个页面中使用它们时,有什么好方法? 你推荐使用数据库吗?
我的问题与 ListView 类似(当您单击 item 时,您可以转到另一个详细信息页面)但这里我使用的是可绑定布局。
【问题讨论】:
-
您通常使用页面的构造函数将数据传递给页面,就像使用任何 C# 类一样
-
是的,谢谢,我只是不知道该怎么做。
标签: c# xamarin xamarin.forms