Global xmlns: and lex definitions!

Jul 12, 2012 at 8:12 AM
Edited Jul 12, 2012 at 8:13 AM

Hi,

is it somehow possible to add this code in a more global place?

 

xmlns:Engine="clr-namespace:WPFLocalizeExtension.Engine;assembly=WPFLocalizeExtension"        
        xmlns:Providers="clr-namespace:WPFLocalizeExtension.Providers;assembly=WPFLocalizeExtension"
        xmlns:lex="clr-namespace:WPFLocalizeExtension.Extensions;assembly=WPFLocalizeExtension"
        
        Engine:LocalizeDictionary.DesignCulture="de"
        Providers:ResxLocalizationProvider.DefaultAssembly="LocExTest"
        Providers:ResxLocalizationProvider.DefaultDictionary="Language"
I do not want to add it in every wpf window. Thanks for your help
Coordinator
Jul 12, 2012 at 9:20 PM
Edited Jul 12, 2012 at 9:21 PM

Generally, the xmlns definitions cannot be removed completely in XAML. We are working on an updated XML schema definition in order to reduce it to only one xmlns definition.

Best regards,

Uwe

Jul 13, 2012 at 7:38 AM
Edited Jul 13, 2012 at 7:58 AM

Hi,

yesterday i played arround with the source code and I implemented a solution for my "problem".

I implemented two new singleton classes: ResxLocalizationProviderConfig.cs, LocalizeDictionaryConfig.cs

public class ResxLocalizationProviderConfig
    {
        private static ResxLocalizationProviderConfig instance;

        public string DefaultDictionary { get; set; }

        public string DefaultAssembly { get; set; }
        
        public ResxLocalizationProviderConfig()
        {
            instance = this;
        }

        public static ResxLocalizationProviderConfig Instance
        {
            get
            {
                return instance ?? (instance = new ResxLocalizationProviderConfig());
            }
        }
    }
public class LocalizeDictionaryConfig
    {
        private static LocalizeDictionaryConfig instance;

        public string DesignCulture { get; set; }


        public LocalizeDictionaryConfig()
        {
            instance = this;
        }

        public static LocalizeDictionaryConfig Instance
        {
           get
           {
               return instance ?? (instance = new LocalizeDictionaryConfig());
           }
        }
    }

Additionally I changed some code of the ResxLocalizationProvider.cs:

       public static readonly DependencyProperty DefaultDictionaryProperty = DependencyProperty.RegisterAttached(
                "DefaultDictionary",
                typeof(string),
                typeof(ResxLocalizationProvider),
                new PropertyMetadata(ResxLocalizationProviderConfig.Instance.DefaultDictionary, AttachedPropertyChanged));

You can see that I am using my Singleton here to set a Default Value for PropertyMetadata. Before I changed this, the first parameter was NULL. I did the same with the DefaultAssemblyProperty.

Additionally I changed some code of the LocalizeDictionary.cs:
public static CultureInfo DefaultCultureInfo
        {
            get
            {
                var result = CultureInfo.InvariantCulture;
                
                if (!string.IsNullOrEmpty(LocalizeDictionaryConfig.Instance.DesignCulture))
                {
                    try
                    {
                        result = CultureInfo.GetCultureInfo(LocalizeDictionaryConfig.Instance.DesignCulture);
                    }
                    catch (Exception)
                    {
                        // Catch invalid design culture, avoids errors in xaml window definition
                        result = CultureInfo.InvariantCulture;
                    }
                }

                return result;
            }
        }

After these changes I am able to define these values inside App.xaml like this:

<Application x:Class="LocalizationExtensionTests.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:Providers="clr-namespace:WPFLocalizeExtension.Providers;assembly=WPFLocalizeExtension" xmlns:Engine="clr-namespace:WPFLocalizeExtension.Engine;assembly=WPFLocalizeExtension" StartupUri="MainWindow.xaml">
    <Application.Resources>
        <Providers:ResxLocalizationProviderConfig DefaultAssembly="LocExTest" DefaultDictionary="Language" x:Key="ProviderConfig"/>
        <Engine:LocalizeDictionaryConfig DesignCulture="de-DE" x:Key="DesignerConfig" />
    </Application.Resources>
</Application>

This will set some default global configuration inside my configuration singletons. This way I never need to import any namespace or do configuration for one of my windows. This is the only thing I need to do in my Window XAML:
<Window x:Class="LocExTest.SampleWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="SampleWindow" Height="300" Width="300"
        xmlns:lex="clr-namespace:WPFLocalizeExtension.Extensions;assembly=WPFLocalizeExtension">
    <Grid>
        <Button Width="200" Height="30" Content="{lex:Loc Sample}"/>
    </Grid>
</Window>
I hope some of you guys can use it. If you have any questions or want to have my sample solution feel free to ask.


UPDATE
Please keep in mind that this is just for the designer. You need to init wpflocalize extension in your code behind again. I do that in a Window_Loaded event inside of a Window Base Class.

Kind regards, Nicolai Schönberg
Coordinator
Jul 13, 2012 at 8:01 AM

Hi Nicolai,

now I got your point. Thank you for this interesting approach.

Best regards,

Uwe