Как связать свойство статического класса с компонентом пользовательского интерфейса в XAML / Xamarin - PullRequest
0 голосов
/ 04 июля 2018

Мне нужна помощь специалиста. Я пишу приложение Xamarin. Я не могу привязать статическое свойство определенного пользователем статического свойства C # (Colors. BackgroundColor) к XAML. На самом деле мне нужно установить цвет фона сетки статическим значением, определенным в статическом классе. Но это всегда дает мне ошибку Class not found в строке

Я получаю сообщение об ошибке типа UserInterfaceDefinitions не найден в xmlns

BackgroundColor = "{Binding Source = {x: Static Circassia.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?

Привет

Susheel

Ответы [ 2 ]

0 голосов
/ 04 июля 2018

У меня есть разрешения. Это было из-за того, что класс Nested Static был недоступен внутри 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>
0 голосов
/ 04 июля 2018

Для привязки к статическому свойству:

1) Объявите пространство имен для импорта, используя xmlns

2) Используйте xmlns соответственно в Source

=>

<?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";
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...