I'm trying to use a resource file in my XAML. For some reason i keep getting the error of not finding the type Texts. Not sure what I'm doing wrong here.
XAML
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:CashRegisterApp.Resources"
x:Class="CashRegisterApp.Start">
<ContentPage.Content>
<StackLayout
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand" >
<Label Text="{x:Static local:Texts.Start}"/>
</StackLayout>
</ContentPage.Content>
</ContentPage>
RESX
Solution explorer
Creating RESX files within Shared projects is known to cause issues. You can see several lengthy posts in Xamarin Forums regarding this (here and here for example).
The easiest solution that will allow you to use the approach you want is to create a new .NET Standard Library of PCL library in your solution, create your RESX files there and set their visibility to public. Then you will be able to utilize them using the x:Static syntax as expected.
Many developers use an alternative in the form of a custom markup extension like the solution by ClaudioPereira in this forum. This simplifies the syntax even more.
Finally, for most detailed information on Xamarin.Forms you can refer to the official documentation.
I had this issue too and I hope this answer helps people in the future with this issue.
Following this guide taught me how to set up resx files in Xamarin forms.
Their TranslateExtension allows for referring to the resx file directly from Xaml.
Unfortunately, in its raw form it doesn't pick up a runtime change in locales.
This is fixable by changing their "Localize" class (on the native platforms) to keep a reference of the CultureInfo when changed via the SetLocale method and return it when the GetCurrentCultureInfo method is called.
Related
In my last years release i used net core 3 and WPF like that:
<Label Content="{x:Static p:Resources.WJAJobtitle}" VerticalAlignment="Center"/>
Now i'm trying to upgrade to net 5 with WinUI3. As far as i read, i have to use now TextBlock instead of Label. I read the docs, but haven't found anything about static resources.
Maybe anyone knows, how to set it correctly?
As found there the new format for such things are:
<TextBlock x:Uid="WJAJobtitle"/>
How would you make a reference in XAML vs code behind? It seems like mrkup:Class="System.Web.UI.HtmlTextWriterTag" in the following snip would work
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="ns.proj"
mrkup:Class="System.Web.UI.HtmlTextWriterTag"
>
...
allowing the following.
<mrkup:hr/>
I still get the undeclared prefix error.
I have tried backing down the tree to System.Web.UI and made sure I was not using a reserved word like html:xx and it still fails. I must be overlooking something basic or seriously missing a XAML/Xamarin.Forms premise.
Update Edit:
Based on Jason and Diego's input:
Intellisense kicked in after typing using xmlns:mrkup="using: resulting in
xmlns:mrkup="clr-namespace:System.Web;assembly=netstandard"
Which is a great start
<mrkup:UI.HtmlTextWriterTag hr=""/>
Fails with mrkup:UI.HtmlTextWriterTag.hr not found in xmlns clr-Namespace:System.Web;assembly=netstandard
Is it possible that .UI is not in System.Web in .NET Standard 2.0?
Update to the Update:
It appears that UI is NOT in System.Web in .NET Standard 2.0. This is from the System.Web.dll in C:\Program Files (x86)\Microsoft SDKs\NuGetPackagesFallback\NETStandard.Library\2.0.1\build\netstandard2.0\ref
Actually, you have to declare the mrkup prefix before use it.
Just like this:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="ns.proj"
xmlns:mrkup="clr-namespace:System.Web.UI;assembly:dllName">
...
<mrkup:HtmlTextWriterTag hr="Property Value"/>
...
</ContentPage>
I'm not sure where the namespace ends, so in the snippet above I'm supposing the namespace is System.Web.UI and the class HtmlTextWriterTag and then you have a property hr on it. If the referred namespace is outside the actual project, you have to notice that assembly part of the declaration, but you can ignore it when referring to the class of the same assembly.
You can take a look at the official docs from Microsoft about Declaring Namespaces for Types to get a more detailed information.
<Window x:Class="Template.MainWindow"
xmlns:System="clr-namespace:System;assembly=mscorlib">
<Grid>
</Grid>
What does xmlns:System="clr-namespace:System;assembly=mscorlib means?
My ideas:
Include System from mscorlib? What about clr-namespace? So I can
use all functionality in mscorlib in Template.MainWindow.xaml.
Thanks for replies
Is the namespace associated to Window tag
namespaces are used to avoid collisions when two different systems use the same tag name.
Refer to Microsoft Documentation about this:
http://msdn.microsoft.com/en-us/library/ms747086.aspx
See the two topics:
The WPF and XAML Namespace Declarations
Mapping to Custom Classes and Assemblies
You will get idea little clear.
I am currently making a basic Windows Phone 7 application for fun, and I am trying to store string (currently only those) values in a RESX file rather than embedding them all within the XAML files directly, or even adding code to manually insert the values.
In WPF, to use the property from the RESX file, you simply map the namespace that contains the RESX:
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:resx="clr-namespace:Namespace.To.Your.Resx;assembly=SuppliedIfSeparate"
and then use it:
<TextBlock Text="{x:Static resx:Strings.AppTitle}" />
But, I tried, and I've read that x:Static is simply not supported by Silverlight, and by extension, not supported in WP7 (It appears that they have provided the means, but not direct access to x:Static (French) in Silverlight 5/Mango).
I found this post describing an "AppConverter" class that is manually created to leverage similar functionality and it is currently what I am doing. The gist of the post is to create a resource within your XAML page that uses the AppConverter class for pre-specified keys, and then using that to replace the x:Static with a binding.
<TextBlock Text="{Binding Source={StaticResource AppTitle},Converter={StaticResource ResourceRetriever}}" />
It works, but it's verbose and error prone compared to the WPF implementation (a lot of copying and pasting between pages, and even controls).
What is the current best practice for this situation?
MSDN How to: Build a Localized Application for Windows Phone
I have a reasonably large Silveright 3.0 project on the go, and I'm having issues accessing a couple of custom font resources from within one of the assemblies.
I've got a working test solution where I have added a custom font as a resource, and can access it fine from XAML using:
<TextBlock Text="Test" FontFamily="FontName.ttf#Font Name" />
The test solution consists of the TestProject.Application and the TestProject.Application.Web projects, with all the fun and games obviously in the TestProject.Application project
However, when I try this in my main solution, the fonts refuse to show in the correct type face (instead showing in the default font). There's no difference in the way the font has been added to project between the test solution and the main solution, and the XAML is identical.
However, there is a solution layout difference. In the main solution, as well as having a MainApp.Application and MainApp.Application.Web project, I also have a MainApp.Application.ViewModel project and a MainApp.Application.Views project, and the problem piece of XAML is the in the MainApp.Application.Views project (not the .Application project like the test solution).
I've tried putting the font into either the .Application or .Application.Views project, tried changing the Build Action to Content, Embedded Resource etc, all to no avail.
So, is there an issue accessing font resources from a child assembly that I don't know about, or has anyone successfully done this?
My long term need will be to have the valid custom fonts being stored as resources in a separate .Application.FontLibrary assembly that will be on-demand downloaded and cached, and the XAML controls in the .Application.Views project will need to reference this FontLibrary assembly to get the valid fonts. I've also tried xcreating this separate font library assembly, and I can't seem to get the fonts from the second assembly.
As some additional information, I've also tried the following font referencing approaches:
<TextBlock Text="Test" FontFamily="/FontName.ttf#Font Name" />
<TextBlock Text="Test" FontFamily="pack:application,,,/FontName.ttf#Font Name" />
<TextBlock Text="Test" FontFamily="pack:application,,,/MainApp.Application.Views;/FontName.ttf#Font Name" />
<TextBlock Text="Test" FontFamily="pack:application,,,/MainApp.Application.Views;component/FontName.ttf#Font Name" />
And a few similar variants with different assembly references/sub directories/random semi colons.
And so far nothing works... anyone struck this (and preferably solved it)?
This code works for me:
... FontFamily="/(DLL);Component/(DIR-optional)/(Font_file)#(Font_name)"/> ...