skt*_*skt 5 xaml xamarin.forms
在 Xamarin 应用程序中,我无法将 C# 用户定义的静态类属性 (Colors.BackgroundColor) 的静态属性绑定到 XAML。我需要通过静态类中定义的静态值设置网格颜色的背景。
但我收到错误
在 xmlns 中找不到类型UserInterfaceDefinitions
在此 XAML 上
BackgroundColor = "{Binding Source = {x:Static MyNamespace.Mobile:UserInterfaceDefinitions.Colors} }"
Run Code Online (Sandbox Code Playgroud)
静态类代码
namespace MyNamespace.Mobile
{
public static class UserInterfaceDefinitions
{
public static class Colors
{
public static string BackgroundColor = "#DCECE";
}
}
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
代码隐藏.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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
如何将静态类属性绑定到 XAML?
我已经有了决心。这是因为嵌套静态类在 XAML 中无法访问,正确的代码如下。
用户定义的静态类:
namespace MyNamespace.Mobile
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public static class UserInterfaceDefinitions
{
public static string BackgroundColor { get; } = "#DCECEC";
}
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
为了绑定静态属性:
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>
Run Code Online (Sandbox Code Playgroud)
此外,BackgroundColor 应该是一个属性以便可访问:
public static string BackgroundColor {get;} = "#DCECE";
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5054 次 |
| 最近记录: |