I can not seem to reach .resx WPF - c#

I have a WPF c# project, and I created two .resx files for changing the language.
I've set Access Modifier: Public
Added in .xaml
xmlns:resx="clr-namespace:Management_Software.Properties"
When I try to access a string with {x:Static resx:...} I see only a .settings file .
Do I have to add the .resx files in App.config,App.xaml ?

in order to use resoiurces you have to:
1. Include resource file in xaml like
xmlns:resource ="clr-namespace:WpfApplication1.Strings.en_US"
2.You can use it with binding like:
<TextBlock Text="{x:Static resource:Resource1.Greeting}" />
if you do these things try to rebuild the project. Xaml cannot figure out that you included new file and generates an error.
In my resource file I have:
Name = Greeting
Value = Sample test
Hope this helps.

Related

How do I localize a Button's ToolTip text in UWP - WinUI 2

I'm making a UWP app with WinUI 2.8.
I have a button control, and with a basic ToolTip in it.
How can I localize the ToolTip using localization tables?
Here's my XAML code of the button:
<Button
x:Name="SaveB"
x:Uid="Save"
Content="Save"
ToolTipService.ToolTip="Saves the input."
Click="SaveB_Click"
/>
What you need to do is to create resources files targeting different languages in your app and save the localized string in the files.
Here are the steps to create a localized string for en-US. You could take it as example.
On the Application tab, confirm that the Default language is set appropriately (for example, "en" or "en-US").
Under your project node, create a new folder and name it "Strings".
Under Strings, create a new sub-folder and name it "en-US".
Under en-US, create a new Resources File (.resw) and confirm that it is named "Resources.resw".
Save the resource strings into Resources.resw file.
Like this:
The Xaml code should be like this:
<Grid>
<Button x:Name="SaveB" x:Uid="Save" Click="SaveB_Click" />
</Grid>
Result:
More information please refer to: Localize strings in your UI and app package manifest

How to fetch a string resource in a class library

I'm working on a class library which contains several .resw resource files and pages. The question is, how to fetch the string and use it for UI component properties.
Better to show the case in another way. First, please refer to to image for the solution structure:
From the picture, "MobileReplicaBase" is a class library. Please not the two selected file. In "UIresources.resw", I defined a string resource:
And trying to use it in a button control in EdicolaPage:
<Button x:Name="mOpenBtn" Grid.Row="4" x:Uid="OpenBtn" Visibility="{x:Bind Path=type, Converter={StaticResource typeStringToVisibilityConverterForOpenButton}}" Tag="{x:Bind Path=productCode}" Click="mOpenBtn_Click"/>
But this won't work, the Content property for the button is only an empty string. What I can guess is that, the application is trying to load the "OpenBtn" resource from resource map in project "MobileReplica", which is currently the start up project in the solution.
Note: the button may be in a datatemplate in a GridView, so fetch the resource in C# code may not be a good idea.
Problem solved. Use following code to access resource in a class lib:
<Button x:Uid="/{library_name}/{resource_file_name}/{resource_name}"/>
In my case, I should use:
<Button x:Uid="/MobileReplicaBase/UIresources/OpenBtn">
To access to the resource.

WPF - Import custom fonts in C#

I would like to import custom fonts on my WPF application so that they work without having the client to install them.
All the answers I have found so far are in XAML, I would like to do it only in C#.
My fonts are in Resources/Fonts/.
I have already tried this :
Fonts.GetFontFamilies(new Uri("pack://application:,,,/Resources/Fonts/#"));
But it didn't work.
I did everything bluetoothfx said but it still did not work.
Then I changed the Build action of my fonts (it was to Content), to Embedded Resource, and it worked. Resource works also for me.
Thanks anyway.
I think the way that you are working will not work.
At first create a folder name fonts then Add the font to your project, change its Build Action to Content.
Now you need to find the internal name (Real name) of the font not the font-file name. You can have it by opening the font file and you can see it on top.
Now edit App.xaml
<Application.resources>
<style x:key="MYFONT_INTERNAL_NAME">
<setter property="TextElement.FontFamily"
value="pack://application:,,,/fonts/#MYFONT_INTERNAL_NAME" />
</style>
Now use it in your code like:
<TextBlock Style="{StaticResource MYFONT_INTERNAL_NAME}" FontSize="16" Text="Font Style" />
To know more search here:
http://www.alteridem.net/2014/02/24/custom-fonts-in-wpf-applications/

In shared classes how to use local resources?

I have a Parent project and a child project. In the child project I have removed all the classes (MainPage.xaml and it's .cs file) and have linked the Parent Project's MainPage.xaml and it's .cs file using Add as a link.
In my child Project I want to run the project using this project's resource files(like AppResources.resx). For example, I want to print a customised string value from AppResources.resx from child project, I use this code in my xaml..
<TextBlock x:Name="Txtblk" HorizontalAlignment="Left" Margin="56,147,0,0" Grid.Row="1" TextWrapping="Wrap"
Text="{Binding Path=LocalizedResources.ApplicationName, Source={StaticResource LocalizedStrings}}"
VerticalAlignment="Top" Height="87" Width="155"/>
The above code prints child Project's name.
I would like to achieve this bringing the code into my .cs file. Using the below statement I can do that ,
Txtblk.Text = AppResources.ApplicationTitle;
But to do this, I have to add Parent Projects resources and it returns Parent Project's resource values...
I want to use binding principle like
Binding b = new Binding();
b.Source = LocalizedStrings; // How do I set this to {StaticResource LocalizedStrings}
b.Path = new PropertyPath("LocalizedResources.ApplicationName");
b.Mode = BindingMode.OneWay;
Txtblk.SetBinding(TextBlock.TextProperty, b);
How do I make this work?
Thanks.
The solution is to have 2 resource files for each project. One that is shared/linked across all projects and then one that is unique to each project. Give then different names and then you can refer to the specific one you want in each location.

wpf how to use jpg from resource in xaml-image?

In my c#/WPF project, I added a jpg to the resources to embed into the exe file.
Now I want to use this jpg in an image tag, something like
<xmlns:prop="clr-namespace:MyProgram.Properties"
<Image Source="{Binding Source={StaticResource prop:LogoJpg}}"
My problem: it does not work. I got no idea, how to use the image. I could use files from the hdd, but I need the image to be embedded in the exe file.
First, add the image to your project, with a Build Action of "Resource" (not EmbeddedResource). For instance, I've added an image called "logo.jpg" to a folder called Images in my main project.
Then, in XAML, you use just use that resource as follows:
<Image Source="Images\logo.jpg" />
You can also use the pack syntax for the source:
<Image Source="pack://siteoforigin:,,,/Images/logo.jpg" />
Hope this helps.

Categories