如何在Xamarin形式的XAML中绑定类属性 [英] How to bind class properties in XAML of xamarin forms
本文介绍了如何在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
这篇关于如何在Xamarin形式的XAML中绑定类属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文