In Xamarin XAML, how do I set a Constraint on a RelativeLayout using a Style? : <p>I am struggling to work out the XAML syntax to apply constraints to a <code>RelativeLayout</code> using a <code>Style</code>.</p>

<p>The first piece of Xamarin XAML below shows a pair of nested <code>RelativeLayout</code> elements used to construct a simple layout (the inner element simply puts a margin around an area to which I can add other content).  This version of the code builds and runs fine on iOS and Android.</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
&lt;ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="App2.Page1"&gt;
    &lt;RelativeLayout BackgroundColor="Gray"&gt;
        &lt;RelativeLayout BackgroundColor="Maroon"
            RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent,Property=Height,Factor=0.9,Constant=0}"
            RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent,Property=Width,Factor=0.9,Constant=0}"
            RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent,Property=Height,Factor=0.05,Constant=0}"
            RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent,Property=Width,Factor=0.05,Constant=0}"&gt;
            &lt;BoxView Color="Yellow"
                RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent,Property=Height,Factor=0.25,Constant=0}"
                RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent,Property=Width,Factor=0.25,Constant=0}"
                RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent,Property=Height,Factor=0.25,Constant=0}"
                RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent,Property=Width,Factor=0.25,Constant=0}"/&gt;
        &lt;/RelativeLayout&gt;
    &lt;/RelativeLayout&gt;
&lt;/ContentPage&gt;
</code></pre>

<p>What I would like to do it use the same layout on multiple pages, so I want to put the <code>RelativeLayout</code> constraints into a <code>Style</code>.  This second piece of code does not parse or run, but I hope it shows what I am trying to achieve.  If I can get the right syntax for this, the idea is that the <code>Style</code> can then be moved out into a shared file, so I can easily re-use it across multiple instances of <code>ContentPage</code>.</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
&lt;ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="App2.Page2"&gt;
    &lt;ContentPage.Resources&gt;
        &lt;ResourceDictionary&gt;
            &lt;Style x:Key="LayoutStyle" TargetType="RelativeLayout"&gt;
                &lt;Setter Property="BackgroundColor" Value="Maroon"/&gt;
                &lt;Setter Property="HeightConstraint"&gt;
                    &lt;Setter.Value&gt;"Type=RelativeToParent,Property=Height,Factor=0.9,Constant=0"&lt;/Setter.Value&gt;
                &lt;/Setter&gt;
                &lt;Setter Property="WidthConstraint"&gt;
                    &lt;Setter.Value&gt;"Type=RelativeToParent,Property=Width,Factor=0.9,Constant=0"&lt;/Setter.Value&gt;
                &lt;/Setter&gt;
                &lt;Setter Property="YConstraint"&gt;
                    &lt;Setter.Value&gt;"Type=RelativeToParent,Property=Height,Factor=0.05,Constant=0&lt;/Setter.Value&gt;
                &lt;/Setter&gt;
                &lt;Setter Property="XConstraint"&gt;
                    &lt;Setter.Value&gt;"Type=RelativeToParent,Property=Width,Factor=0.05,Constant=0&lt;/Setter.Value&gt;
                &lt;/Setter&gt;
            &lt;/Style&gt;
        &lt;/ResourceDictionary&gt;
    &lt;/ContentPage.Resources&gt;
    &lt;RelativeLayout BackgroundColor="Gray"&gt;
        &lt;RelativeLayout Style="LayoutStyle"&gt;
            &lt;BoxView Color="Yellow"
                RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent,Property=Height,Factor=0.25,Constant=0}"
                RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent,Property=Width,Factor=0.25,Constant=0}"
                RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent,Property=Height,Factor=0.25,Constant=0}"
                RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent,Property=Width,Factor=0.25,Constant=0}"/&gt;
        &lt;/RelativeLayout&gt;
    &lt;/RelativeLayout&gt;
&lt;/ContentPage&gt;
</code></pre>

<p>Please can anyone help me out with the syntax for doing this?</p>

<p>This is a link to a complete example (which obviously requires Xamarin to be installed and needs the nuget packages to be restored): <a href="https://drive.google.com/file/d/0Bz__vXtiMe03bTFTb3owaWtfV00/view?usp=sharing">XAML Layout Example</a></p>
