Xamarin.forms: [Bug] BindableLayout.SetItemTemplate raises Specified cast is not valid when set

Created on 11 Sep 2019  路  6Comments  路  Source: xamarin/Xamarin.Forms

Description

I am trying to make use of the Bindable Layouts feature specifically with a StackLayout.
Everything is working fine expect when I attempt to set the data template via the BindableLayout.SetItemTemplate method which raises an exception. Yes I am doing this all via code and not XAML.

Steps to Reproduce

  1. Take the XF sample from https://docs.microsoft.com/en-au/samples/xamarin/xamarin-forms-samples/userinterface-bindablelayouts/
  2. This works fine but is all XAML so create a new page using c#.
    public Page1()
    {
    BindingContext = new UserProfileViewModel();

        var stackLayout = new StackLayout
        {
            Children = {
                new Label { Text = "Welcome to Xamarin.Forms!" }
            }
        };
        stackLayout.Margin = new Thickness(20, 50);
        var identstack = new StackLayout();
        identstack.Orientation = StackOrientation.Vertical;
        identstack.HorizontalOptions = LayoutOptions.FillAndExpand;
        identstack.VerticalOptions = LayoutOptions.FillAndExpand;
        BindableLayout.SetItemsSource(identstack, (BindingContext as UserProfileViewModel).User.FavoriteTech);
        //var dataTemplate = new DataTemplate(() =>
        //{
        //    var grid = new Grid();
    
        //    return new ViewCell { View = grid };
        //});
        //BindableLayout.SetItemTemplate(identstack, dataTemplate);
    
        stackLayout.Children.Add(identstack);
        Content = stackLayout;
    
  3. This page will work fine as it uses a default template. If you uncomment the code above to specify a custom template you get the error. Yes I am aware there is no real value to my sample template I was simply trying to keep as simple as possible to ensure it was not my logic being the issue here.

Expected Behavior

Data is displayed when custom template set

Actual Behavior

runtime exception raised System.InvalidCastException: 'Specified cast is not valid.'

Basic Information

Templates seem to work fine when defined in XAML but not from code and I need to use code.

My env values

Microsoft Visual Studio Professional 2019
Version 16.2.4
VisualStudio.16.Release/16.2.4+29230.47
Microsoft .NET Framework
Version 4.8.03752

Installed Version: Professional

Application Insights Tools for Visual Studio Package 9.1.00611.1
Application Insights Tools for Visual Studio

ASP.NET and Web Tools 2019 16.2.292.25104
ASP.NET and Web Tools 2019

ASP.NET Web Frameworks and Tools 2019 16.2.292.25104
For additional information, visit https://www.asp.net/

Azure App Service Tools v3.0.0 16.2.292.25104
Azure App Service Tools v3.0.0

Azure Functions and Web Jobs Tools 16.2.292.25104
Azure Functions and Web Jobs Tools

C# Tools 3.2.1-beta4-19408-03+2fc6a04980f800c59e8ede97e6ae294ff47d666e
C# components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Common Azure Tools 1.10
Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.

Extensibility Message Bus 1.2.0 (d16-2@8b56e20)
Provides common messaging-based MEF services for loosely coupled Visual Studio extension components communication and integration.

IntelliCode Extension 1.0
IntelliCode Visual Studio Extension Detailed Info

Microsoft Azure Tools 2.9
Microsoft Azure Tools for Microsoft Visual Studio 0x10 - v2.9.20626.2

Microsoft Continuous Delivery Tools for Visual Studio 0.4
Simplifying the configuration of Azure DevOps pipelines from within the Visual Studio IDE.

Microsoft JVM Debugger 1.0
Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines

Microsoft Library Manager 1.0
Install client-side libraries easily to any web project

Microsoft MI-Based Debugger 1.0
Provides support for connecting Visual Studio to MI compatible debuggers

Microsoft Visual Studio Tools for Containers 1.1
Develop, run, validate your ASP.NET Core applications in the target environment. F5 your application directly into a container with debugging, or CTRL + F5 to edit & refresh your app without having to rebuild the container.

Mono Debugging for Visual Studio 16.2.6 (4cfc7c3)
Support for debugging Mono processes with Visual Studio.

NuGet Package Manager 5.2.0
NuGet Package Manager in Visual Studio. For more information about NuGet, visit https://docs.nuget.org/

ProjectServicesPackage Extension 1.0
ProjectServicesPackage Visual Studio Extension Detailed Info

ResourcePackage Extension 1.0
ResourcePackage Visual Studio Extension Detailed Info

ResourcePackage Extension 1.0
ResourcePackage Visual Studio Extension Detailed Info

SQL Server Data Tools 16.0.61906.28070
Microsoft SQL Server Data Tools

TypeScript Tools 16.0.10627.2001
TypeScript Tools for Microsoft Visual Studio

Visual Basic Tools 3.2.1-beta4-19408-03+2fc6a04980f800c59e8ede97e6ae294ff47d666e
Visual Basic components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Visual F# Tools 10.4 for F# 4.6 16.2.0-beta.19321.1+a24d94ecf97d0d69d4fbe6b8b10cd1f97737fff4
Microsoft Visual F# Tools 10.4 for F# 4.6

Visual Studio Code Debug Adapter Host Package 1.0
Interop layer for hosting Visual Studio Code debug adapters in Visual Studio

Visual Studio Spell Check Everywhere VSSpellCheckEverywhere
An extension that enables spell checking within any Visual Studio file editor or tool window that uses WPF text boxes.
https://GitHub.com/EWSoftware/VSSpellChecker

Visual Studio Spell Checker VSSpellChecker
An editor extension that checks the spelling of comments, strings, and plain text as you type or interactively with tool windows.
https://GitHub.com/EWSoftware/VSSpellChecker

Visual Studio Tools for Containers 1.0
Visual Studio Tools for Containers

VisualStudio.Mac 1.0
Mac Extension for Visual Studio

Xamarin 16.2.0.95 (d16-2@37df81894)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin Designer 16.2.0.375 (remotes/origin/d16-2@357d38ef4)
Visual Studio extension to enable Xamarin Designer tools in Visual Studio.

Xamarin Templates 16.3.117 (59a59e8)
Templates for building iOS, Android, and Windows apps with Xamarin and Xamarin.Forms.

Xamarin.Android SDK 9.4.1.1 (d16-2/cec9eb4)
Xamarin.Android Reference Assemblies and MSBuild support.
Mono: mono/mono/2019-02@e6f5369c2d2
Java.Interop: xamarin/java.interop/d16-2@d64ada5
LibZipSharp: grendello/LibZipSharp/d16-2@caa0c74
LibZip: nih-at/libzip/rel-1-5-1@b95cf3f
ProGuard: xamarin/proguard/master@905836d
SQLite: xamarin/sqlite/3.27.1@8212a2d
Xamarin.Android Tools: xamarin/xamarin-android-tools/d16-2@6f6c969

Xamarin.iOS and Xamarin.Mac SDK 12.14.0.114 (c669116)
Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support.### Screenshots

high unverified bug

Most helpful comment

I spend so many hours trying to figure out what the issue was. @samhouts it would be really wise to add some comments to the Xamarin documentation, since it only shows the example on a listview and might lead to confusion just like it happend to @Chris-Marassovich and I .

Thanks!

All 6 comments

Did your XAML also use a ViewCell? Try just returning the Grid in the DataTemplate lambda.

Good eye, @GalaxiaGuy! The BindableLayout doesn't know what to do with ViewCells. Those are just for ListView and TableView. Remove the ViewCell, and it should work like a charm!

Thanks!

I spend so many hours trying to figure out what the issue was. @samhouts it would be really wise to add some comments to the Xamarin documentation, since it only shows the example on a listview and might lead to confusion just like it happend to @Chris-Marassovich and I .

Thanks!

@davidbritch Do/should we have this called out somewhere in our docs?

There's no ListView example in the Bindable Layout doc. All the Bindable Layout examples clearly show that you don't put a ViewCell in a DataTemplate. Not feeling the need to call it out any further.

Not showing a ViewCell example is not the same as showing that you shouldn't use a ViewCell, so this is far from clear. You may not be "feeling the need" to see this explained, but as far as I can tell it is absolutely not logically inferable from the examples given.

Was this page helpful?
0 / 5 - 0 ratings