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
Related
What I've learnt from the MAUI Tutorial is that the XAML file gets translated into equivalent C# code. This means, if I have a handler (say BtnClick) defined on a button (say x:Name="BtnEx"), somewhere while compilation, it'll get translated to BtnEx.Clicked += BtnClick. However, I also know that it is usually a standard to separate the handler from XAML so that one doesn't accidentally delete it, and define it in the code-behind instead. What I did:
<Button
x:Name="CounterBtn"
Text="Click me"
SemanticProperties.Hint="Counts the number of times you click"
Clicked="OnCounterClicked"
HorizontalOptions="Center" />
Code-behind file:
public MainPage()
{
InitializeComponent();
CounterBtn.Clicked -= OnCounterClicked; // method definition omitted in MRE
}
The resultant code worked as if the handler had not subscribed meaning that -= was below the += in the compiled code-behind. So where exactly will the += lie in the compiler code-behind? Somewhere in the ctor after the initializeComponent call or somewhere else entirely?
It doesn't work the way you are thinking. You won't find "equivalent c#", similar to what you would type if you typed it yourself.
AFAIK, xaml is handled the way it was in WPF: its compiled into a data "blob", which is processed by the Maui runtime.
All that shows up in c# is "mappings", so that c# compiler can connect c# references to xaml declarations.
"InitializeComponent" is in the .g.cs, but it does not reveal the xaml details, just the names of elements visible to c#.
"InitializeComponent" is when the equivalent of += is run; but you can't see that.
This is exactly the reason why you find .g.cs files in your projects obj folder. All of them are generated code files. They are created by the compiler when you build your project. The purpose of this files is to provide a generated implementation for partial classes or XAML-generated classes in your project.
For example, when you create a XAML-based user interface in a WPF, UWP or MAUI application, the XAML compiler generates .g.cs files that contain the generated code for the classes that correspond to the elements in the XAML markup. This generated code includes properties, event handlers, and other code necessary to interact with the user interface elements at runtime. Since the elements classes are defined partial, InitializeComponent() then is used to set everything up in your own implementations file (constructor).
In general, you should not make any changes to the .g.cs files, because any changes will be overwritten the next time you build the project. Instead, you should make changes to the related partial classes or XAML markup files and let the compiler generate the updated .g.cs files.
I am having a problem to realize how to use Font Awesome icons in my Xamarin application, I want to use it with ImageButtonas icon. And most tutorials I found didn't help me understand how it works.
As explained in Microsoft Documentation:
You need to first to have the font file I believe .ttf (or .otf).
Add it to your shared project.
Right click on the file and click on "properties", then set it build action to Embedded Resource.
Export it with a friendly alias name in your AssemblyInfo.cs or App.xaml.cs:
[assembly: ExportFont("file-name.ttf", Alias = "FontAwesome")]
Consume it:
<Label FontFamily="FontAwesome" Text=""/>
For the list of icons code take a look at FontAwesome codes.
If you want to use it with click capabilities like a button, then you can use a label with GestureRecognizers:
<Label FontFamily="FontAwesome" Text="">
<Label.GestureRecognizers>
<TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped"/>
</Label.GestureRecognizers>
</Label>
UPDATE
Even better use an ImageButton with a FontImageSource property instead of a label, you have click capabilities of a button also I found interesting that you can change the color of your glyph icon, weather hard-coded or dynamically depending on the selected theme, here is an example:
<ImageButton>
<ImageButton.Source>
<FontImageSource FontFamily="FontAwesome"
Glyph="{x:Static fonts:IconFont.AddressBook}"
Color="{AppThemeBinding Dark=White,
Light=Black}"/>
</ImageButton.Source>
</ImageButton>
You can also define a static class having const string properties, each one having as value the code corresponding to an icon glyph and as name a description of it that way you will need to provide only the description instead of the code like I did with Glyph="{x:Static fonts:IconFont.AddressBook}", it will looks something like this:
static class IconFont
{
public const string Ad = "\uf641";
public const string AddressBook = "\uf2b9";
...
}
I invite you to follow this video tutorial and check out this GitHub Page which generate the static c# class for you starting from a font glyph file that you submit.
FontAwesome have multiple framework supported (Vue, React, Angular, WordPress, LESS, SCSS). But I don't know why they are not providing it for Xamarin Forms and MAUI.
I have created custom control for Xamarin Forms and MAUI.
Instructions are available at
https://www.nuget.org/packages/Brushtail.FontAwesome.Mobile/
Feedbacks are welcome.
In C# or else VB.Net, using a Visual Studio Package, I would like to assign a custom keyboard shortcut to a CommandBarButton, for example Ctrl + E + R, then, when pressed, the associated CommandBarButton should call its Execute method (I imagine that).
How I could do it?.
Update
( please avoid question above, that issue was already answered. )
Actually I'm using an vsct file, then my keybinding is this:
<KeyBindings>
<KeyBinding guid="guidMainCmdSet" id="cmdidMyCommand" editor="guidVSStd97"
mod1="Control" key1="E"
mod2="Control" key2="R"/>
</KeyBindings>
MSDN explains that the guidVSStd97 is global, it seems to affect to all parts of the IDE:
https://msdn.microsoft.com/en-us/library/bb165973%28v=vs.90%29.aspx
To define key bindings at the global scope, use an Editor ID value of
guidVSStd97.
My extension works with the selected text of the Code Window, so its weird that for example in the solution explorer, or with any project loaded I'm able to press the hotkey.
Then, I would like to be able to press that hotkey (Ctrl+ E + R) only in the Code Window Editor.
My question is:
Which is the editor id of the Code Window Editor?
Additional requisite:
I need an MSDN reference to see more related editor ids, I can't find anything about that.
In Visual Studio, keyboard shortcuts are associated to commands, not directly to CommandBarButtons. Technically they are called Keyboard Bindings and are declared in the .vsct file where you declare commands. See KeyBindings element
Edited: You have to use:
<KeyBinding guid="guidVSMyPackageCmdSet" id="cmdidMyCommand" editor="guidSourceCodeTextEditor" mod1="Control" key1="X" mod2="Control" key2="X"/>
<GuidSymbol name ="guidVisualBasicEditor" value="{2c015c70-c72c-11d0-88c3-00a0c9110049}" />
<GuidSymbol name ="guidSourceCodeTextWithEncodingEditor" value="{c7747503-0e24-4fbe-be4b-94180c3947d7}" />
<GuidSymbol name ="guidSourceCodeTextEditor" value="{8b382828-6202-11d1-8870-0000f87579d2}" />
...
where guidSourceCodeTextEditor can be any name that you define in the <Symbols> section whose value you must get from HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\12.0Exp_Config\Editors. Once you run the package, if you go to Tools >Options window, Environment > Keyboard section, type the name of your command in Show Commands Containing, and you should see the shortcut in the list with the editor between parenthesis, as if you have selected it from the "Use new shortcut in" list. Which yields us to the question if the guids are the same for each Visual Studio version. AFAIK, this is not guaranteed (nothing prevents Microsoft changing guids in a new version) but likely they are the same. I cannot verify right now because the computer that I am using only has VS 2013.
I was unable to find the relevant information for Visual Studio 2019 "C# Editor". But it relatively easy, though, not intuitive. To get the guids for the standard editors you can:
Go to Tools > Options > Environment > Keyboard
Select any command in the list
Open Use new shortcut in dropdown, select required editor
Assign any shortcut to it
Save the options
Go to Tools > Import and Export Settings... > Export
Unselect ALL and then choose Environment > Keyboard
Press Export and choose where to save it as .vssettings file
Open the .vssettings file with any text editor
Search for UserShortcuts. You'll find your modified command there
In the Shortcut node, you'll find your scope (in my case Scope="C# Editor")
Search for that scope value (e.g. C# Editor)
You'll find a Scope XML node. In my case it is:
<Scope Name="C# Editor" ID="{A6C744A8-0E4A-4FC6-886A-064283054674}"/>
Copy the id including curly braces, and set it to the value of <GuidSymbol ... /> as described in another answer in this topic. In my case it is:
<GuidSymbol name="guidCSharpEditor" value="{A6C744A8-0E4A-4FC6-886A-064283054674}" />
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.
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.