Bug 31547 - Type Mismatch when using DynamicResource in XAML to set value in RelativeLayout Constraint
Summary: Type Mismatch when using DynamicResource in XAML to set value in RelativeLayo...
Status: RESOLVED INVALID
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 1.4.4
Hardware: PC Windows
: Normal normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2015-07-01 14:11 UTC by David
Modified: 2015-07-06 03:58 UTC (History)
5 users (show)

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


Attachments
XAML file showign lyout that exhbits issue (1.02 KB, application/xaml+xml)
2015-07-01 14:11 UTC, David
Details
Sample created (19.37 KB, application/octet-stream)
2015-07-02 07:41 UTC, Rajneesh Kumar
Details


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 David 2015-07-01 14:11:56 UTC
Created attachment 11807 [details]
XAML file showign lyout that exhbits issue

I wanted to use a DynamicResource to control the height of a RelativeLayout in XAML, so that it could be calculated dynamically in the View Model, based upon the screen size and other content to be displayed.

I declare a resource in a Resource Dictionary within the XAML, and use it to set the Factor property in RelativeLayout.HeightConstraint. If Factor is set via a StaticResource declaration it renderers correctly. If it is set via a DynamicResource declaration a run time exception is generated: 

'Xamarin.Forms.Xaml.XamlParseException' occurred in Xamarin.Forms.Xaml.DLL but was not handled in user code

Additional information: Position 39:7. Cannot assign property "Factor": type mismatch between "Xamarin.Forms.DynamicResource" and "System.Double"



This generates run time exception:

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Class="TestApp.TestProject"
    >

  <ContentPage.Resources>
    <ResourceDictionary>
      <x:Double x:Key="headingHeightFactor">0.18</x:Double>
    </ResourceDictionary>
  </ContentPage.Resources>

  <RelativeLayout
    x:Name="PageLayout"
    HorizontalOptions="FillAndExpand"
    VerticalOptions="FillAndExpand"
    >
    <BoxView
      x:Name="TestLayout"
      Color="Green"
      RelativeLayout.XConstraint="{ConstraintExpression Type=Constant, Constant=0}"
      RelativeLayout.YConstraint="{ConstraintExpression Type=Constant, Constant=0}"
      RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height,  Factor={DynamicResource headingHeightFactor}}"
      RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width,  Factor=1.0}"
    />
  </RelativeLayout>
</ContentPage>




This renders correctly:

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Class="TestApp.TestProject"
    >

  <ContentPage.Resources>
    <ResourceDictionary>
      <x:Double x:Key="headingHeightFactor">0.18</x:Double>
    </ResourceDictionary>
  </ContentPage.Resources>

  <RelativeLayout
    x:Name="PageLayout"
    HorizontalOptions="FillAndExpand"
    VerticalOptions="FillAndExpand"
    >
    <BoxView
      x:Name="TestLayout"
      Color="Green"
      RelativeLayout.XConstraint="{ConstraintExpression Type=Constant, Constant=0}"
      RelativeLayout.YConstraint="{ConstraintExpression Type=Constant, Constant=0}"
      RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height,  Factor={StaticResource headingHeightFactor}}"
      RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width,  Factor=1.0}"
    />
  </RelativeLayout>
</ContentPage>
Comment 1 Rajneesh Kumar 2015-07-02 07:41:15 UTC
Created attachment 11819 [details]
Sample created

I have checked this issue and bale to reproduce this. To reproduce this issue i have followed the steps and instruction provided in bug description.

I have created a sample with the help of code provided in bug description and I have attached it.

Steps to reproduce:

1. Open attached test sample in XS.
2. Build and deploy it on emulator.
3. Observed that it will throw run time exception.

Screencast: http://www.screencast.com/t/sMdxTfdVY

Application Output: https://gist.github.com/Rajneesh360Logica/f28ed44b8aaf000f3146
Build Output: https://gist.github.com/Rajneesh360Logica/447b2b9bcf671b993094
Ide Logs: https://gist.github.com/Rajneesh360Logica/6da3db90a4e68c25cd94

Environment Info:

=== Xamarin Studio ===

Version 5.9.4 (build 5)
Installation UUID: 08cac54d-8d5b-406e-adf0-707e52f76272
Runtime:
	Microsoft .NET 4.0.30319.18408
	GTK+ 2.24.22 (MS-Windows theme)
	GTK# 2.12.26

=== Xamarin.Android ===

Version: 5.1.4 (Business Edition)
Android SDK: E:\android-sdk
	Supported Android versions:
		2.3    (API level 10)
		4.0.3  (API level 15)
		4.2    (API level 17)
		4.3    (API level 18)
		4.4    (API level 19)
		4.4.87 (API level 20)
		5.0    (API level 21)
Java SDK: C:\Program Files (x86)\Java\jdk1.6.0_39
java version "1.6.0_39"
Java(TM) SE Runtime Environment (build 1.6.0_39-b04)
Java HotSpot(TM) Client VM (build 20.14-b01, mixed mode)

=== Xamarin Android Player ===

Version: 0.3.7.2
Location: C:\Program Files\Xamarin Android Player\DeviceManager.exe

=== Build Information ===

Release ID: 509040005
Git revision: 8010a90f6e246b32364e3fb46ef2c9d1be9c9a2b
Build date: 2015-06-08 17:16:25-04
Xamarin addins: 7e93e9c3503f28770f23ce1b7eafd829919f18e8

=== Operating System ===

Windows 6.2.9200.0 (64-bit)
Comment 2 Stephane Delcroix 2015-07-06 03:58:41 UTC
You can only use {DynamicResource} to assign value to BindableProperties. ConstraintExpression.Factor is a "normal" property, not backed up by any BindableProperty.

In your case, you can use a StaticResource, and that'll work like a charm.