簡體   English   中英

如何在xamarin.forms條目中存儲占位符值

[英]How to store placeholder value in xamarin.forms entry

我希望這樣做,這樣我的用戶不必每次在xamarin.forms應用程序上填寫此表單時都輸入wifi憑據。 我試圖找到一種方法,可以存儲用戶上次輸入到條目中的值。 有什么建議么?

我以為如果不為null,將其存儲到字段中可能很容易,但是我收到一條錯誤消息,指出“只有賦值,調用,遞增,遞減,等待和新的對象表達式才可以用作語句”

在我班的頂部,我有領域。

string WifiNamePlaceholder;

然后在頁面的后面輸入

if (WifiNamePlaceholder == null)
{
   WifiNamePlaceholder = "Enter in your wifi credentials"
}

wifi_name = new Xamarin.Forms.Entry
{
   BackgroundColor = Color.White,
   Placeholder = WifiNamePlaceholder,
   Margin = new Thickness(0, 0, 10, 0),
   FontFamily = Fonts.HelveticaNeueBold,
   FontSize = labelSize
}

//Attempt to set new value here but get error
//Doing this in hopes it can save their entry for next time.

wifi_name.Placeholder == WifiNamePlaceholder;

If you have any suggestions it would be greatly appreciated! Thank you!

Entry.Placeholder實際上僅對空的Entry有用。 Entry.Text為空時顯示的只是參考性文本。 因此,一旦用戶輸入了一個值,您將在下次顯示該頁面時設置Entry.Text

通常,您應該花一些時間熟悉Xamarin.Forms和MVVM中數據綁定的工作方式。

使用Xamarin.Essentials.Preferences或Xamarin.Essentials.SecureStorage。 它們易於使用,不需要很多開銷。

Xamarin.Essentials.Preferences.Get("Key", default(string));
Xamarin.Essentials.Preferences.Set("Key", "stringToSave")

然后只需使用存儲的值設置Entry.Text 請參閱文檔: Xamarin.Essentials.Preferences

您可以將其保存在用戶電話的本地數據庫中,然后使用從數據庫中獲取的值來填充占位符

嘗試以下鏈接: https : //docs.microsoft.com/zh-cn/xamarin/xamarin-forms/data-cloud/data/databases

嘗試將其保存在數據庫或xml文件中。 這樣,您可以在整個應用程序中使用它。

如果您使用的是api調用,則嘗試將該密碼保存在后端中,以便您可以在用戶打開應用程序時通過調用該api來獲取密碼。

否則,只需將密碼保存為共享的首選項,這樣即使應用程序按照前面的答案中的說明關閉,密碼也將保留。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM