我有 2 個頁面,一個用于將資料插入 Firebase 資料庫,我們稱之為 Page2,另一個用于顯示 Firebase 資料,即 Page1。
我在 Page2 上有一個按鈕單擊引數,我在其中將資料插入到資料庫中,但在此之后想要重新加載 Page1。它的結構如下(Page2.xaml.cs):
void btInsert_Clicked(System.Object sender, System.EventArgs e)
{
//Firebase connection
//Firebase data insertion
//Code to reload Page1
}
在我的第 1 頁上,我有我的資料庫連接并顯示
Page1.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:
<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>
我該如何處理?
提前致謝。
uj5u.com熱心網友回復:
有兩種方法可以解決這個問題:
使用MessagingCenter:您可以使用
MessagingCenter.Send<MainPage>(this, "Hi");發送。然后使用以下代碼接收資訊:
MessagingCenter.Subscribe<MainPage> (this, "Hi", (sender) => { // Do something whenever the "Hi" message is received });可以在此處找到有關MessagingCenter的更多資訊: https ://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/messaging-center
可以在兩個頁面跳轉時將集合作為引數傳遞,然后修改系結的集合。
對于您系結的資料陣列,建議您使用 ObservableCollection 動態更新資料。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/454207.html
上一篇:iOS將舊專案轉換為使用UI
