简体   繁体   中英

how to fix unworked xmlns?

In a .NET MAUI project, I made a class and I want to consume it in XAML file, so I added a xmlns but this namespace can't access the class.

I'm trying to access the class through this namespace:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:HossamTest1.behaviorsfolder"
             x:Class="HossamTest1.BehaviorPage"             
             Title="BehaviorPage">

    <StackLayout>
        <Entry local:NumericBehaviorValidation/>
        <!-- here it gives an error-->

    </StackLayout>
</ContentPage>

here I am trying to add custom behavior to an entry but the problem is repeated in all other projects for all classes of the named namespace, all classes are public, I update my visual studio itself but the problem still, the c# code is

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HossamTest1.behaviorsfolder
{
    public static class NumericBehaviorValidation
    {
        public static readonly BindableProperty NumricValidationProperty =
                    BindableProperty.CreateAttached("NumricValidation",typeof(bool),typeof(NumericBehaviorValidation),false,propertyChanged:OnValidation );
        public static bool getNumricValidationProperty(BindableObject view)
        {
            return (bool)view.GetValue(NumricValidationProperty);
        }
        public static void setNumricValidationProperty(BindableObject view,bool value)
        {
            view.SetValue(NumricValidationProperty,value);
        }
        public static void OnValidation(BindableObject view, object  oldvalue,object newvalue)
        {
            Entry entry=view as Entry;
            if (entry == null)
            {
                return;
            }
            bool valid = (bool)newvalue;
            if (valid) 
            {entry.TextChanged+= OnValidationHandler;
            }
            else
            {
                entry.TextChanged -= OnValidationHandler;
            }
        }
        static void OnValidationHandler(object sender, TextChangedEventArgs e)
        {
            Entry entry=sender as Entry;
            double result;
            bool isvalid=double.TryParse(e.NewTextValue, out result);
            entry.TextColor=isvalid?Colors.Green:Colors.Red;
        }
    }
}

then I made a blank new project to test this issue

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:HOSSAM1"
             x:Class="HOSSAM1.MainPage">

    <ScrollView>
        <VerticalStackLayout
            Spacing="25"
            Padding="30,0"
            VerticalOptions="Center">


            <Label
                Text="Hello, World!"
                SemanticProperties.HeadingLevel="Level1"
                FontSize="32"
                HorizontalOptions="Center" 
               local:Behavoirs/>



        </VerticalStackLayout>
    </ScrollView>

</ContentPage>

the class Behavior is an empty public class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HOSSAM1
{
    public class Behaviors
    {
    }
}      

the error message for first project is

Error XLS0415 The attachable property 'NumricValidation' was not found in type 'NumericBehaviorValidation'. HossamTest1 D:\hossam\backup2\HossamTest1\HossamTest1\BehaviorPage.xaml 12

the problem was that I made a space between the colon and the namespace name

 <Entry local: NumericBehaviorValidation/>

it should be

<Entry local:NumericBehaviorValidation/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM