79607464

Date: 2025-05-05 18:46:53
Score: 1
Natty:
Report link

Thank you, aw3!
It works! Really happy about the solution.

Update requested by Kristof Bergé...
--------------------------------------------

It could perhaps be a bit confusing, but I wrote the question for the "Dashboard" page but I tried out the answer for a page named "MainPage".

XMAL code

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:viewmodel="clr-namespace:SleepDiaryViewModel.ViewModels;assembly=SleepDiaryViewModel"
             x:DataType="viewmodel:MainPageViewModel"
             x:Class="SleepDiary.MainPage"
             xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit">

    <ContentPage.BindingContext>
        <viewmodel:MainPageViewModel />
    </ContentPage.BindingContext>
    
    <ScrollView>
        <VerticalStackLayout>
            <Label 
                HorizontalOptions="Start"
                VerticalOptions="Center"
                Text="{Binding Test}"
                TextColor="Green"
                FontSize="Title"/>
        </VerticalStackLayout>
    </ScrollView>
</ContentPage>

.CS-file (I have left out unneccesary code)

public partial class MainPage : ContentPage
{

    protected override void OnAppearing()
    {
        (BindingContext as MainPageViewModel).ReloadData();

        base.OnAppearing();
    }

    public MainPage()
    {
        InitializeComponent();
        BindingContext = new SleepDiaryViewModel.ViewModels.MainPageViewModel();
    }
}
Reasons:
  • Blacklisted phrase (0.5): Thank you
  • Long answer (-1):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Filler text (0.5): --------------------------------------------
  • Low reputation (1):
Posted by: Niklas Karlsson