如何在 XAML / Xamarin 中将静态类属性绑定到 UI 组件

5
在Xamarin应用程序中,我无法将C#用户定义的静态类属性(Colors.BackgroundColor)绑定到XAML。 我需要通过静态类中定义的静态值来设置网格的颜色背景。

但是我收到以下错误消息

在xmlns中找不到类型UserInterfaceDefinitions

出现在此XAML上

BackgroundColor = "{Binding Source = {x:Static MyNamespace.Mobile:UserInterfaceDefinitions.Colors} }"

静态类代码

namespace MyNamespace.Mobile
{
    public static class UserInterfaceDefinitions
    {
        public static class Colors
        {
            public static string BackgroundColor = "#DCECE";
        }

        
    }
}

XAML 代码

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
              xmlns:buttons="clr-namespace:MyNamespace.Mobile.UI.Buttons" 
              xmlns:Status="clr-namespace:MyNamespace.Mobile.UI.StatusDetails"     
             x:Class="MyNamespace.Mobile.UI.TestAndDemoSelection">
    <ContentPage.Content  Margin="0,0,0,0" BackgroundColor="White">

    
 <Grid x:Name="ChildGrid" Grid.Row="1" Grid.Column="0"  ColumnSpacing="10" BackgroundColor="White" >
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"></ColumnDefinition> 
                </Grid.ColumnDefinitions>
            
            <!-- I am getting the error as Type UserInterfaceDefinitions not found in xmlns-->

            <BoxView Grid.Column="0" BackgroundColor = "{Binding Source = {x:Static MyNamespace.Mobile:UserInterfaceDefinitions.Colors} }" /> 
         
 </Grid>     
 
    </ContentPage.Content>
</ContentPage>

后台代码 .cs

using MyNamespace.Mobile.UI.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace MyNamespace.Mobile.UI
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class TestAndDemoSelection : ContentPage
    {
        public TestAndDemoSelection()
        {
            InitializeComponent();
        }
 
    }
}

如何将静态类属性绑定到XAML?
3个回答

5

我已经解决了这个问题。原因是XAML中无法访问嵌套静态类,正确的代码如下:

用户定义的静态类:

namespace MyNamespace.Mobile
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public static class UserInterfaceDefinitions
    {
        public static string BackgroundColor { get; } = "#DCECEC";
    }
}

XAML文件:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
              xmlns:local="clr-namespace:MyNamespace.Mobile"   
             x:Class="MyNamespace.Mobile.UI.TestAndDemoSelection">
    <ContentPage.Content  Margin="0,0,0,0" BackgroundColor="White">


 <Grid x:Name="ChildGrid" Grid.Row="1" Grid.Column="0"  ColumnSpacing="10" BackgroundColor="White" >
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"></ColumnDefinition> 
                </Grid.ColumnDefinitions>


           <BoxView Grid.Column="0" BackgroundColor = "{Binding Source = {x:Static local:UserInterfaceDefinitions.BackgroundColor}}" />

 </Grid>     

    </ContentPage.Content>
</ContentPage>

你的答案与原始代码有多种不同之处。如果你这样做,原始嵌套静态类就可以工作了:BackgroundColor="{Binding Source={x:Static local:UserInterfaceDefinitions+Colors.BackgroundColor}}"。请注意使用 + 表示类嵌套。 - ToolmakerSteve

1
XAML与嵌套类的兼容性非常差。 是的,通常情况下,公共嵌套类经常是一种非常糟糕的技术。
示例:
namespace MyNamespace.Mobile
{
    public static class Colors
    {
        public static string BackgroundColor { get; } = "Red";
    }

}

XAML:

<StackPanel xmlns:Circassia.Mobile="clr-namespace:MyNamespace.Mobile"
    Background ="{Binding Source={x:Static Circassia.Mobile:Colors.BackgroundColor}}"/>

第二个例子:
namespace MyNamespace.Mobile
{
    public static class UserInterfaceDefinitions
    {
        public static ColorsClass  Colors{ get; } = new ColorsClass();

        public class ColorsClass
        {
            private static readonly string s_BackgroundColor = "Red";
            public static string BackgroundColor { get; } = s_BackgroundColor;
        }

    }
}

XAML:
<StackPanel xmlns:Circassia.Mobile="clr-namespace:MyNamespace.Mobile"
    Background ="{Binding BackgroundColor, Source={x:Static Circassia.Mobile:UserInterfaceDefinitions.Colors}}"/>

“XAML在处理嵌套类方面表现非常糟糕。” - 根据您尝试引用嵌套类的位置,有时只需使用OuterClass+InnerClass而不是OuterClass.InnerClass就可以解决问题。 - ToolmakerSteve

1
为了绑定到静态属性:
1)使用xmlns声明要导入的命名空间
2)在源中相应地使用xmlns
= >
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
              xmlns:buttons="clr-namespace:MyNamespace.Mobile.UI.Buttons" 
              xmlns:Status="clr-namespace:MyNamespace.Mobile.UI.StatusDetails" 
             xmnlns:local="clr-namespace:MyNamespace.Mobile" 
             x:Class="MyNamespace.Mobile.UI.TestAndDemoSelection">
    <ContentPage.Content  Margin="0,0,0,0" BackgroundColor="White">


         <Grid x:Name="ChildGrid" Grid.Row="1" Grid.Column="0"  ColumnSpacing="10" BackgroundColor="White" >
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"></ColumnDefinition> 
                </Grid.ColumnDefinitions>


            <BoxView Grid.Column="0" BackgroundColor = "{x:Static local:UserInterfaceDefinitions.Colors.BackgroundColor}" /> 

         </Grid>     

    </ContentPage.Content>
</ContentPage>

此外,为了使其可访问,BackgroundColor应该是一个属性:
public static string BackgroundColor {get;} = "#DCECE";

嗨,感谢您的回复。我已根据您的评论进行了更新,但仍然出现以下错误:error: 在xmlns clr-namespace:MyNamespace.Mobile;assembly=MyNamespace.Mobile中未找到类型UserInterfaceDefinitions.Colors。 - skt
好的,也许还有一件事,BackgroundColor 应该是一个属性。请看我的编辑。 - Rudy Spano

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接