How to localize WPF without recompilation? (dynamically loaded XAML) - wpf

We have a WPF application that is loading a usercontrol at runtime from the deployed .XAML file (customer requirement - they want to be able to replace the views entirely).
We would like to localize strings in this XAML file. All .resx file based WPF localization approaches I have found on the web seem to require recompilation when the localized values are changed.
How to localize a dynamically loaded XAML usercontrol without needing to recompile the resources?

This solution seems to provide a markup extensiion that manages localization loaded from XML files.
Syntax:
<TextBlock loc:Translate.Uid="3"
Text="{loc:Translate}"
Background="{loc:Translate}"
Width="{loc:Translate}"
Height="{loc:Translate}" FontSize="18"
/>
It also supports on the fly language selection (no need to reopen the window).

Related

Loading external XAML file into a WPF project? The file is our "styles"

Has anyone had experience, or is it even possible to load an external XAML file into a WPF project from a hosted website.
We are wondering because we are defining the XAML file as our "styles". We would like a person not familiar with XAML to edit the file and then we don't have to redeploy the whole application, but next time the application loads it will just reference the changed XAML file.
Or is this not possible because the XAML files are compiled into the project?
Or would an option be to load an external XML file in code behind and populate our "style" properties that way? Is this possible?
We currently are using ResourceDictionary and calling an internal XAML file in the application, but we would like a more dynamic solution.
Using XamlReader.Load you can use the XAML parser. There is a performance hit from parsing XAML instead of BAML, and downloading a file from the network could negatively impact the (in my experience) often already slow startup times for WPF applications. The blog entry below provides a nice explanation of dynamically loading resource dictionaries.
http://blogs.interknowlogy.com/2011/09/02/xamlreader-looseresourcedictionaryfiles-parsercontext/

Is it possible to localize a string inside cs file in WPF project using LocBalm method?

In our WPF plugin project we use LocBaml tool to localize UI. But now it is needed to localize a string inside a cs file:
ToolInfo.MenuCaption = "&Toggle console";
I've found some localization topics:
WPF Localization - On-the-fly Language Selection
WPF Application Framework (WAF)
WPF Localization Extension
It seems that for using these 3rd party tools it is needed to remove LocBalm implementation and write a new one.
Is it possible to stay with LocBaml tool and add a translation just for this string in cs file?
If the custom localization is taking place only in codebehind (cs file(s)), there's no reason that I can see for it to not work with LocBaml.
You could always use a hack to use LocBaml for this localized string as well, something like this :
<TextBlock x:Uid="HiddenText" Name="HiddenText" Text="&Toggle console" Visibility="Collapsed" />
And in codebehind :
ToolInfo.MenuCaption = HiddenText.Text;
This is kind of mildly awful, but you should be able to still localize the string with LocBaml this way.
Otherwise, you'll have to string together a separate system yourself - something with resource strings and a resource manager, most likely - or try to get one of the localization platforms to play nice with LocBaml.

Why does the Application class (App) have both xaml and code behind files?

In WPF applications all the views are inherited from System.Windows.Window and have an associated xaml and codebehind file. That seems logical.
However I'm confused that why does the App file, inherited from System.Windows.Application, have a xaml file? Although it is an application and not a view (It is not visible)? I know that this file is usually used to define application resources, etc, and xaml provides an efficient way of defining them. But that can also be done programatically. Then what benefit did the designers of wpf achieve by having both the xaml and code behind files for "App"? Wouldn't one of them have been enough?
However I'm confused that why does the App file, inherited from System.Windows.Application, have a xaml file? Although it is an application and not a view (It is not visible)?
Remember that XAML is not a UI language, but a general declarative language. While it's true that it's mostly used to represent UI for WPF or SilverLigth, it's also used to declare graph of objects in other non-UI technology.
The first example that comes into my mind is the Workflow (the XOML is a derivate of the XAML), SharePoint also use XAML in some hidden parts, and I've seen in a customer project with use XAML as a meta-language for generating web-apps (and yes, it actually outputs HTML).
Then, to answer to your question, the application have both files (and it is not actually a requirement) because you can :
declare some objects (in the xaml)
override the behavior of the application (by overriding appropriate methods)
Designers can specify resources for entire application without entering any code and use it in any Window. Its something like a root for all windows. For example, if you use one style for every TextBox (or any other control) in every window, you can specify it in App.xaml and bind anywhere without duplicating.

What controls should I use on a Silverlight website for loading textual content?

I am embarking on development of a Silverlight based website. I am the lone developer and am doing it on my own (ie, not for any company).
Now I want to load a lot of textual content on the website along with animations and rich user interfaces that can be created using Silverlight. The text content may change from time to time and when that happens, I don't want to do a lot of rework. So I m thinking to load the text from a Word/text file into controls and whenever new content arrives/existing content is modified, I just have to append it to the Word/text file.
This way the application itself remains untouched, only the file contents keep changing. Silverlight doesn't support FlowDocument. RichTextBox doesnt have a Load or LoadFile property. So how do I go about this? Should I make use of Frame, Downloader and similar other controls as well? What do you suggest? What would be the best approach to this?
The RichTextBox does have a Xaml property so you could download Xaml files containing the restricted set of textual elements that RichTextBox supports. You could also create a Silverlight editor around which you could create and upload this Xaml text content.
However have you considered whether Silverlight is the right platform to deliver primarily textual content? HTML is pretty good at that and with frameworks such as JQuery you can create quite interactive experiences that work well across browsers.

WPF XAML resources

I would like to place some WPF Data Templates in a loose file that I can reference where needed in my UserControl. I do not want the Data Templates to be application wide (use app.config), I only want it to be specific to my library.
Is there a way to do this besides placing the Data Templates in UserControls and then loading the UserControls?
Thanks.
Create a ResourceDictionary and put them in that. You can then load the ResourceDictionary and access the contained DataTemplates using indexer syntax:
DataTemplate myTemplate = (DataTemplate)rd["MyTemplate"];
The ResourceDictionary is a XAML file which you would compile into your library just as you would with a UserControl or Window. If you want to ship the templates as loose (uncompiled) XAML then you can still use a ResourceDictionary, but would need to use XamlReader to load it from the .xaml source file.

Resources