Xamarin Forms XAML 将静态值传递给简单数据类型



这是我第二次遇到这样的事情。想知道是否有人知道这在 Xamarin Forms 的 XAML 中是否可行。像这样:

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

显然以上是无法编译的。我尝试使用<Color Accent="">但它不喜欢那样。我使用上面的代码得到以下内容:

无法将"{x:静态通用:常量.LightBackgroundColor}"转换为 Xamarin.Forms.Color" System.FormatException: 输入字符串不在 格式正确。 在 System.Text.StringBuilder.AppendFormatHelper(IFormatProvider provider, 字符串格式,ParamsArray args) at System.String.FormatHelper(IFormatProvider provider, String format, ParamsArray args) at System.String.Format(IFormatProvider provider, 字符串格式,对象[] 参数) at Microsoft.Build.Framework.LazyFormattedBuildEventArgs.FormatString(CultureInfo 区域性, 字符串未格式化, 对象[] args) VirtualAdvisor C:\Users...

我也尝试用x:Arguments做一些事情,但后来我遇到了同样的问题......如果这些东西中只有一个具有我可以指定的Value属性或其他东西......

另外,我意识到我可以只放入真正的十六进制stringColor值,但由于引用不同项目的方式,我需要将Color值保留在我的 Xamarin Forms 共享项目之外,但仍想从App.xaml访问颜色值,也许我需要在这一点上放弃App.xaml, 至少对于这些Color值。

有趣的问题,成功重现了它。
x:Static ...放入 App.xaml 资源字典时,将获得:

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)

但是,如果您在ContentPage xaml上包含x:Static,例如,它可以毫无问题地工作。

在这种情况下,我将在单独的类中定义静态属性(就像您已经拥有的那样)并直接访问它们,而无需在 App.Xaml 中定义它们

附言: 若要在编译时捕获 XAML 错误,必须向AssemblyInfo.cs添加一行:

[assembly: XamlCompilation(XamlCompilationOptions.Compile)]

编辑:
对我来说,应用程序xaml中的x:Static标签破坏了编译,但是我找到了解决方法。如果以编程方式添加颜色,它将起作用。例:

静态值的解除:

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

用法:

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

最新更新