help with data coming from server

Eduardo Gomez 3,431 Reputation points
2023-02-08T11:31:08.4266667+00:00

I am saving into the database the date of birth of the user, and I upload the age into my server (Firebase)

but I realize that I must keep track of the date of birth (to send an email, when is his birthday)

and in my viewModel, I am trying to assign that date int a property, so I can make calculations


        public ObservableCollection<FirebaseObject<LocalUser>>? UserData { get; set; }

        public bool IsReadOnly { get; set; }

        public string? BtonContent { get; set; }

        public DateTime DateOfBirth { get; set; }

        public string UserId { get; set; }

        public ProfilePageViewModel(string userId) {

            UserId = userId;

            LoadDataFromFirebase();
            BtonContent = Lang.Edit;
            var age = GetAge(DateOfBirth);
        }

        public static int GetAge(DateTime dateOfBirth) {
            var today = DateTime.Today;

            var a = (today.Year * 100 + today.Month) * 100 + today.Day;
            var b = (dateOfBirth.Year * 100 + dateOfBirth.Month) * 100 + dateOfBirth.Day;

            return (a - b) / 10000;
        }


        private async void LoadDataFromFirebase() {

            UserData = new ObservableCollection<FirebaseObject<LocalUser>>();

            var firebase = new FirebaseClient(
                "https://transcribed-c69c8-default-rtdb.europe-west1.firebasedatabase.app/");

            var FireUser = await firebase
               .Child("Users")
               .OnceAsync<LocalUser>();


            UserData.Clear();
            foreach (var item in FireUser) {
                if (item.Object.Id == UserId) {
                    UserData.Add(item);
                }

            }

            if (UserData.Count > 0) {
                var user = UserData.First().Object;
                DateOfBirth = user.DateOfBirth;
            }
        }
    }
}

but my date of birth doesn't set correctly

User's image

here is setting it to my local variable, but then it resets

User's image

What I do not understand is that the UI, is correct

User's image

 <StackPanel Orientation="Horizontal">
            <Ellipse
                Width="100"
                Height="100"
                HorizontalAlignment="Left"
                VerticalAlignment="Top">
                <Ellipse.Fill>
                    <ImageBrush ImageSource="/Images/Placeholder.png" />
                </Ellipse.Fill>
            </Ellipse>

            <TextBlock
                Margin="20,0,0,0"
                VerticalAlignment="Center"
                Text="{Binding DateOfBirth}" />

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,824 questions
{count} votes

Accepted answer
  1. Hui Liu-MSFT 48,616 Reputation points Microsoft Vendor
    2023-02-09T09:08:24.78+00:00

    According to your code, I updated and tested the following code, you can refer to the code below.

    The problem is that you got the data but didn't successfully assign it to the property in the ViewModel.

    So,you could add property notifications for properties in the ViewModel.

     <StackPanel >
                <TextBlock Background="AliceBlue" Width="300" Height="50"  Margin="20,0,0,0"  VerticalAlignment="Center"  Text="{Binding DateOfBirth,Mode=TwoWay ,UpdateSourceTrigger=PropertyChanged}" />
                <TextBlock Background="AliceBlue" Width="300" Height="50"  Margin="20,0,0,0"  VerticalAlignment="Center"  Text="{Binding UserId,Mode=TwoWay ,UpdateSourceTrigger=PropertyChanged }" />
                <TextBlock Background="AliceBlue" Width="300" Height="50"  Margin="20,0,0,0"  VerticalAlignment="Center"  Text="{Binding City,Mode=TwoWay ,UpdateSourceTrigger=PropertyChanged}" />
                <TextBlock Background="AliceBlue" Width="300" Height="50"  Margin="20,0,0,0"  VerticalAlignment="Center"  Text="{Binding Email,Mode=TwoWay ,UpdateSourceTrigger=PropertyChanged}" />
                
    
            </StackPanel>
    
    public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                string UserId = "DqPTMCpLNfeZuYBfixrIHyxmDZB2";
    
                DataContext = new ViewModel(UserId);
                
            }
        }
    
    
    public class ViewModel : ObservableObject
        {
            public ViewModel() { }
    
            public ObservableCollection<FirebaseObject<LocalUser>>? UserData { get; set; }
    
            public bool IsReadOnly { get; set; }
    
            public string? BtonContent { get; set; }
           
    
            private string email;
            public string Email
            {
                get => email!;
                set
                {
                    email = value;
                    OnPropertyChanged();
                }
            }
            private DateTime dateOfBirth;
            public DateTime DateOfBirth
                
            {
                get => dateOfBirth!;
                set
                {
                    dateOfBirth = value;
                    OnPropertyChanged();
                }
            }
            public string UserId { get; set; }
    
            public ViewModel(string userId)
            {
    
                UserId = userId;
    
                LoadDataFromFirebase();
               // BtonContent = Lang.Edit;
                var age = GetAge(DateOfBirth);
            }
    
            public static int GetAge(DateTime dateOfBirth)
            {
              ...
            }
    
    
            private async void LoadDataFromFirebase()
            {
    
                UserData = new ObservableCollection<FirebaseObject<LocalUser>>();
    
                var firebase = new FirebaseClient(
                    "https://transcribed-c69c8-default-rtdb.europe-west1.firebasedatabase.app/");
    
                var FireUser = await firebase
                   .Child("Users")
                   .OnceAsync<LocalUser>();
    
                UserData.Clear();
    
                foreach (var item in FireUser)
                {
                    if (item.Object.Id == UserId)
                    {
                        UserData.Add(item);
                    }
    
                }
               
                if (UserData.Count > 0)
                {
                    var user = UserData.First().Object;
                    DateOfBirth = user.DateOfBirth;
                    Email = user.Email;
                }
            }
        }
    

    The result:

    enter image description here


    If the response is helpful, please click "Accept Answer" and upvote it. Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.