Localize x:String - xaml

I saw such questions on MSDN, but they were not answered. So I can localize all my controls using x:Uid and specific key format in *.resw file. But how do I localize x:String? For example this:
<x:String x:Key="AppName">My App</x:String>
I know some workarounds. But are there any nice approaches?

Looks like it is impossible now. Should use x:String only for strings that are not supposed to be localized (e.g. App Name). Otherwise, using TextBlock or some other text controls is preferred

There is an msdn post on doing that here. The essence of it is something like this:
<TextBlock x:Uid="MyTextBlock" FontSize="24.667" Text="Design Placeholder" />
Notice the x:Uid value here. This now maps back to the key in your
RESW file. Anything with that starting key will have properties
merged into it. So we can have a key in our RESW for “Text” using the
key name MyTextBlock.Text with a value of “Hello World” and the
runtime will do the replacement for you.
Steps:
Create a textblock with the x:Uid property defined.
Create a RESW file for your language appropriately named for your localization
Add strings to your RESW to replace the property in your textblock

Related

CustomButton_123509716 targetType does not match type of element CustomButton_096728

I recently switched to Visual Studio 2017, and my xaml design time view has suffered considerably.
The most recent challenge is a design time-only bug where my components won't show up because their style_1093579 doesn't match style_0965327, but when I run the program, it works fine.
I agree that the numbers don't match, but I didn't add them, and can't figure out how to update them. I have tried cleaning, resetting, reverting, and manually deleting files. I've attached to the process to try to debug it. I've added [DesignTimeVisible(true)] to see if that would change anything - all I've managed to do is get some of the numbers to change. I've Googled, but even the promising questions, like TargetType does not match type of Element or 'Chart' TargetType does not match type of element 'Chart' either don't apply, or are not answered.
My best guess is that some auto generated file does not get regenerated correctly when I rebuild. (it's not the *.g.i.cs file though - I already tried deleting it.) Does anyone have another file I should try deleting, or a workaround?
I had a similar problem when creating a view based on my custom ContentControl, where I had created a Style for my custom ContentControl.
Try to explicitly set the TargetType of the Style on the CustomButton to the one used on the element that the Style is defined with.
So in in a view that is based on your CustomButton
<my:CustomButton x:Class="MyModule.Views.MyButtonView">
<my:CustomButton.Style>
<Style BasedOn="{StaticResource StyleCustomButton}" TargetType="{x:Type my:CustomButton}" />
</my:CustomButton.Style>
</my:CustomButton>
when StyleCustomButton is defined similar to this:
<Style x:Key="StyleCustomButton"
TargetType="{x:Type my:CustomButton}">
...

Formatting text with RichtTextBlock

Small question, anyone have a small example on different data formatting tips when using a RichTextBlock control in Windows8 - XAML?
Currently I'm MVVM binding the Paragraph element of the RichTextBlock to a large text property on my MVVM.
But I was wondering, how can I indicate where to add Line break, setting titles, etc...?
Anyone any good tips on doing this?
I never worked with the RichTextBlock before but there's an interesting topic on the MSDN forum: http://social.msdn.microsoft.com/Forums/en-US/winappswithnativecode/thread/d215abeb-7acd-41c1-81ba-c73a0ab68785 where an MSFT employee explains you should use the Blocks property.
Now, the Blocks property is of type BlockCollection, meaning it can hold any type of block. In your ViewModel you could fill the property with an HTML string (containing your text with headers, paragraphs, ...). Then you should consider creating an attached behavior that reads the HTML and creates the required blocks (like Windows.UI.Xaml.Documents.Paragraph) to finally fill up the Blocks property.
Take a look at the following guide to create attached behaviors: http://dotnet.dzone.com/articles/dragflickbehavior-windows
<RichTextBlock ...>
<MyBehaviors:Interaction.Behaviors>
<MyBehaviors:RichTextBehavior HtmlText="{Binding ...}"/>
</MyBehaviors:Interaction.Behaviors>
</RichTextBlock>

Can I apply "StaticResource" to element in run-time?

Can I apply "StaticResource" to element in run-time?
I know I can use "StaticResource" to element in xaml files. But, I want to know how to use it from C# (code-behind).
A StaticResource is a static defined resource, usually defined in the <X.Resources> element, for any element or page.
In C# you simply access it with X.Resources["MyResource"] like you should do {StaticResource MyResource}.
Not really with the same meaning. StaticResources are "static" in the sense that their value is resolved during Xaml parsing. The XamlParser will resolve the resource by examining the resource dictionary the ancestor FrameworkElement Resource properties that are also in the same xaml and then the Application.Resources if necessary.
If you happen to know where to find the resource you want to assign using C# code then it is as simple as in Claus' answer. However if you only know the name of the resource but not which dictionary its found in then its much tricker.
It is possible to write a routine (you can probably find one in SO or elsewhere on the web) that you can use to hunt up the Visual tree using the VisualTreeHelper looking at all the Resource properties along way. You could probably get away with this but be aware that this may search more dictionaries than the original Xaml version would and its possible for you to get some unexpected value.

XAML tag list reference

As the question suggests I'm simply looking for a XAML tag list reference. I've banged the obvious queries in Google/SO but not found what I am looking for yet. Any useful links welcome.
There's a WPF Binding Cheatsheet and another XAML for WPF Cheatsheet which might help, but really the "tags" in XAML are just the properties of the classes.
There isn't such a thing as a xaml tag list.
XAML is just a declarative way to instantiate .Net classes. Class names are elements in XAML and properties on the class are attributes or attribute elements using dot notation.
Tags in XAML only mirror the types in one or more assemblies that are bound to a particular XAML namespace.
There are however a specific set of elements that are specific to XAML in itself and are not related to any particular .Net assembly, those are usually in the x: namespace, more info here: XAML Namespace (x:).
There is no such thing as the XAML tag list since XAML is an open system.
There are, however, standard vocabularies. Rob Relyea's Blog is a good place to keep track of the standardization around these vocabluaries. For example, this is an entry for the Silverlight XAML vocabulary.
With WPF the XAML elements map to the classes like StackPanel. MSDN seems to give XAML examples for many of the controls.
There are XAML-specific conventions about representing things like complex properties and bindings. However, there is no definitive list of XAML tags. XAML tags are actually mapped to WPF objects. For example, <Button> is just a XAML representation of the System.Windows.Controls.Button class and the attributes allowed on the <Button> tag are the public properties of the Button class.
There should be several WPF cheatsheets available soon on http://www.devsheets.com (since around September 2011) ... You can download it there, and also buy printed versions with more detailed information on them (not all content fits in publicly available pdf, because pdf would not be readable if printed on paper without high quality print ... that is why we also decided to sell high quality laminated cheatsheet prints in addition to their free versions)

How to correctly inherit from a usercontrol defined in XAML in Silverlight

If I have a usercontrol (in Silverlight) that I've written, that uses XAML to define it's appearance, how can I make a customised version of it?
i.e. I have MyControl.xaml & MyControl.xaml.cs
What do I need to do if I want a "SpecialisedControl" child class? I assume I just make a new code file, then inherit from MyControl. But what if I want to change the appearance of the base class - then what do I do?
I wrote this thinking you were talking about WPF, rather than Silverlight, but there may be enough overlap for this to be helpful, so I'm posting it, anyway.
If by "change the appearance of the base class" you mean "provide a new template", then what you need is probably a CustomControl, not a UserControl.
The best way to accomplish this is to follow the example set by other Microsoft controls, such as Button or ListBox:
Create a class that derives directly from Control (or whatever is closest to your control).
If any properties will need to be exposed to the control (such as text on a button, for example), make sure that you properly define them as DependencyProperties.
As described here, create a ResourceDictionary called Themes/generic.xaml and add a style for your class that includes a template (don't give the style a key).
Use TemplateBindings for any properties of elements on your control that need to get values from your control.
If you'll need to attach any event handlers to elements in your template, give them a unique name. Microsoft uses the convention of prefixing these names with "PART_", and I think it's a good thing to do for the sake of consistency, but it's not strictly required.
Again, if you need to attach event handlers, overload OnApplyTemplate(). In this method, you should detach any old event handlers (we certainly don't want any memory leaks!), and look for elements that have the names your provided in your template--when you find them, attach event handlers, as necessary.
This is certainly much more work than simply deriving from UserControl, but if you want to be able to totally re-template controls, like you can with the built-in controls, this is the way to do it.
On the other hand, if all you want to do is to provide a certain amount of limited customization, such as changing the background, or associating a Command with some user action, then the best thing to do is to expose DependencyProperties, which can then be set in styles for your control, or on instances of your control, itself.
In the case you mentioned of wanting to customize the look in an inherited control, the process is pretty similar: just add a default style for the new control with a new template; if you need to add more event handlers, just be absolutely certain that you call base.OnApplyTemplate().
I dunno, I like doing things with just plain objects. Here's an article that describes an easy way to slip a XAML-designed control outside your inheritance hierarchy so that you can customize appearance and behavior using SimpleThingsLikeInheritance rather than MicrosoftStuffThatAlmostWorks
http://gen5.info/q/2009/02/10/subverting-xaml-how-to-inherit-from-silverlight-user-controls/
As Mihnea's link describes, the easiest solution is to simply add a namespace in your XAML:
C#
public class MyBase : UserControl
{
}
public class FirstUserControl : MyBase
{
...
}
XAML
<local:MyBase
x:Class="FirstUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:local="YourAssembly" ...>
<!-- Sticking with UserControl instead of local:MyBase makes this clearer -->
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
..
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
..Your XAML
</local:MyBase>
You can solve this by using a wrapper as described in the link above.
But you can also use the strategy pattern to solve this problem.
In this post I explain how you implement these two methods.
http://www.lab101.be/2008/07/silverlight-usercontrol-inheritance/