Reputation: 52
I am trying to use a usercontrol defined outside the solution by referring the dll. It works if I use code; but not XAML
Here is the code - 1. UserControl code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xamarin.Forms;
using Xamarin.Forms.Internals;
namespace ComplexControl
{
[Preserve]
public class ComplexControl : ContentView
{
public ComplexControl()
{
var button = new Button
{
Text = "Click Me!",
//VerticalOptions = LayoutOptions.CenterAndExpand,
HorizontalOptions = LayoutOptions.FillAndExpand,
};
int clicked = 0;
button.Clicked += (s, e) => button.Text = "Clicked: " + clicked++;
Content = button;
}
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
class PreserveAttribute : Attribute { }
}
}
It works with code ( ie. not using XAML )
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using ComplexControl;
namespace CustomControlUsageExample
{
public partial class MainPage : ContentPage
{
public MainPage()
{
// without InitializeComponent(); works
//this way works
//Step 1.
StackLayout sl = new StackLayout();
Label lb = new Label();
lb.Text = "Hello";
sl.Children.Add(lb);
sl.Children.Add(new ComplexControl.ComplexControl());
Content = sl;
}
}
}
If I use with XAML it does not work
Like this
XAML is
<?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:CustomControlUsageExample"
x:Class="CustomControlUsageExample.MainPage"
xmlns:custom="clr-namespace:ComplexControl;assembly:ComplexControl">
<StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<Label Text="Welcome to Xamarin Forms!"/>
<custom:ComplexControl.ComplexControl/>
</StackLayout>
</ContentPage>
And code behind for that is
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using ComplexControl;
namespace CustomControlUsageExample
{
public partial class MainPage : ContentPage
{
public MainPage()
{
//This way does not work
//Step 2
InitializeComponent();
}
}
}
I have already tried to debug thru and do not see it add the user control to content.
Can someone help me see what I am doing wrong? It is works if I don't use XAML for the custom control.
I have a usercontrol work being done where the userControl is done using code; but it will be referred by projects inside XAML.
Thanks,
Upvotes: 2
Views: 467
Reputation: 3101
XP, you are doing everything correctly.
Unfortunately, small mistype (: instead of =) led you to this problem with XAML-defined control.
xmlns:custom="clr-namespace:ComplexControl;assembly:ComplexControl"
should be
xmlns:custom="clr-namespace:ComplexControl;assembly=ComplexControl"
Syntax for custom XAML namespace is clr-namespace:NAMESPACE;assembly=ASSEMBLY
I believe Xamarin.Forms should be smarter about namespaces and warn you about such mistypes in the editor or during build time (Xamarin.Forms does not compile XAML by default but nobody stops it from running simple syntax check, right?).
If you want, you can file a bug to Xamarin (it is easy!)
Upvotes: 1