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.
Related
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
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 can I bind value from my resources files (es,en resources) to a element on page like text blocks and so on.
I've found this tutorial but in this case is using C#, I want to do it but just put some binding on the element markup not coding.
That page clearly shows how to associate controls to resources:
You need to associate every control that needs localized text with the
.resw file. You do this using the x:Uid attribute on your XAML
elements like this
<TextBlock x:Uid="Greeting" Text="" />
Have you tried it?
I have a class library that is essentially a collection of forms to be run. Consider it a module/plugin in a larger program, that can be developed independently, all the larger program cares about is the DLL (and interface).
Running the main form of the class library is fine and works well. My issue is with pictures. I've set up an Images folder in the class library, added an image, set it's Build Action to Embedded Resource and then rebuilt the project, but the images won't appear in the main program.
XAML:
<Button x:Name="btnAdd" Command="{Binding Add}">
<StackPanel Orientation="Horizontal">
<Image x:Name="imgAdd" Source="Resources/Add.png"/>
<Label>New</Label>
</StackPanel>
</Button>
The interesting part though, is that if I create a BitmapSource in code-behind and assign it to imgAdd in the constructor of the form, it works as expected. Does anyone have any ideas as to why this might be the case?
Use Pack URIs for your images.
<Button x:Name="btnAdd" Command="{Binding Add}">
<StackPanel Orientation="Horizontal">
<Image x:Name="imgAdd" Source="pack://application:,,,/ReferencedAssembly;component/Resources/Add.png"/>
<Label>New</Label>
</StackPanel>
</Button>
It turns out that the correct Build Action is actually Resource rather than Embedded Resource. Thinking about it now, Embedded Resource does seem more like a reference to a Resource in another DLL.
I inadvertently found the answer in this post while trying to improve my code.
I am currently customizing a ListBox. With that I mean adding an image-element, second line-element etc. to one listItem.
The itemSource is a List in C#, therefore I have no preview of the items in Expression Blend/VS. And that's the pain.
Because I have always to edit the XAML and then deploy to check. And this goes on and on until the last pixel is correct.
Isn't there a way, of editing a ListBox with custom items (with a dynamic itemSource) live in Blend/VS?
That would really fasten up my developing.
If you want to see how your controls look like in design time, you must use SampleData. There are several ways to do it, it depends on your framework.
Let's say you have a page named MainPage.xaml. If you don't have view model already, create a new one and name it MainViewModel.cs. Define all public properties that will be used for binding.
Once you have your view model, create new file in a folder named SampleData and name it MainViewModelSampleData.xaml.
Now, in the MainPage.xaml add the following attribute to the page element:
d:DataContext={d:DesignData Source=SampleData/MainViewModelSampleData.xaml}
Also set Build Action for MainViewModelSampleData.xaml to DesignData.
Now, if you want to display data in your MainPage, you need to define all properties in the sample data file. For example:
// view model contains public properties Title of type string and Children of type
// PersonViewModel which contains properties Name and Age (string and int respectively)
<local:MainViewModel xmlns:local="clr-namespace:myapp"
Title="Title">
<local:MainViewModel.Children>
<local:ChildViewModel Name="John" Age="31" />
</local:MainViewModel.Children>
</local:MainViewModel>
You should now see your page filled with data in your design view. This way by using MVVM you can create mock data quickly. That will ensure that you can design your view around existing data without running the application.
Read more on the following links:
31 Days of Mango | Day #18: Using Sample Data
Modify sample data
Generate sample data
Using Blend Sample data at Design time and real data at Runtime
I now know how to do that.
If anyone if you guys ever stumble upon this problem, do this:
Copy all the XAML you wrote in the stackpanel of your itemtemplate
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
//...
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
And copy it up to <ListBox> //Here </ListBox>
There you can edit it in the designer.
And when you're done, just copy the code back to the StackPanel.