locked
Metro : Getting strings from Resources with globalization RRS feed

  • Question

  • I am designing a media player for the Windows Store and I am using a Resources.resw file to store all the strings in English (en-GB) and other .xlf files to store translations in several other languages. I am usingx:Uid="*something*" in the XAML code to access the strings. For example I have the next AppBarButton :

     
    <AppBarButton Icon="Play" x:Uid="Play" Label="Play" Click="btnPlayPause_Click" Name="PlayPauseAppBarButton"/>

    Displaying the text in any language I tried worked perfectly. The only problem is that my "PlayPauseAppBarButton" is a double purpose button : - when there is nothing playing the the Icon is "Play" and Label is "Play" - when there is something playing right now the Icon is "Pause" and Label should be "Pause"

    Changing the Icon from code was very simple :

            
    PlayPauseAppBarButton.Icon = new SymbolIcon(Symbol.Pause);

    The problem remains to change the Label accordingly. I have already tried :

    var loader = new Windows.ApplicationModel.Resources.ResourceLoader(); PlayPauseAppBarButton.Label = loader.GetString("Pause.Label");

    There are not errors, the resource "Pause.Label" is recognised, but when the program is running it displays "Play" (because of the initial XAML) and when it has to change the Label, it displays nothing. After debug, I observed that loader.GetString("Pause.Label") return a "" value (no text).

    So how can I resolve the problem ?


    • Edited by ArchTM Monday, December 23, 2013 4:53 PM
    Monday, December 23, 2013 4:53 PM

Answers

  • Hello,

    You have two options:

    1. Use a converter to switch between two different x:Uid-Values automatically
    2. Use two Buttons and hide or show them

    Make a converter like this:

        public class SampleConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, string culture)
            {
                //look at value (and parameter) to choose a return value (e.g. Visibility.Collapsed, or "PauseButton"
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, string culture)
            {
                throw new NotImplementedException();
            }
        }

    Then use the converter in the XAML like this:

    <Page.Resources>
            <local:SampleConverter x:Key="SampleConverter " />
    </Page.Resources>
    
    ...
    
    
    <AppBarButton x:Uid="SlideshowStartButton" Visibility="{Binding IsSlideshowActive, Mode=OneWay, Converter={StaticResource SampleConverter}, ConverterParameter=start}" Icon="SlideShow"  Click="SlideshowStart_Click" />
    <AppBarButton x:Uid="SlideshowStopButton" Visibility="{Binding IsSlideshowActive, Mode=OneWay, Converter={StaticResource SampleConverter}, ConverterParameter=stop}" Icon="StopSlideShow"  Click="SlideshowStop_Click" />
    

    You should be able to use similar logic to bind the x:Uid property (and others) to something.

    cheers,

    michael


    Life is unsure - always eat the dessert first!

    • Marked as answer by ArchTM Monday, December 23, 2013 11:06 PM
    Monday, December 23, 2013 5:53 PM

All replies

  • Hello,

    You have two options:

    1. Use a converter to switch between two different x:Uid-Values automatically
    2. Use two Buttons and hide or show them

    Make a converter like this:

        public class SampleConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, string culture)
            {
                //look at value (and parameter) to choose a return value (e.g. Visibility.Collapsed, or "PauseButton"
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, string culture)
            {
                throw new NotImplementedException();
            }
        }

    Then use the converter in the XAML like this:

    <Page.Resources>
            <local:SampleConverter x:Key="SampleConverter " />
    </Page.Resources>
    
    ...
    
    
    <AppBarButton x:Uid="SlideshowStartButton" Visibility="{Binding IsSlideshowActive, Mode=OneWay, Converter={StaticResource SampleConverter}, ConverterParameter=start}" Icon="SlideShow"  Click="SlideshowStart_Click" />
    <AppBarButton x:Uid="SlideshowStopButton" Visibility="{Binding IsSlideshowActive, Mode=OneWay, Converter={StaticResource SampleConverter}, ConverterParameter=stop}" Icon="StopSlideShow"  Click="SlideshowStop_Click" />
    

    You should be able to use similar logic to bind the x:Uid property (and others) to something.

    cheers,

    michael


    Life is unsure - always eat the dessert first!

    • Marked as answer by ArchTM Monday, December 23, 2013 11:06 PM
    Monday, December 23, 2013 5:53 PM
  • Yeah, I already tried that, but I receive the error "Values for 'Uid' property must be Text".
    Monday, December 23, 2013 5:57 PM