简体   繁体   中英

how to use a class property in xaml in xamarin?

I want a property from c# class in a text view in XAML. How to do this? I want to show when the user started his job end how long he works (Every day after he's logged in).

Here's my code:

XAML

 <?xml version="1.0" encoding="utf-8"?>
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" 

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/gradient">


    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        app:itemBackground="@color/colorPrimary"
        app:itemIconTint="@drawable/nav_item_color_state"
        app:itemTextColor="@drawable/nav_item_color_state"
        app:menu="@menu/bottom_navigation_main"

    />

    android:text="You're at work since:"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:textStyle="bold"
            android:textSize="24sp"
            android:textColor="#ffffff"
            android:layout_alignParentTop="true"
            android:layout_marginTop="84dp"
            android:id="@+id/entryTextView"
            android:layout_centerHorizontal="true"
            android:theme="@style/MyCustomTheme"/>
    <TextView
            android:text= Today you work already:"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_alignParentTop="true"
            android:layout_marginTop="226dp"
            android:textStyle="bold"
            android:textSize="24sp"
            android:textColor="#ffffff"
            android:id="@+id/timeTextView"
            android:layout_centerHorizontal="true" 
            android:theme="@style/MyCustomTheme"/>

C# class I want to do the binding with

public LoginViewModel(ILogin login)
{
    _loginService = login;
    _appVariables = new AppVariables();
    GoToDashboard = new Interaction<Unit, Unit>();
    var canLogin = this.WhenAnyValue(x => x.UserName, x => x.Password, LoginInputValidator.Validate);
    LoginCommand = ReactiveCommand.CreateFromTask(async () => { await TryLogin(); }, canLogin);
}

private async Task<IObservable<bool>> TryLogin()
{
    var lg = await _loginService.Login(_userName, _password);
    if (lg)
    {
        _appVariables.UserName = UserName;
        _appVariables.logDate = DateTime.Now;
        _appVariables.isLogged = true;

        await GoToDashboard.Handle(Unit.Default);
    }
    return Observable.Return(lg);
}

c# another class

public class AppVariables
{
    public bool isLogged;
    public DateTime logDate;
    public DateTime offDate;
    public string UserName;
}

I've tried with this

BindingContext = this;  
InitializeComponent();

but it's always red, I don't know what's the problem.

1.To access the element in the axml in Xamarin.android project, you can use:

TextView textV = FindViewById<TextView>(Resource.Id.entryTextView); 

2. TextView textV = FindViewById<TextView>(Resource.Id.entryTextView); should be added in the Activity class instead of model class.

3.To start a new line in label/textView, you can use "/n"

4.To center the text in the textView, you can set the TextAlignment :

textV.TextAlignment = TextAlignment.Center;

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