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
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 2.1.0
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2016-03-16 10:11 UTC by Filip Drsek
Modified: 2016-03-16 12:48 UTC (History)
3 users (show)

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:

Description Filip Drsek 2016-03-16 10:11:30 UTC
After upgrading a Xamarin.Forms solution to Xamarin.Forms, 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
                return this.content;
                this.content = value; // Bug in Xamarin.Forms This setter is not called from XAML!

        public View Content1
                return this.content1;
                this.content1 = value;

----- TestPage_TestControl.xaml -----
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
        <StackLayout />
        <StackLayout />

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; }

  <Button />

This will then set OtherContent.