0

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.

Keyur Ramoliya
  • 1,900
  • 2
  • 16
  • 17
  • What's the red error you get? And you can't use binding in Xamarin.Android. Data-binding is available in Xamarin.forms project. – nevermore Jul 19 '19 at 02:09
  • Cannot resolve bindingcontext and initalize component. I've got a separate class that keeps the time of logging and class with instance of that. Only thing I miss is how to display this in xaml that the user began his work that time. – felekmenelek Jul 19 '19 at 05:39
  • Are you writing a xamarin.android project or a xamarin.forms project? – nevermore Jul 19 '19 at 07:02
  • xamarain.android – felekmenelek Jul 19 '19 at 07:12
  • So, can you get the textView in code behind and set the text of textView like: `TextView textV = FindViewById(Resource.Id.timeTextView);` and then `textV.Text = "Today you work already:" + //the time you want to display` – nevermore Jul 19 '19 at 07:18
  • And what to write in xaml? And what to do with "cannot resolve findviewbyid"? – felekmenelek Jul 19 '19 at 07:43
  • No change to xaml, just build your project. – nevermore Jul 19 '19 at 07:53
  • And what with red FindViewById? – felekmenelek Jul 19 '19 at 07:59
  • Build your project directly and it will work. – nevermore Jul 19 '19 at 08:01
  • Now I have this. Build successful, but app crashes. – felekmenelek Jul 19 '19 at 08:17
  • What changes did you make to your code? You should find which line caused the crash and any stack trace? – nevermore Jul 19 '19 at 08:19
  • _appVariables.UserName = UserName; _appVariables.logDate = DateTime.Now; _appVariables.isLogged = true; await GoToDashboard.Handle(Unit.Default); TextView textV = FindViewById(Resource.Id.entryTextView); textV.Text = "You're at work since:" + _appVariables.logDate; – felekmenelek Jul 19 '19 at 08:23
  • had to do this to avoid red: private T FindViewById(int timeTextView) { throw new NotImplementedException(); } } – felekmenelek Jul 19 '19 at 08:25
  • You have to add `TextView textV = FindViewById(Resource.Id.entryTextView); textV.Text = "You're at work since:" + _appVariables.logDate;` in the Activity instead of model class. – nevermore Jul 19 '19 at 08:26
  • Thanks, it works :) What to do if I want the date to be displyed in new line? – felekmenelek Jul 19 '19 at 08:55
  • Use `textV.Text = "Today you work already:"+ "\n" + "your text";` – nevermore Jul 19 '19 at 08:58
  • Used it, but doesn't look as nice as I wanted, second line is longer, and first part of text is not really in the center of the line – felekmenelek Jul 19 '19 at 09:03
  • Set the TextAlignment to center: `textV.TextAlignment = TextAlignment.Center;`. – nevermore Jul 19 '19 at 09:05
  • Does your problem solved? I list some points in out chat as an answer and can you please mark that answer so that we can help more people with same problem! – nevermore Jul 19 '19 at 09:18
  • Thanks a lot. Another issue I have, is to count timespan between now and begin of work. I want it to be refreshed every minute. – felekmenelek Jul 19 '19 at 09:24
  • Use `TimeSpan time = DateTime.Now - start;`. Check the answer in [this thread](https://stackoverflow.com/questions/12521366/getting-time-span-between-two-times-in-c) – nevermore Jul 19 '19 at 09:29
  • Yes, but it has to be an updated time span between now and the beginning. – felekmenelek Jul 20 '19 at 16:52
  • You can use a timer to get the time span every minute and then update the label's text. Have a look at [this thread](https://stackoverflow.com/questions/47875791/adding-a-timer-to-a-xamarin-app-c) about how to use a timer in xamarin.forms. Can you please accept my answer if it helps you? – nevermore Jul 22 '19 at 01:21

1 Answers1

0

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;

nevermore
  • 15,432
  • 1
  • 12
  • 30