Bind to a static variable inside App.xaml

Jassim Al Rahma 1,596 Reputation points
2021-04-09T19:27:29.42+00:00

Hi,

How can I bind my XAML to a static variable inside the App.xaml.cs?

For example:

namespace Mawaqeet
{
    public partial class App : Application
    {
        public static string my_name { get; set; }
        public static DateTime my_birthday { get; set; }

        public App()
        {

and then:

<Label Text="{Binding App.my_name}" />
<Label Text="{Binding my_birthday, StringFormat='{0:MMMM dd}'}" />

Thanks,
Jassim

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,377 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. JarvanZhang 23,966 Reputation points
    2021-04-12T03:54:21.757+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    How can I bind my XAML to a static variable inside the App.xaml.cs

    For this function, try using ResourceDictionary to store the resources. Then reference and apply the resource to elements by using the StaticResource or DynamicResource markup extension.

    To Label.Text is type of string, so please use a string value instead. You could create the resource for 'my_birthday' in code and use DynamicResource markup extension to apply it.

    Check the code:

       //App.xaml  
       <Application xmlns="http://xamarin.com/schemas/2014/forms"  
                    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
                    x:Class="TestApplication_5.App">  
           <Application.Resources>  
               <x:String x:Key="my_name">MyName</x:String>  
           </Application.Resources>  
       </Application>  
         
       //App.xaml.cs  
       public partial class App : Application  
       {  
           public App()  
           {  
               InitializeComponent();  
               MainPage = new NavigationPage(new TestPage());  
         
               DateTime date = xxx;  
               Application.Current.Resources.Add("my_birthday", date.ToString());  
           }  
           ...  
       }  
    

    Consume the resources in the page.xaml.

       <StackLayout>  
           <Label Text="{StaticResource my_name}" />  
           <Label Text="{DynamicResource my_birthday}" />  
       </StackLayout>  
    

    Related doc: https://learn.microsoft.com/en-us/xamarin/xamarin-forms/xaml/resource-dictionaries

    Best Regards,

    Jarvan Zhang


    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.


  2. RemVas 0 Reputation points
    2025-03-03T00:33:08.8233333+00:00

    I needed something similar, but it wasn't easy to find any good examples, so I'm posting a solution here that worked well for me. A custom markup extension can be used to return different strings:

    // App.xaml

    <maui:MauiWinUIApplication
        x:Class="MyNamespace.App"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:maui="using:Microsoft.Maui"
        xmlns:local="using:MyNamespace">
    	<maui:MauiWinUIApplication.Resources>
    		<DataTemplate x:Key="MauiAppTitleBarTemplate">
    			<Grid Height="32" VerticalAlignment="Stretch">
    				<TextBlock Margin="10,0,0,0" VerticalAlignment="Center" Text="{local:GetResourceString Key=ProductName}" FontSize="11pt" />
    			</Grid>
    		</DataTemplate>
    	</maui:MauiWinUIApplication.Resources>
    </maui:MauiWinUIApplication>
    

    // GetResourceString.cs

    using Microsoft.UI.Xaml.Markup;
    
    namespace MyNamespace
    {
    
    	[MarkupExtensionReturnType(ReturnType = typeof(string))]
    	public sealed class GetResourceString: MarkupExtension
    	{
    		public string Key { get; set; }
    
    		protected override object ProvideValue()
    		{
    			switch (this.Key) {
    				case "ProductName":
    					return MyNamespace.App.ProductName;
    				default:
    					return "Invalid string key.";
    			}
    		}
    	}
    
    }
    
    0 comments No comments

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.