Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UserControl for cross-platform Xamarin Forms

I have been looking for a long time now but still haven't found a way to create WPF like UserControls for cross-platform Xamarin Forms. Can this even be done? I am using Xamarin with Visual Studio 2013.

like image 277
eYe Avatar asked Jul 29 '15 13:07

eYe


1 Answers

Here is a XAML example. Just to add you can have any base visual element. E.g. a Grid, StackLayout, Image etc.

 <Grid xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Mobile.Control.UserControl">

 </Grid>

Here is the code behind

namespace Mobile.Control
{
    public partial class UserControl : Grid
    {
        public UserControl()
        {
            InitializeComponent();

        }

    }
}

To use it, go to your page and implement the namespace

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
          xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
          xmlns:control="clr-namespace:Mobile.Control;assembly=Mobile"
          x:Class="Mobile.View.MyPage">

And then in the page

<control:UserControl />
like image 179
Adam Avatar answered Nov 17 '22 22:11

Adam