In shared classes how to use local resources? - c#

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.

Related

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.

I can not seem to reach .resx WPF

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.

Make a button "public" in xaml and c#

I am building a mobile app using the Xamarin platform. In the solution, there is a core project and then the platform specific projects, in this case, .ios and .android. In my core project I have a XAML layout, with a button element.
My_App.Core.HomePage.xaml
<Button x:Name="LoginIcon"
Image="key.png"
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand"
Grid.Row="9" Grid.Column="3" Grid.ColumnSpan="3" />
I have some platform specific logic in those projects, the result of this I would like to alter the source of these buttons. e.g.
My_App.ios.customfile.cs:
My_App.HomePage.LoginIcon.Source = ImageSource.FromFile("bluetoothg.png");
of course, as the button is defined in XAML, LoginIcon isn't visible from this file, how can I make this XAML element public, so I can reference it from other projects in the solution which reference the core files?
The x:FieldModifier namespace attribute specifies the access level for generated fields for named XAML elements.
Create a property on your page and return the button:
public Button PubLoginIcon{ get{ return LoginIcon; } }
BUT, I must say something is wrongly designed if you must access the controls on another page, a better approach is to have the functionality in the page as a public function, in this case you have no need to access the controls.

How to get an icon in stackPanel in a XAML file

I am new to XAML and C#
I have an icon created already in a project and and I have to use this icon whenever I select one of the option from the dropdown menu.
I made a stackpanel in XAML file
<StackPanel Name="stackPanelforIcon">
</StackPanel>
In the code behind file I have different cases for the dropdown menu.
case IconOnSelect:
?????? = IconList.NewIcon;
This NewIcon is the one already created and I am using the source also for this
using IconProject.Iconlists;
On writing IconList.NewIcon I am not getting any error, it is referenced correctly.
What should I write at ?????? to reference it. Is there any other way apart from using stackPanel to include an icon
A StackPanel cannot show an icon on it's own. You need a control for it, for example an Image.
<StackPanel Name="stackPanelforIcon">
<Image x:Name=theImage" />
</StackPanel>
Then you can use your Icon in your code behind like this:
this.theImage.Source = IconList.NewIcon;
You may need to convert your value, you never said what type it actually is.
Please note that using code-behind is not the preferred way with WPF. Using MVVM is way easier and more natural working with WPF, using code-behind you will fight WPF all the way. Using MVVM, this could be:
<StackPanel Name="stackPanelforIcon">
<Image Source="{Binding CurrentImage}" />
</StackPanel>
with your ViewModel having a property called CurrentImage that you would set when you want to change it. Don't forget to implement INotifyPropertyChanged for the changes to take effect though.

Image not displaying at runtime C# WPF

I have been trying to learn C# but I'm coming across a lot of problems. I am trying to display an image in WPF but for some reason, the image won't show! It appears on the Visual Studio editor but when I run the application it doesn't appear.
Here is some of my code:
This is how I'm trying to display the image:
<Image HorizontalAlignment="Left" Height="100" Margin="273,147,0,0"
VerticalAlignment="Top" Width="100" Source="image.jpg"/>
I have also tried using this:
Source="pack://application:,,,/image.jpg"
Thanks for your help!
In your project:
Create a folder say "img", right click on it and select add existing item add image to folder
Go to properties of the added image, set Build Action as Resource.
It worked for me.
In XAML
<Image HorizontalAlignment="Left" Name="MyImg" Height="80" Margin="273,147,0,0"
VerticalAlignment="Top" Width="100" Source="/img/Desert.jpg"/>
If none of these work, try changing the Build Action to Content.
That's what worked for me after struggling for a long time with this.
Go to the properties for the image in Visual Studio and change "Build Action" to "Resource" and "Copy to Output Directory" to "Copy if newer".
I had to do a rebuild, but then it worked. Cred to swapnil.
please drag the image to a image source,it will be like this /img/image.jpg"/
<Image HorizontalAlignment="Left" Height="100" Margin="273,147,0,0"
VerticalAlignment="Top" Width="100" Source="/img/image.jpg"/>
If none of the above works, try Rebuilding your application from the build menu. Not "Build", but "Rebuild"
For example, this is your project structure
+ProjectName
+--+imagesFolder
| +--amogus.png
|
+--App.xaml
+--MainWindow.xaml
+--etc.
and you want to access the to amogus.png in your xaml window,
You have two ways:
note this way the imagesFolder will be visible in the release build to users
to set amogus.png Build Action to Content and
Copy to Output Directory to Copy always more info,
then rebuild from the build menu, then add this to the window xaml
<Image Source="pack://siteoforigin:,,/imagesFolder/amogus.png" ></Image>
note this way the imagesFolder will be not visible in the release build to users
to set amogus.png Build Action to Resource and
Copy to Output Directory to Do not copy or blank more info,
then rebuild from the build menu, then add this to the window xaml
<Image Source="/imagesFolder/amogus.png" ></Image>
more detail
Right click images on the Solution Explorer, choose Properties and then set the Build Action as Resource.
Did not have to do a clean and rebuild. I tried every combination listed above (I am in VS2017)
Go to Project->Properties->Resources
Select File (drop down with choices of strings, images, icons...)
Click Add Resource->Existing File
Navigate to the image and import it
VS identifies it as an image (mine is PNG) and switches the view to show Image resources
Select the thumbnail of the image and in the Properties of the Image (type should be Bitmap) set Persistence to Embedded in resx
I saved and closed Project Properties as I got confused here before
Go to the Resources folder under the project and select the image (it should be listed for you)
Select the image and set the BuildAction to Embedded Resource
I set the File Action to Copy if Newer
From here I move back and forth between Debug and runtime, various combinations of clean, build and publish and the image has FINALLY been displayed every time.
Last tidbit, the XAML in the dialog looks like this:
<Image Source="pack://siteoforigin:,,,/Resources/DeathSpiral.png" />
I have updated several projects that were supposed to display graphics but didn't always do so using the steps above. They all work now. Tested in both VS2017 and VS2019 and no errors so far.

Categories