跨平台Xamarin表单的UserControl

跨平台Xamarin表单的UserControl

问题描述:

我已经寻找了很长时间,但仍然没有找到创建WPF的方法,例如针对跨平台Xamarin Forms的UserControls.可以做到吗?我在Visual Studio 2013中使用Xamarin.

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.

这里是一个XAML示例.只需添加即可,您可以具有任何基本的视觉元素.例如.网格,StackLayout,图像等.

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>

这是背后的代码

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">

然后在页面

<control:UserControl />