references in xamarin forms XAML - xaml

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.

Related

Using Resource files in XAML in Xamarin.Forms

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.

Why does Xamarin ListView Cahcing Strategy show "Ambiguous Reference"?

Below is a screenshot of a page in the "core" project. When adding CachingStrategy="RecycleElement", I receive an
"Ambiguous Reference" error.
Hovering over it does not give any additional information on how to fix it, neither does ReSharper.
You can specify RecycleElement in the ListView constructor instead using x:Arguments
<ListView ...>
<x:Arguments>
<ListViewCachingStrategy>RecycleElement</ListViewCachingStrategy>
</x:Arguments>
</ListView>
ReSharper is simply unable to see that property because it does not really exist or at least ReSharper is not able to detect it (read that paragraph for Xamarin saying this). Xamarin Forms does some tricky stuff (I think they use PInvoke or a compiler trick but cannot quite remember), when the code gets compiled, in order to change your ListView code when that property is specified and ReSharper is not aware that it is going on. Hopefully ReSharper will fix this in an update though I am not sure if they ever will.
When use the above method shows
"Error CS0234 The type or namespace name 'CachedListView' does not exist in the namespace 'Xamarin.Forms' (are you missing an assembly reference?)" is it nesseary to add x:Arguments? or is it nessasary to give reference as
xmlns:local ="clr-namespace:TestProject.Data"
Just add class
public class CachedListView : ListView
{
public CachedListView() : base(ListViewCachingStrategy.RecycleElement) { }
}
And in Xaml use CachedListView instead ListView.

What does this xmlns attribute do

<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.

Assembly Reference in Loose XAML

I have scenario where my loose xaml file can contain the custom control from another assembly. How do i make a reference to that assembly. My Loose XAML and assembly are at the same path.
I know the embedded xaml or xaml with in a project, the reference is added like this:
xmlns:WpfToolKit="http://schemas.microsoft.com/wpf/2008/toolkit"
Now how can i give similar type of reference in the loose xaml file.
Like so:
xmlns:Awesome="clr-namespace:MyAwesomeNameSpace"
And then use your controls as such:
<Awesome:MyAwesomeControl />
For nice looking schemas, please read this article on MSDN:
http://msdn.microsoft.com/en-us/library/ms747086.aspx
You will need the XmlnsDefinitionAttribute definition on your class.
Hope this helps!

Setting the namespace in XAML

Suppose I have a top-level ResourceDictionary element in a XAML file with no C# code-behind. How can I put this element in a specific namespace?
I'm not sure if you can -- but I've never seen this done.
What I have seen is putting ResourceDictionary XAML files in a directory structure in an assembly, and getting it via the Pack URI syntax.
Dr. WPF has an interesting post on getting ResourceDictionaries in code here.
It is much simpler than I had thought:
<ResourceDictionary x:Class="SomeNamespace.SomeClassName"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
</ResourceDictionary>