如何在Xamarin形式的XAML中绑定类属性
问题描述:
我的课有以下内容-
public static class ColorResources
{
public static readonly Color ListTextColor = Color.Blue;
}
任何具有控制权的xaml文件-
And any xaml file having control like -
<Button Text="Create Account" TextColor="#000000" BackgroundColor="ListTextColor" Clicked="btnCreateAcc_clicked"/>
让我们说我想要在类文件中声明的Button的BackgroundColor.如何做到这一点?
Lets Say I want BackgroundColor of button which is declared in my class file. How to make this possible?
答
您可以通过在XAML中声明一个新的名称空间并使用它来实现.
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;
}
}
确保在XAML中声明的名称空间与类中的名称空间相同.在这种情况下, MyApp
Make sure that the namespace you are declaring in the XAML is the same as the namespace in your class. In this case MyApp