Home > other >  Binding class property into label
Binding class property into label

Time:11-05

I have an issue with xaml that i can't resolve. I'm not very practice with xaml and binding so i search for help (i work with MAUI.NET). My problem is:

i have this class MemoryInfo:

namespace APEEvo.Mobile.Settings
{
    public static class MemoryInfo
    {
        public static LoginInfo CurrentUserInfo { get; set; }
    }
}

the CurrentUserInfo refer to this class:

namespace APEEvo.Commons
{
    public class LoginInfo
    {
        public string UserName { get; set; }
        public string UserNameFull { get; set; }
        public string Role { get; set; }
    }
}

My purpose is to binding directly in a label into the XAML (and not by code) the info of MemoryInfo.CurrentUserInfo.UserNameFull. Something like that:

<Label x:Name="txtUsername" Text="{Binding Mode=TwoWay, Source={x:DynamicResource Settings:MemoryInfo}, Path=LoginInfo.UserNameFull }" HorizontalOptions="Center" VerticalOptions="Center" Margin="0,0,15,0"/>

So when MemoryInfo.CurrentUserInfo.UserNameFull change into another Page, i can see the modidify in this label.

Thanks for support

I update for more clarity all the xaml:

<?xml version="1.0" encoding="utf-8" ?>
<ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:Settings="clr-namespace:APEEvo.Mobile.Settings"
         x:Class="APEEvo.Mobile.Components.InfoBar">
<HorizontalStackLayout BackgroundColor="#85ABD5" >
    <Grid Margin="5,5">
        <Grid.RowDefinitions>
            <RowDefinition Height="50" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition Width="90"/>
            <ColumnDefinition Width="200" />
            <ColumnDefinition Width="90" />
            <ColumnDefinition Width="90" />
            <ColumnDefinition Width="90" />
        </Grid.ColumnDefinitions>
        <ImageButton Grid.Column="0" WidthRequest="25" HeightRequest="25"  Source="icon_infobar_menu.png" Clicked="ImageButton_Clicked" Margin="0,0,10,0"/>
        <ImageButton Grid.Column="1" x:Name="imgLogo" Source="logo_ae1.png" Clicked="ImageButton_Clicked" Margin="0,0,350,0"/>
        
        <Grid Grid.Column="2" >
            <HorizontalStackLayout HorizontalOptions="Center" VerticalOptions="Center">
                <Image Source="icon_infobar_tablet.png" WidthRequest="25" HeightRequest="25" Margin="0,0,5,0"/>
                <Label x:Name="txtMachine" HorizontalOptions="Center" VerticalOptions="Center" Margin="0,0,15,0"/>
                <Line Stroke="White" Y1="0" Y2="50" StrokeDashArray="4,4" StrokeDashOffset="2"/>
            </HorizontalStackLayout>
        </Grid>

        <Grid Grid.Column="3" >
            <HorizontalStackLayout HorizontalOptions="Center" VerticalOptions="Center">
                <Image Source="icon_infobar_login.png" WidthRequest="25" HeightRequest="25" Margin="0,0,5,0"/>
                <Label x:Name="txtUsername" Text="{Binding Mode=TwoWay, Source={x:Null Settings:MemoryInfo.CurrentUserInfo}, Path=UserNameFullName }" HorizontalOptions="Center" VerticalOptions="Center" Margin="0,0,15,0"/>
                <Line Stroke="White" Y1="0" Y2="50" StrokeDashArray="4,4" StrokeDashOffset="2"/>
            </HorizontalStackLayout>
        </Grid>

        <Grid Grid.Column="4" >
            <HorizontalStackLayout HorizontalOptions="Center" VerticalOptions="Center">
                <ImageButton Source="icon_infobar_logout.png" BackgroundColor="Transparent" WidthRequest="25" HeightRequest="25" Margin="0,0,5,0" Clicked="ImageButtonLogout_Clicked"/>
                <Label Text="Logout" HorizontalOptions="Center" VerticalOptions="Center" Margin="0,0,15,0"/>
                <Line Stroke="White" Y1="0" Y2="50" StrokeDashArray="4,4" StrokeDashOffset="2"/>
            </HorizontalStackLayout>
        </Grid>

        <Grid Grid.Column="5" >
            <HorizontalStackLayout HorizontalOptions="Center" VerticalOptions="Center">
                <ImageButton Source="icon_infobar_info.png" WidthRequest="25" HeightRequest="25" Margin="0,0,5,0" Clicked="ImageButtonHelp_Clicked"/>
                <Label Text="Help" HorizontalOptions="Center" VerticalOptions="Center" Margin="0,0,15,0"/>
                <Line Stroke="White" Y1="0" Y2="50" StrokeDashArray="4,4" StrokeDashOffset="2"/>
            </HorizontalStackLayout>
        </Grid>

        <Grid Grid.Column="6" >
            <HorizontalStackLayout HorizontalOptions="Center" VerticalOptions="Center">
                <Image Source="icon_infobar_clock.png" WidthRequest="25" HeightRequest="25" Margin="0,0,5,0"/>
                <Label x:Name = "txtClock" Text="17:22" HorizontalOptions="Center" VerticalOptions="Center" Margin="0,0,15,0" />
            </HorizontalStackLayout>
        </Grid>

    </Grid>

</HorizontalStackLayout>

CodePudding user response:

What you actually ask is "How to bind static property of a static class".

First. Your properties are not observable. The setter should notify that a change has been made. So when this static property is set from somewhere else in the program, it knows that it has to be updated in places, where it is bound to.

Second. Please do not do anything of the above. For 10 years, I had to deal with old projects that were written by sharing data with static structures. It is nightmare to debug problems, and to fix them.

I recommend that you change your design approach.

New user has logged. You are probably returning to the page you had open, and you want to update the UI. Override OnAppearing and make the changes there.

If you really want to share, do it by dependency injecting a singleton.

builder.Services.AddSingleton<ILogin, LoginService>();

Also, sometimes you may want to keep the user logged. Why not use settings to store information that will be accessible from anywhere in your app, and still be reusable after restart of application.

public string FirstName
        {
            get => settings.Get("FirstName", "");
            set => settings.Set("FirstName", value);
        }

Edit: In some situations, where I need to pass information that something has happened between separated segments (And to keep them as separate as possible) I use CommunityToolkit.MVVM Messaging.

It is another decent way to avoid use of static classes.

  • Related