Bug 35388 - Attached properties break XAML compilation
Summary: Attached properties break XAML compilation
Status: RESOLVED ANSWERED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 1.5.1
Hardware: PC Mac OS
: --- normal
Target Milestone: ---
Assignee: Stephane Delcroix
URL:
Depends on:
Blocks:
 
Reported: 2015-10-29 21:33 UTC by Kent
Modified: 2015-11-05 19:53 UTC (History)
4 users (show)

Tags: AC XamlC
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED ANSWERED

Description Kent 2015-10-29 21:33:52 UTC
If I define an attached property, such as:

    public static class ListViewBehavior
    {
        public static BindableProperty IsSelectionEnabledProperty = BindableProperty.CreateAttached(
            "IsSelectionEnabled",
            typeof(bool),
            typeof(ListViewBehavior),
            true,
            propertyChanged: OnIsSelectionEnabledChanged);

        private static void OnIsSelectionEnabledChanged(BindableObject bindable, object oldValue, object newValue)
        {
            var listView = (ListView)bindable;

            if ((bool)newValue)
            {
                listView.ItemSelected -= OnListViewItemSelected;
            }
            else
            {
                listView.ItemSelected += OnListViewItemSelected;
            }
        }

        private static void OnListViewItemSelected(object sender, SelectedItemChangedEventArgs e)
        {
            ((ListView)sender).SelectedItem = null;
        }
    }

Then try to use it in compiled XAML as follows:

    <ListView b:ListViewBehavior.IsSelectionEnabled="false">

I get a compilation error:

    error : Position 17:47. No property, bindable property, or event found for 'IsSelectedEnabled'

The XAML works fine when compilation is disabled.
Comment 1 Stephane Delcroix 2015-11-02 05:36:01 UTC
Are you sure the pasted snippet of code represents your actual code ?

the error speaks about `IsSelectedEnabled` while your property is `IsSelectionEnabled`
Comment 2 Stephane Delcroix 2015-11-02 09:33:10 UTC
adding a 

public static bool Get IsSelectionEnabled (BindableObject view) {
    return (bool) view.GetValue(IsSelectionEnabledProperty);
}

should get you through
Comment 3 Kent 2015-11-05 19:29:52 UTC
Now I'm getting a completely different error even without having added the static accessors:

error MSB4018: The "XamlCTask" task failed unexpectedly.
System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.

Server stack trace: 
   at Mono.Collections.Generic.Collection`1.get_Item(Int32 index)
   at Xamarin.Forms.Build.Tasks.XamlCTask.Compile()
   at Xamarin.Forms.Build.Tasks.XamlCTask.Execute()
   at System.Runtime.Remoting.Messaging.StackBuilderSink._PrivateProcessMessage(IntPtr md, Object[] args, Object server, Object[]& outArgs)
   at System.Runtime.Remoting.Messaging.StackBuilderSink.SyncProcessMessage(IMessage msg)

Exception rethrown at [0]: 
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
   at Microsoft.Build.Framework.ITask.Execute()
   at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
   at Microsoft.Build.BackEnd.TaskBuilder.<ExecuteInstantiatedTask>d__26.MoveNext()

I think I'll email support and include my project at this point. I'll update this ticket again once I have a resolution.
Comment 4 Kent 2015-11-05 19:53:14 UTC
Hmm, it seems a complete clean (git clean -xdf) and rebuild has resolved the issue (in conjunction with the static accessors, as you suggested).

Thanks!