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

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.

Related

Can I ignore Xamarin.Forms error warnings in XAML code if the app still works?

I am using the latest stable build of Xamarin Forms (2.3.4.247), and I'm getting errors (in the form of green underlining) in my XAML code.
One of the errors, "The type 'ContentPage' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built", seems to be a bug according to here, although this link talks about a much older version. When I rebuild the solution the green underlining and error messages disappear (until I edit the XAML page, and the errors reappear)
Another error I get is "The Page is not usable as an object element because it is not public or doesn't define a public parameterless constructor or type converter". This error also disappears after a rebuild, but then reappears when I start editing the file.
The app works as intended on both Windows and Android (can't test IOS for now). Should I just ignore these errors?
On the second error, I am passing an object to the base constructor of my XAML page (that is not related to layout in anyway). Is this a problem?
My XAML class layout looks something like this:
public partial class BaseView : ContentPage { public BaseView(Obj obj) {}}
public partial class SubPage: BaseView { public SubPage(Obj ob) : base(obj) {}}
If I add parameterless overloads of the constructor, I get the same error message. Having read a bit on Google it seems that this error (the second one I mention) will stop any of the XAML from the child page being drawn. If there is a way to have XAML page hierarchies that can add their own objects to a parent, I would prefer that (i.e. I would like the child page's XAML to be drawn).
===== EDIT
It looks like Xamarin officially suggests passing data between pages via the page constructor HERE. This directly contradicts that warning messages should be avoided (at least that is what I have always thought).

XamlParseException when using UserControl from class library dll

I have created a library which has a Popup UserControl similar to the one here.
When I create a fresh Universal Windows App and create the same UserControl inside an app and open the popup, it opens.
But if I create a Class Library and create the same UserControl inside it and try to use it (by opening the popup) inside an app, I get a XamlParseException.
It is as follows -
Windows.UI.Xaml.Markup.XamlParseException occurred
HResult=-2144665590
Message=XAML parsing failed.
Source=Windows
StackTrace:
at Windows.UI.Xaml.Application.LoadComponent(Object component, Uri resourceLocator, ComponentResourceLocation
componentResourceLocation)
at PopupTestLibrary.MyUserControl1.InitializeComponent()
I am not able to understand exactly why this is happening, since the code works fine when not called from an external class library.
Some Questions I found to be similar to mine, here on SO -
XamlParseException when consuming a Page from a library
Cannot instantiate UserControl from another assembly
All help is appreciated!
You need to Add a Resource Dictionary in your App and Add the Usercontrol Xaml content to it
as Xaml is Considered as a Content file not compiled into the code
I think that this post is just as yours.. :
https://social.msdn.microsoft.com/Forums/en-US/63f071be-a3c5-4f2d-ace2-73ca750e3252/rtm-usercontrol-class-library-and-assembly-name-with-
And, It's known issue:
Dot in the project's name cause XAMLParseException
I hope that this will help you in your issue..

Loading Loose Xaml with custom controls on WinRT fails unless dummy DataTemplate exists

In ReactiveUI, I run this code at a certain point:
const string template = "<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:routing='using:ReactiveUI.Routing'>" +
"<routing:ViewModelViewHost ViewModel=\"{Binding}\" VerticalContentAlignment=\"Stretch\" HorizontalContentAlignment=\"Stretch\" IsTabStop=\"False\" />" +
"</DataTemplate>";
var theTemplate = XamlReader.Load(template);
On other platforms, this works great (the xmlns declaration is different of course), but on {WinRT / Metro / Windows Store}, this throws an Unspecified Error:
WinRT information: The type 'ViewModelViewHost' was not found. [Line: 1 Position: 253]
The Twist
However, if you include a dummy resource on the page:
<Page.Resources>
<DataTemplate x:Name="Foo">
<routing:ViewModelViewHost ViewModel="{Binding}" />
</DataTemplate>
</Page.Resources>
...then it works! What gives?
The "twist" makes me think this must be because the application does not have correct XAML metadata for the type being instantiated - rather than using reflection to resolve types in XAML files like WPF/Silverlight, WinRT uses code generation to resolve via the IXamlMetadataProvider interface (there's a decent description here; this sounds like what you're doing, see also the followup). Adding the reference forces this metadata code to be generated properly. If this is the case, you should be able to achieve the same effect by simply adding the type itself to the resources under some unused key, without the data template.
Have a look in your application's "obj" directory, Visual Studio generates a XamlTypeInfo.g.cs file to implement IXamlMetadataProvider. This should contain an entry for the type that is failing - in the case where you have added a dummy reference, there should be full details required to instantiate the type. Without this, I've found it's possible to have some reference to type type, but insufficient information - however this prevents the fallthrough behaviour (looking up the type in a dependent DLL which might have a custom metadata provider).
Other than adding a dummy reference to the library type in the final application itself, the only solution I found for this is to apply the Bindable attribute to the type. While this is supposed to relate to C++, I found this can be used in C# to force a type to always appear in the code generated for XAML type metadata.

Setting local int variable's value to Int.MaxValue in xaml

I was wondering how I can set a local int variable's value to Int.MaxValue in xaml (which is in the ResourceDictionary in my case).
something like: (but something that works :) )
xmlns:s="clr-namespace:System;assembly=mscorlib
<s:Int32 x:Key"HelloWorld">{x:Static s:Int.MaxValue}</s:Int32>
EDIT:
#Ian:
Thank you :)
But how do I use the static resource as an int?
let's say if I have in my ResourceDictionary
<ResourceDictionary>
<x:Static
x:Key="HelloWorld"
Member="s:Int32.MaxValue"
/>
...
<blablalba TooltipService.ShowDuration="{StaticResource HelloWorld}"/>` <-- this does not work by the way
</ResourceDictionary>
This should do it:
<x:Static
x:Key="HelloWorld"
Member="s:Int32.MaxValue"
/>
Just in case it's not clear what's actually happening here, it's using a handy trick that a lot of people don't know about: although markup extensions like x:Static are normally used in attributes inside braces ({}) you can also use the element syntax for them. This is useful in situations like these where you want to use a markup extension, but the Xaml syntax expects an element. (It's most often necessary in scenarios involving collections, like this one.)
The one slight gotcha with using element syntax for markup extensions is that you no longer get to pass in constructor arguments. (Technically, constructor argument support was introduced in XAML 2009, which has been supported since .NET 4. However, it's only supported if you load Xaml via the new Xaml APIs. The MSDN documentation says that the new XAML 2009 features are not supported for compiled Xaml in WPF.) So you can only use this trick with markup extensions that offer default constructors, and which provide properties that can be used instead of constructor arguments. The StaticExtension provides a Member property that does the trick, so we're OK here, but be aware that sometimes, you'll be stuck due to the inability to call the appropriate constructor.
Note that as you type this in, the Xaml IntelliSense in Visual Studio will probably suggest StaticExtension, which is the real name of this markup extension. When you use the attribute syntax, it leaves off the Extension part, but with elements, it seems not to, for some reason. Either form is correct though - I'd go with what I've written here and not what IntelliSense suggests, because we're more accustomed to seeing "{x:Static ...}" in attributes than "{x:StaticExtension ...}" - again, both are legal, but idiomatically, we tend to leave off the Extension.
You were so close first time, just turns out you missed the 32 from Int32
<blablalba TooltipService.ShowDuration="{x:Static s:Int32.MaxValue}"/>
The other solution of adding a static resource seems a bit ridiculous - you're essentially creating a static resource as a wrapper around an existing static property...
The problem is that there is no class Int If you try with Int32 it works
As such.
<Button>
<ToolTipService.ShowDuration>
<x:Static Member="sys:Int32.MaxValue" />
</ToolTipService.ShowDuration>
</Button>

SuppressMessage in interface

I tried to suppress a particular FxCop warning for a method defined in an interface by adding SuppressMessage attribute to the method. But the warning still appears. I know the SuppressMessage attribute is the right choice.
public interface ICustomerAccess
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design",
"CA1024:UsePropertiesWhereAppropriate",
Justification = "This method involves time-consuming operations", Scope="member")]
IList<ICustomer> GetCustomers();
}
Does anyone have the experience on suppressing FxCop warning in an interface?
Thanks,
H
For the record, the answer is in the question's comments:
#Angelina said: Thank you very much! I
am able to resolve the issue by adding
CODE_ANALYSIS to the project.