×

Discussion Board

Results 1 to 5 of 5
  1. #1
    Regular Contributor
    Join Date
    Nov 2006
    Location
    Switzerland
    Posts
    476

    Using constants in XAML TextBlock

    Hello,

    In App.xaml.cs, I've defined a static class for constants as follows:
    Code:
    static class Constants
    {
      public const string AppReleaseDate = "April 2, 2014";
    }
    Now, I'd like to access this constant in one of my XAML pages as follows. I don't get any compile errors, but anyway it doesn't work, i.e. the string is not displayed:
    Code:
    <StackPanel Margin="0,0,12,0" >
      <TextBlock Text="{ Binding AppReleaseDate }" Style="{StaticResource PhoneTextTitle3Style}" />
    </StackPanel>
    Does someone have a solution for this?

    Thanks and best regards,
    Patrick

  2. #2
    Nokia Developer Champion
    Join Date
    Sep 2012
    Location
    Morocco
    Posts
    295

    Re: Using constants in XAML TextBlock

    You should normalement point to Constants in your code, so I think you TextBlock should be :
    IsEnabled="{Binding Path=User, Source={x:Static Application.Current}}"
    Code:
    <StackPanel Margin="0,0,12,0" >
      <TextBlock Text="{ Binding Path=AppReleaseDate , Source={x:Static Constants}}" Style="{StaticResource PhoneTextTitle3Style}" />
    </StackPanel>
    I didn't test out this code,
    As for me, I would have declared those strings in the App.xaml resources and called them in my TextBlock as a StaticResources
    Code:
    <system:String x:key="AppReleaseDate" value="April 2, 2014" />
    Code:
     <TextBlock Text="{StaticResource AppReleaseDate }" Style="{StaticResource PhoneTextTitle3Style}" />
    I hope it helps
    Yassine,

  3. #3
    Regular Contributor
    Join Date
    Nov 2006
    Location
    Switzerland
    Posts
    476

    Re: Using constants in XAML TextBlock

    Hmm, I get the following error message: "The type 'x:Static' was not found. Verify that..."., i.e. x:Static is not supported in Silverlight...

  4. #4
    Nokia Developer Champion
    Join Date
    Sep 2012
    Location
    Morocco
    Posts
    295

    Re: Using constants in XAML TextBlock

    Oups my bad,
    x:Static is not available on windows phone, you should use the second solution.

  5. #5
    Regular Contributor
    Join Date
    Oct 2013
    Location
    Pennsauken, NJ, USA
    Posts
    105

    Re: Using constants in XAML TextBlock

    I'm not sure you can do x:Static with WinRT xaml... might have been only for WPF.

    In any case, I am not sure how to bind directly to a constant defined that way. I know this would work though:

    Code:
        public static class Constants
        {
            public const string AppReleaseDate = "April 2, 2014";
        }
    
       public class NonStaticConstants
       {
           public string AppReleaseDate
           {
               get { return Constants.AppReleaseDate; }
           }
       }
    Code:
    <Page.Resources>
            <local:NonStaticConstants x:Key="Constants" />
    </Page.Resources>
    
    <TextBlock Text="{Binding Path=AppReleaseDate, Source={StaticResource Constants}}" />

Similar Threads

  1. In Wp7, how to include the 1 xaml page content in another xaml page
    By Murugan india in forum Windows Phone General
    Replies: 7
    Last Post: 2013-10-18, 13:42
  2. In my page I have an problem with textblock
    By Murugan india in forum Windows Phone General
    Replies: 4
    Last Post: 2013-09-25, 23:32
  3. html formatting in xml retrieve to textblock
    By thejeraldo in forum Windows Phone General
    Replies: 1
    Last Post: 2012-02-21, 07:33
  4. seperate Constants file or RMS to store string CONSTANTS in J2ME
    By H@RIPRIY@KOND@PI in forum Mobile Java General
    Replies: 7
    Last Post: 2011-04-29, 12:52
  5. Replies: 4
    Last Post: 2010-07-21, 13:43

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×