简体   繁体   中英

XamarinForms: Use a Control from a Custom Renderer in XAML

I created my own custom renderer in Xamarin that looks like so:

namespace TestApp
{
    public class CustomEntry : Entry
    {
        public CustomEntry ()
        {
        }
    }
}

How can I include this in my HomePage.xaml file? I tried this:

<?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:TestApp;assembly:TestApp" 
x:Class="TestApp.SubPage">
    <ContentPage.Content>
        <StackLayout>
            <local:CustomEntry></local:CustomEntry>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

But it didn't work, saying CustomEntry is not a valid control in the " http://xamarin.com/schemas/2014/forms " namespace. Any ideas?

Try changing <x:local="clr-namespace:TestApp;assembly:TestApp"> into <xmlns:local="clr-namespace:TestApp;assembly:TestApp">

And <x:local:CustomEntry> into <local:CustomEntry>

Nevermind, I found the problem. It seems that the xmlns:local declaration I had was wrong. I had this:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
xmlns:local="clr-namespace:TestApp;assembly:TestApp" 
x:Class="TestApp.SubPage">

And it was this:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
xmlns:local="clr-namespace:TestApp;assembly=TestApp" 
x:Class="TestApp.SubPage">

Seems that assembly was assigned with the '=' operator, not the ':' one. The class stayed the same and everything works now.

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