我有一個彈出視窗,用戶需要在其中輸入一些資訊:
<pages:PopupPage xmlns="http://xamarin.com/schemas/2014/forms"
. . .
<StackLayout>
<Label Text="Enter name" />
<Entry FontSize="20"
Placeholder=" Some_Thing" />
</StackLayout>
<StackLayout>
<Label Text="Enter price" />
<Entry FontSize="20"
Placeholder="250" />
</StackLayout>
<Button BackgroundColor="DodgerBlue"
FontSize="30"
Text="Save"
TextColor="White"
Clicked="new_product"
/>
</StackLayout>
</pages:PopupPage>
彈窗呼叫如下
private void ShowNewUserPopup(object o, EventArgs e)
{
PopupNavigation.Instance.PushAsync(new NewUserPopupPage());
}
但是我有一個問題。如何從彈出視窗中獲取輸入的資料?在此之前,我嘗試了 DisplayPromptAsync 選項,一切都解決了。但是這次我應該為“結果”變數分配什么?但由于這是我第一次使用彈出視窗,我一點也不擅長,所以我將非常感謝您的幫助:
async void new_product(object sender, EventArgs e)
{
string result = await DisplayPromptAsync("New product", "name", "add", "cancel");
if (result != null)
{
if (result.Length != 0)
{
await App.Database_Product.Create(new Database.Product(result, 100));
name_colec.ItemsSource = await App.Database_Product.GetAsync();
}
else
{
await App.Database_Product.Create(new Database.Product());
name_colec.ItemsSource = await App.Database_Product.GetAsync();
}
}
}
uj5u.com熱心網友回復:
您可以通過頁面建構式傳遞值。例如,當您想要傳遞您在彈出頁面中輸入的值并在導航時將其顯示在另一個頁面中時,頁面建構式是一種簡單的方法。
在彈出頁面中設定條目的名稱:
<StackLayout>
<StackLayout>
<Label Text="Enter name" />
<Entry x:Name="popup_Name" FontSize="20"
Placeholder="Some_Thing" />
</StackLayout>
<StackLayout>
<Label Text="Enter price" />
<Entry x:Name="popup_Price" FontSize="20"
Placeholder="250" />
</StackLayout>
<Button BackgroundColor="DodgerBlue"
FontSize="30"
Text="Save"
TextColor="White"
Clicked="new_product"
/>
</StackLayout>
設定要導航的頁面的建構式:
public Page2(string name, string price)
{
InitializeComponent();
Name.Text = name;//Name if the Label name of Page2
Price.Text = price;
}
當您離開彈出頁面到另一個頁面時將值傳遞。我使用 ContentPage 例如:
private async void new_product(object sender, EventArgs e)
{
await Navigation.PushAsync(new Page2(popup_Name.Text, popup_Price.Text));
await PopupNavigation.Instance.PopAllAsync();
}
更新:
private async void new_product(object sender, EventArgs e)
{
if (popup_Name.Text!= null)
{
await App.Database_Product.Create(new Database.Product(popup_Name.Text, 100));
name_colec.ItemsSource = await App.Database_Product.GetAsync();
}
else
{
await App.Database_Product.Create(new Database.Product());
name_colec.ItemsSource = await App.Database_Product.GetAsync();
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/459772.html
