Bug 49228

Summary: XAMLC NRE when x:Static is used on a const defined in seperate assembly
Product: Forms Reporter: John Miller [MSFT] <john.miller>
Component: FormsAssignee: Stephane Delcroix <stephane.delcroix>
Severity: normal CC: jas, rohanbethune, sahou, stephane.delcroix
Priority: ---    
Version: 2.3.3   
Target Milestone: ---   
Hardware: PC   
OS: Windows   
Tags: AC XamlC Is this bug a regression?: ---
Last known good build:
Attachments: Sample Project

Description John Miller [MSFT] 2016-12-07 14:22:10 UTC
Created attachment 18810 [details]
Sample Project

** Overview **:

The following scenario causes a null reference exception when XAMLC is used in 2.3.3 at compile time:

// In AppProject1 and has a reference to LibraryProject1
> <Label Text="{x:Static constants2:Globals.AppName}"/>

Where `constants2` is a namespace from another separate referenced library project. 

// In LibraryProject1
> public class Globals
> {
>    public const string AppName = "My Xamarin Forms App 2";
> }

This results in a null reference exception for Globals.AppName.

** Steps to Reproduce **

1. Open the attached project in VS
2. Build the XFApp project

** Actual Results **:

> Object reference not set to an instance of an object.	XFApp	<path>\XFApp\XFApp.Views.MainPage.xaml		

Occasionally, I was getting an error that the XAMLC task failed unexpectedly. However, Rebuilding usually surfaced this NRE.

** Expected Results **:

No NRE at compile time.

** Additional Information **:

Downgrading to the project back to Forms does not reproduce the issue anymore. It only starts happening after updating to

Marking the Globals class as static, and the AppName field as static also works around this. 

Referencing similar code that is included in the same assembly does not cause the issue, see commented out section in MainPage.xaml
Comment 1 rohan bethune 2016-12-07 16:10:27 UTC

Just to confirm , the attached project that I supplied fails in both XF and The issue has nothing to do with Prism as same issue manifests itself outside of Prism. The project builds successfully in with and without XAMLCompilation but fails >= with XAMLCompilation and using x:Static referencing constants in separate project.

Since we can only move to >= without enabling XAMLCompilation it would be great if we can increase priority of this bug as most prism mobile apps are modular in nature and thus do not confine themselves to just referencing statics within same assembly as defining view.
Comment 2 John Miller [MSFT] 2016-12-07 19:43:19 UTC

I removed all Prism stuff from the Sample Project I attached.
Comment 3 rohan bethune 2016-12-07 22:21:12 UTC
yep my *bad* , didnt look at the attachment and thought it was the same attachment I sent you - but your quite right , issue exists regardless of any third party mvvm frameworks..
Comment 4 Stephane Delcroix 2017-01-24 14:04:03 UTC
I thought this PR https://github.com/xamarin/Xamarin.Forms/pull/642 would have solved this, but apparently not :(

It works for public static string, but fails for public const string
Comment 5 Stephane Delcroix 2017-01-24 14:27:15 UTC
fixed in https://github.com/xamarin/Xamarin.Forms/pull/710
Comment 6 Samantha Houts [MSFT] 2017-03-16 01:09:01 UTC
Should be fixed in 2.3.4-pre2. Thanks!