Bug 39677 - Cannot set a hiding property (with the “new” keyword) in XAML
Summary: Cannot set a hiding property (with the “new” keyword) in XAML
Status: RESOLVED INVALID
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 2.1.0
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2016-03-16 10:11 UTC by Filip Drsek
Modified: 2016-03-16 12:48 UTC (History)
3 users (show)

Tags:
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 INVALID

Description Filip Drsek 2016-03-16 10:11:30 UTC
After upgrading a Xamarin.Forms solution to Xamarin.Forms 2.1.0.6526, one component derived from the “ContentView” control stopped functioning and started throwing a “NullReference” exception, which is due to not calling the setter of the property declared as “public new View Content { get { … } set { … } }”. I created an example control to demonstrate this problem:

----- TestControl.cs -----
using Xamarin.Forms;

namespace MyDemo
{
    public class TestControl : ContentView
    {
        private View content;
        private View content1;

        public new View Content
        {
            get
            {
                return this.content;
            }
            set
            {
                this.content = value; // Bug in Xamarin.Forms 2.1.0.6526: This setter is not called from XAML!
            }
        }

        public View Content1
        {
            get
            {
                return this.content1;
            }
            set
            {
                this.content1 = value;
            }
        }
    }
}

----- TestPage_TestControl.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:myDemo="clr-namespace:MyDemo;assembly=MyDemo"
             x:Class="ASOL.SCS.Client.XamarinForms.Views.Demo.TestPage_TestControl">
  <StackLayout>
    <myDemo:TestControl>
      <myDemo:TestControl.Content>
        <StackLayout />
      </myDemo:TestControl.Content>
      <myDemo:TestControl.Content1>        
        <StackLayout />
      </myDemo:TestControl.Content1>
    </myDemo:TestControl>
  </StackLayout>
</ContentPage>

Here in XAML I set both the “Content” and “Content1” properties, but if I place breakpoints in the setters of these properties, I see that only the setter of the “Content1” property is invoked.
Comment 1 Jason Smith [MSFT] 2016-03-16 12:48:17 UTC
This is by design. When you using the new keyword to mask a property you can also change the return type. This means we are either forced to crash in that case or to have a complex set of rules about when we do or do not use a masked result. In general this behavior is simply not supported. You can however use the ContentPropertyAttribute to change which property is detected as the Content and then assign that to a different non-masked property.

[ContentProperty ("OtherContent")]
public class MyThingus : ContentView
{
  public View OtherContent { get; set; }
}

<MyThingus>
  <Button />
</MyThingus>

This will then set OtherContent.