Bind Strings from an .resw file with ReswFileCodeGenerator in XAML - c#

I tried to localize my Windows Universal App with the Multilingual app toolkit.
Because you can't bind strings directly from a .resw file in XAML, I used the ReswFileCodeGenerator tool. It works great in code behind, but in XAML I can't get this right.
In Windows Phone 8.0 I could use:
Text="{Binding Path=LocalizedResources.StringName, Source={StaticResource Strings}}"
Is there a similar way in Windows (Phone) 8.1 with the ReswFileGenerator tool?

I would still suggest you do it as Depechie already suggested: use x:Uid attribute.
If you need to get the localized string from code, just replace . in resource name with /. So, in XAML you'll write:
<TextBlock x:Uid="appString" />
In code you'll use:
var resourceLoader = new ResourceLoader();
var localizedText = resourceLoader.GetString("appString/Text");

When doing resource translations in wp8.1 with .resw files, you need to use the x:Uid attribute on your xaml control!
Like <TextBlock x:Uid="FieldKey" />
Details are mentioned here...

I've coded something to help me with this. It may not be perfect, but works great for me. Here's a link to the helper. You build it and put the .exe file in some easy-to-reach folder.
In the project with the resources, you set the pre-build action to something like this (you only need to change the "path\to\ResourcesHelper.exe" part):
call "path\to\ResourcesHelper.exe" "$(TargetName)" "$(ProjectDir)\" "$(RootNameSpace)" "universal"
Also, the main resources must be in Resources/en-US folder of your project (you can change it in the code, though).
This way, when you build the project, it will generate a file called LocalizedStrings.cs which is something like the file generated for .resx files. It contains some additional properties called LC (for lower case), UC (for upper case) and UCF (for upper case first) which return the strings in that casing. I hope you'll find it useful. :)
Note: The tool wasn't meant for other people, so I really just coded what I needed, so it may not work flawlessly.

Related

Windows Universal App UI Library Parser internal error

I built a user control in an user control library and I want to include it into my windows universal app. I remember in the old days you had to add something to app.xaml (something like pack:// this is my xaml).
So my thoughts (or hope) I just include my library and user the control. When I do I get - Parser internal error: Object writer 'xClassNotDerivedFromElement'.
I tried adding it as a msappx reference in the App.Xaml, but I may have the syntax wrong. If I can get this working by literally copying over the code itself into my project (which I do not want to do). Suggestions?
Thanks
EDIT: Added source code
public MainPage()
{
_webControl = new NativeWebView.WebUserControl();
_webControl.Loaded += _webControl_Loaded;
_contentView = new NativeWebView.ContentView(_webControl);
this.InitializeComponent();
root.Children.Add(_webControl);
}
public sealed partial class WebUserControl : UserControl, IWebView
{
public WebUserControl()
{
this.InitializeComponent();
}
}
So, I found out the answer. So to include a library into another project, you have to have some extra crazy files (.xr.xml and .xrc). I manually copied them into a folder, but it seems they can get out of date. I found that if I want to include the library from the build directory, I have to check a box in the build properties page (Generate library layout). This copies the files for me. SOLUTION!
Just add namespace of the library into root tag of the page where you use the control, ex. for ImageButton control in WinRT XAML Toolkit:
<Page
[...]
xmlns:toolkit="using:WinRTXamlToolkit.Controls">
[...]
<toolkit:ImageButton [...] />
[...]
If you still get xClassNotDerivedFromElement, it means there's a problem in the library. (need source to track it)

Use Resources to set Button's ToolTips in a program

I have a program that the user can switch the program language on run time.
I store the current language used in Program Settings and I access it in my program using
Properties.Settings.Default.Language
Now In my xaml View files I want my buttons to change their ToolTips when the user change the current language.
I have two Rescorces files: EnglishRescource.resxandFrenchRescource.rex
And I bind the ToolTips of my buttons using
ToolTip="{Binding Path=NewDocument, Source={StaticResource Resources}}"
But I don't know if this is a correct approach. How can I bind to the correct Resource file when the language is switched.
EDIT
I renamed my resources files to Resources.En-US.resx and Resources.Fr-CA.resx
Is there any particular reason you are naming them {language}Resource.resx instead of the standard, Resource.{locale}.resx?
A few sites that might be of use
http://www.codeproject.com/KB/WPF/WPF_Resx_Localization.aspx
http://msdn.microsoft.com/en-us/library/ms788718.aspx

How to properly use image resource in WPF application

What is the best way to store static images (like toolbox icons) in a WPF app?
Right now I have Images directory and use them like this:
<dxb:BarButtonItem x:Name="rbSignOut" Content="Sign out" Glyph="Images/Icons/close-16x16.png" LargeGlyph="Images/Icons/close-32x32.png" />
I think it's not the best practice because when I move XAML file in to a subfolder, I need to change all paths. Also, it just does not seems right to store paths in code. So how do I do it properly?
Instead of your path, which is relative to the XAML file:
Glyph="Images/Icons/close-16x16.png"
Use a path which is relevant to the application root (using a leading forward slash)
Glyph="/Images/Icons/close-16x16.png"
No matter where your XAML file is, your image will always be referenced from the root. As long as you don't move your images, you'll always be fine.
Use a ResourceDictionary together with your images. You can add it to the generic.xaml ResourceDictionary so you'll only have to change one path if you move it and you can use the images in every xaml file.
Just Right-Click and change your image's "Build action" to "Content" and use like this
<Image Source="/WPFApplication1;component/Images/Image.png" />
I felt it as the best approach to use Images,Video etc as the Files are not embedded into the assembly.

How to change the image on the ApplicationBar from C#?

I have a small C# project that has an ApplicationBar. But I have a small problem: I want 8 icons on the bar, and the ApplicationBar only supports 4. I came up with a solution (in C#): add a small CheckBox to ask if the user wants to use the first or second set of tools.But I'm still not able to change the icons on the ApplicationBar. I tried removing the old ones, first with ApplicationBar.MenuItems.Remove(Button1); and then with ApplicationBar.Buttons.Remove(Button1);
but neither worked. I tried changing the .IconUri property of the button, but that gave me a NullReferenceException.
I don't understand what you mean by changing it from "C#, not Silverlight". C# is a programming language and Silverlight is a framework. Nevertheless, the link you posted to explains exactly how you do it. The ApplicationBar is not a Silverlight control, it's part of the native OS. You can use the code in the link or do something like this:
firstAppBarButton = (ApplicationBarIconButton)ApplicationBar.Buttons[0];
firstAppBarButton.Text = "New Text";
firstAppBarButton.IconUri = new Uri("/appbarIcon.png",UriKind.Relative);
You need to get the ApplicationBarIconButton via the index (0 for first one, 1 for second etc..) instead of by name.
You can't refer to the application buttons by name. Try:
((ApplicationBarIconButton)ApplicationBar.Buttons[0]).Remove
I would also suggest that you do not present two groups of 4 icons to the user. The limit is 4 for a reason. Any more than that requires a UI re-think. Perhaps divide the functionality over a few pages?
The syntax above gave me a compile error. With some additional research, I got this to work for me:
ApplicationBar.Buttons.Remove((ApplicationBarIconButton) ApplicationBar.Buttons[0]);

XMLDataprovider label binding codebehind WPF c#

I am working on a WPF application. I set up a multilanguage with xml files and I use static resource binding in front code to set the corrisponding text. The issue I have is with doing the same thing in the codebehind.
Here you can see how I use it in front code:
<XmlDataProvider x:Key="Lang" Source="/lng/english.xml" XPath="WpfApplication"/>
<Label HorizontalAlignment="Center" Margin="0,10,0,5" Foreground="White" FontWeight="Bold" Content="{Binding Source={StaticResource Lang}, XPath=MenuTextClimate/#Header}"></Label>
I am trying to do the same in codebehind like this:
String selLangFullPath = WpfLibrary.LanguageOptions.getSelLangFullPath();
XmlDataProvider xmlData = (XmlDataProvider)(this.FindResource("Lang"));
xmlData.Source = new Uri(selLangFullPath, UriKind.Relative);
xmlData.XPath = "MenuTextClimate/#Header";
Binding NewBinding = new Binding();
NewBinding.Source = xmlData;
NewBinding.Mode = BindingMode.OneWay;
NewBinding.XPath = "MenuTextClimate";
lblTitle.SetBinding(Label.ContentProperty, NewBinding);
but for some reason it doesent seem to work. Can any one tell me where I went wrong?
The codebehind you've shown doesn't actually do the same thing. It's different in three ways:
You're changing the Source property of the XmlDataProvider
You're providing a different XPath to the XmlDataProvider (MenuTextClimate/#Header instead of WpfApplication).
You're also providing a different XPath in the binding expression.
The problem could simply be that any or all of those things is wrong. (The XPath ones look particularly suspicious, because they look like they presume a completely different XML document structure. Although since you're also providing a different XML document, maybe that's fine. It's impossible to tell from the information provided so far.) So the first thing I'd do is try making your C# do exactly the same as your Xaml - same URI and same XPaths. If that works, it should be easier to see which of the three things that's different is causing the problem.
Alternatively, enable WPF debug output. If you're on .NET 3.5 sp1 or earlier, this is usually on by default for Error level logging of data binding messages. (Data binding errors appear in the Output window.) As of .NET 4.0, Microsoft turned it down so you won't see it unless you ask for it. You turn it on with the 'options' dialog in Visual Studio - it's under Debugging -> Output Window. Ensure that Data Binding is set to show errors. Or for more detail, crank it all the way up and then enable full logging by adding this:
PresentationTraceSources.SetTraceLevel(NewBinding, PresentationTraceLevel.High);
That should show you full gory details of what data binding is attempting to do with your binding, and that's often a pretty good way to find out why things aren't working.

Categories

Resources