How to bind class properties in XAML of xamarin forms

南笙酒味 提交于 2019-12-24 00:35:43

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!