hvaughan3
hvaughan3

Reputation: 11105

Xamarin Forms XAML Pass Static Value To Simple Data Type

This is the second time something like this has come up for me. Wondering if anyone knows if this is possible in Xamarin Forms' XAML. Something like:

<Color x:Key="Green">{x:Static common:Constants.LightBackgroundColor}</Color>

Obviously the above will not compile. I tried using <Color Accent=""> but it did not like that. I get the following using the code above:

Cannot convert "{x:Static common:Constants.LightBackgroundColor}" into Xamarin.Forms.Color" System.FormatException: Input string was not in a correct format. at System.Text.StringBuilder.AppendFormatHelper(IFormatProvider provider, String format, ParamsArray args) at System.String.FormatHelper(IFormatProvider provider, String format, ParamsArray args) at System.String.Format(IFormatProvider provider, String format, Object[] args) at Microsoft.Build.Framework.LazyFormattedBuildEventArgs.FormatString(CultureInfo culture, String unformatted, Object[] args) VirtualAdvisor C:\Users...

I also tried doing something with x:Arguments but then I run into the same issue... if only one of these things had a Value property I could specify or something....

Also, I realize that I could just put the real hex string or Color value in but because of how the different projects are being referenced I need to keep the Color value outside of my Xamarin Forms shared project but would still like to access the Color values from App.xaml, maybe I need to just give up on App.xaml at this point, at least for these Color values.

Upvotes: 2

Views: 857

Answers (1)

EvZ
EvZ

Reputation: 12179

Interesting issue, reproduced it successfully.
When you put x:Static ... inside your App.xaml resource dictionary you get:

Error MSB4018: The "XamlCTask" task failed unexpectedly.
This is an unhandled exception from a task -- PLEASE OPEN A BUG AGAINST THE TASK OWNER.
System.FormatException: Input string was not in a correct format.


Server stack trace: 
  at System.Text.StringBuilder.AppendFormatHelper (System.IFormatProvider provider, System.String format, System.ParamsArray args) [0x000e9] in /private/tmp/source-mono-4.8.0/bockbuild-mono-4.8.0-branch/profiles/mono-mac-xamarin/build-root/mono-x86/mcs/class/referencesource/mscorlib/system/text/stringbuilder.cs:1371 
  at System.String.FormatHelper (System.IFormatProvider provider, System.String format, System.ParamsArray args) [0x00011] in /private/tmp/source-mono-4.8.0/bockbuild-mono-4.8.0-branch/profiles/mono-mac-xamarin/build-root/mono-x86/mcs/class/referencesource/mscorlib/system/string.cs:2978 
  at System.String.Format (System.String format, System.Object[] args) [0x00021] in /private/tmp/source-mono-4.8.0/bockbuild-mono-4.8.0-branch/profiles/mono-mac-xamarin/build-root/mono-x86/mcs/class/referencesource/mscorlib/system/string.cs:2943 
  at Microsoft.Build.Utilities.TaskLoggingHelper.FormatString (System.String unformatted, System.Object[] args) [0x00021] in /private/tmp/source-mono-4.8.0/bockbuild-mono-4.8.0-branch/profiles/mono-mac-xamarin/build-root/mono-x86/mcs/class/Microsoft.Build.Utilities/Microsoft.Build.Utilities/TaskLoggingHelper.cs:93 
  at Microsoft.Build.Utilities.TaskLoggingHelper.LogError (System.String subcategory, System.String errorCode, System.String helpKeyword, System.String file, System.Int32 lineNumber, System.Int32 columnNumber, System.Int32 endLineNumber, System.Int32 endColumnNumber, System.String message, System.Object[] messageArgs) [0x00026] in /private/tmp/source-mono-4.8.0/bockbuild-mono-4.8.0-branch/profiles/mono-mac-xamarin/build-root/mono-x86/mcs/class/Microsoft.Build.Utilities/Microsoft.Build.Utilities/TaskLoggingHelper.cs:134 
  at (wrapper remoting-invoke-with-check) Microsoft.Build.Utilities.TaskLoggingHelper:LogError (string,string,string,string,int,int,int,int,string,object[])
  at Xamarin.Forms.Build.Tasks.XamlCTask.LogError (System.String subcategory, System.String errorCode, System.String helpKeyword, System.String file, System.Int32 lineNumber, System.Int32 columnNumber, System.Int32 endLineNumber, System.Int32 endColumnNumber, System.String message, System.Object[] messageArgs) [0x00024] in <8cb029844f1042e6a4a06f623985b8f8>:0 
  at Xamarin.Forms.Build.Tasks.XamlCTask.LogException (System.String subcategory, System.String errorCode, System.String helpKeyword, System.String file, System.Exception e) [0x0004e] in <8cb029844f1042e6a4a06f623985b8f8>:0 
  at Xamarin.Forms.Build.Tasks.XamlCTask.Compile (System.Collections.Generic.IList`1[T] thrownExceptions) [0x00583] in <8cb029844f1042e6a4a06f623985b8f8>:0 
  at Xamarin.Forms.Build.Tasks.XamlCTask.Execute () [0x00007] in <8cb029844f1042e6a4a06f623985b8f8>:0 
  at (wrapper xdomain-dispatch) Xamarin.Forms.Build.Tasks.XamlCTask:Execute (object,byte[]&,byte[]&)

Exception rethrown at [0]: 
  at (wrapper xdomain-invoke) Xamarin.Forms.Build.Tasks.XamlCTask:Execute ()
  at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute () [0x0002a] in <6f3322fc0bd04a64a6c8db7c8cfce40d>:0 
  at Microsoft.Build.BackEnd.TaskBuilder+<ExecuteInstantiatedTask>d__26.MoveNext () [0x002bf] in <6f3322fc0bd04a64a6c8db7c8cfce40d>:0  (MSB4018) (staticx)

However if you include x:Static on a ContentPage xaml for example it works without any problem.

In this case I would define static properties in a separate class, (like you have already) and access them directly, without defining them in App.Xaml

P.S.: To catch XAML errors at compilation time you have to add one line to AssemblyInfo.cs :

[assembly: XamlCompilation(XamlCompilationOptions.Compile)]

Edit:
For me the x:Static tag in app xaml breaking the compilation, however I found a workaround. If you add your color programmatically it works. Example:

Defenetion of static value:

public class Constants
{
    public static string StaticColor = "#456654";
}

App.xaml.cs:

public App()
{
    InitializeComponent();


    if (Current.Resources == null)
    {
        Current.Resources = new ResourceDictionary();
    }

    Current.Resources.Add("staticValue" ,Color.FromHex(Constants.StaticColor));

    MainPage = new TestPage();
}

Usage:

<Label TextColor="{x:StaticResource staticValue}" Text="Welcome to Xamarin Forms!" />

Upvotes: 1

Related Questions