AMorrisey
AMorrisey

Reputation: 111

Databinding Entry in Xamarin Forms

I'm trying to capture the value of the entry in a Login text box in Xamarin Forms. I've created the class, data bound in xmal, but running into build errors when compiling for iOS. Any pointers much appreciated.

Login.xmal:

 <Entry x:Name="{Binding Source = AppLoginUsername;}" Placeholder="Username" 
                   Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="3"
                   HorizontalOptions="StartAndExpand" WidthRequest="300" />

FormEntry Class.cs

using System; namespace SGC.Class

{
    public class FormEntryBindings
    {
        public FormEntryBindings()
        {


        }


        public static string AppLoginUserName { get; set; }
    }
}

Home.cs (Grabs the username entered in Login page and figures out the CustomerID)

private async void getCustBalance()
        {
            HttpClient client = new HttpClient();

            string x = FormEntryBindings.AppLoginUserName;

            int custid =  GetCustomerId(x);

The errors are:

Invalid token '{' in class, struct, or interface member declaration

Invalid expression term '='

Type or namespace definition, or end-of-file expected

Upvotes: 0

Views: 519

Answers (3)

nevermore
nevermore

Reputation: 15786

Well, based on the comments, I think your problem is that you can get the AppLoginUserName in the login page, however,you want use the AppLoginUserName in another page(another view).

I would give your several suggestions:

1.You can pass the value to the page where you want to use.

2.Use Xamarin.Essentials: Preferences.

You can save the AppLoginUserName once the user has logged in:

using Xamarin.Essentials;

Preferences.Set("AppLoginUserName", "value");

At another view where you want to use, you can get the value:

var myValue = Preferences.Get("AppLoginUserName", "default_value");

And remove the key from preferences when the user has logged out or under other situation:

Preferences.Remove("my_key");

You can refer:essentials/preferences

Upvotes: 1

Ivan I
Ivan I

Reputation: 9990

You can’t bind the name. Name is something that generates the code and you can’t change the name of the variable on the fly.

Upvotes: 1

JP Veldtman
JP Veldtman

Reputation: 182

Why dont you just give it a name, and bind the text? Eg x:Name=“User” Text=“{Binding AppLoginUsername}”...

Upvotes: 2

Related Questions