问题
I have class given as below -
public static class ColorResources
{
public static readonly Color ListTextColor = Color.Blue;
}
And any xaml file having control like -
<Button Text="Create Account" TextColor="#000000" BackgroundColor="ListTextColor" Clicked="btnCreateAcc_clicked"/>
Lets Say I want BackgroundColor of button which is declared in my class file. How to make this possible?
回答1:
You can do this by declaring a new namespace in your XAML and use it.
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:color="clr-namespace:MyApp">
<Button Text="Create Account" TextColor="#000000" BackgroundColor="{x:Static color:ColorResources.ListTextColor}" Clicked="btnCreateAcc_clicked"/>
</ContentPage>
Your class should look like this
using Xamarin.Forms;
namespace MyApp
{
public static class ColorResources
{
public static readonly Color ListTextColor = Color.Blue;
}
}
Make sure that the namespace you are declaring in the XAML is the same as the namespace in your class. In this case MyApp
来源:https://stackoverflow.com/questions/37186399/how-to-bind-class-properties-in-xaml-of-xamarin-forms