[英]Reload page after button click on another page - Xamarin
I have 2 pages, one where I insert data into a Firebase database, let's call it Page2 and one where I display the Firebase Data, Page1.我有 2 个页面,一个是我将数据插入 Firebase 数据库的地方,我们称之为第 2 页,另一个是我显示 Firebase 数据的地方,第 1 页。
I have a button click argument on Page2 where I insert data into the database but want to reload Page1 after this.我在 Page2 上有一个按钮单击参数,我将数据插入数据库,但之后想重新加载 Page1。 It is structured like this (Page2.xaml.cs):
它的结构如下(Page2.xaml.cs):
void btInsert_Clicked(System.Object sender, System.EventArgs e)
{
//Firebase connection
//Firebase data insertion
//Code to reload Page1
}
On my page 1 I have my database connection and display在我的第 1 页上,我有我的数据库连接和显示
Page1.xaml.cs:页面1.xaml.cs:
public Page1()
{
InitializeComponent();
BindingContext = this;
var collection = firebaseClient
.Child("Childname")
.AsObservable<MyDatabaseRecord>()
.Subscribe((dbevent) =>
{
if (dbevent != null)
{
DatabaseItems.Add(dbevent.Object);
}
});
}
Page1.xaml:第1.xaml页:
<StackLayout Margin="15,0,15,5" BackgroundColor="Navy">
<CollectionView ItemsSource="{Binding DatabaseItems}">
<CollectionView.ItemTemplate>
<DataTemplate>
<StackLayout>
<Frame BackgroundColor="Blue" HorizontalOptions="Center" Margin="0,0,0,17" Padding="2" WidthRequest="350" CornerRadius="20">
<StackLayout BackgroundColor="Blue">
<Label Text="{Binding Data1}" TextColor="White" FontAttributes="Bold" HorizontalOptions="Center" Margin="0, 5, 0, 0" FontSize="20"/>
<Label Text="{Binding Data2}" TextColor="White" Margin="10, 0, 0, 0" FontSize="15"/>
<Label Text="{Binding Data3}" TextColor="White" Margin="10, 0, 0, 0" FontSize="15"/>
<Label Text="{Binding Data4}" TextColor="White" Margin="10, 0, 0, 0" FontSize="15"/>
<Label Text="{Binding Data5}" TextColor="White" Margin="10, 0, 0, 20" FontSize="15"/>
</StackLayout>
</Frame>
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</StackLayout>
How can I approach this?我该如何处理?
Thanks in advance.提前致谢。
There are two ways to solve this problem:有两种方法可以解决这个问题:
Using MessagingCenter : You can use MessagingCenter.Send<MainPage>(this, "Hi");
使用MessagingCenter :您可以使用
MessagingCenter.Send<MainPage>(this, "Hi");
to send.发送。
Then use below code to receive information:然后使用以下代码接收信息:
MessagingCenter.Subscribe<MainPage> (this, "Hi", (sender) => { // Do something whenever the "Hi" message is received });
More information about MessagingCenter can be found here: https://learn.microsoft.com/en-us/xamarin/xamarin.forms/app-fundamentals/messaging-center有关MessagingCenter的更多信息,请访问: https://learn.microsoft.com/en-us/xamarin/xamarin.forms/app-fundamentals/messaging-center
You can pass the collection as a parameter when the two pages are jumped and then modify the bound collection.可以在两个页面跳转时将集合作为参数传入,然后修改绑定的集合。
For arrays of data you bind, it is recommended that you use ObservableCollection to dynamically update data.对于你绑定的arrays条数据,建议你使用ObservableCollection来动态更新数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.