简体   繁体   中英

How to store placeholder value in xamarin.forms entry

I would like it so my users do not have to type in wifi credentials everytime they are filling out this form on my xamarin.forms app. I'm trying to figure a way where I can store the value that the user last inputted into an entry. Any suggestions?

I thought it could be as easy as storing it to a field if not null but i get an error stating "Only assignment, call, increment, decrement, await, and new obect expresions can be used as a statement"

At the top of my class I have field.

string WifiNamePlaceholder;

Then later in the page I have my entry

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 is actually only useful for empty Entry's. It's just an informational text that is shown when Entry.Text is empty. So once the user has entered a value you would set Entry.Text the next time the page is shown.

You should really take some time to familiarize yourself with the way data binding works in Xamarin.Forms and MVVM in general.

Use Xamarin.Essentials.Preferences or Xamarin.Essentials.SecureStorage. They're easy to use and don't require a bunch of overhead.

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

Then just set your Entry.Text using the stored value. See documentation: Xamarin.Essentials.Preferences

You could save it in a local database on the user's phone and then fill the placeholder with the value you get from the database

try this link: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/data-cloud/data/databases

Try saving it in the database or in an xml file. This way you can use it in your whole application.

if you are using api calls then try to save that password in the backend , so that you can fetch the password by calling that api whenever user open the app.

Otherwise just save the password in shared prefernces so that it ll remain save even if the app closed as explained in previous answers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM