微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Xamarin,如何创建使用Xaml接受内容的csutom控件? 1创建具有可绑定属性的CustomControl 在ContentPage

如何解决Xamarin,如何创建使用Xaml接受内容的csutom控件? 1创建具有可绑定属性的CustomControl 在ContentPage

我正在尝试创建一个自定义控件,该控件为对话框提供一些标准布局,其中右上角(网格内)有一个按钮,该按钮具有命令的属性,可以像将内容传递到其中的StackLayout一样使用它。

但是,我想通过使用xaml(不是代码)为控件指定所有布局来实现这一点。我知道您可以使用ControlTemplates做简单的事情,但是我正在尝试添加命令。

我尝试了几次,然后放弃了。

问题在于设置内容区域,该控件可与内部的各种控件一起使用,并绑定在xaml中实现的命令(在xaml中具有callong BindableProperty)。

解决方法

在您的情况下,您可以使用Bindable Property将值从ViewModel传递到自定义控件。

1。创建具有可绑定属性的CustomControl

<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="App29.MyCustomControl"
             
             x:Name="CustomView"  // set name of view
             
             >
  <ContentView.Content>
      <StackLayout>

  
        </StackLayout>
  </ContentView.Content>
</ContentView>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace App29
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class MyCustomControl : ContentView
    {
        public MyCustomControl()
        {
            InitializeComponent();

            

        }


        public static readonly BindableProperty ButtonClickCommandProperty =
            BindableProperty.Create(nameof(ButtonClickCommand),typeof(ICommand),typeof(MyCustomControl));

        public ICommand ButtonClickCommand
        {
            get => (ICommand)GetValue(ButtonClickCommandProperty);
            set => SetValue(ButtonClickCommandProperty,value);
        }

        public static BindableProperty ButtonClickParameterProperty =
            BindableProperty.Create(nameof(ButtonClickParameter),typeof(object),typeof(MyCustomControl));

        public object ButtonClickParameter
        {
            get => (object)GetValue(ButtonClickParameterProperty);
            set => SetValue(ButtonClickParameterProperty,value);
        }


        public static BindableProperty ButtonTitleProperty =
            BindableProperty.Create(nameof(ButtonTitle),typeof(MyCustomControl),string.Empty);

        public string ButtonTitle
        {
            get => (string)GetValue(ButtonTitleProperty);
            set => SetValue(ButtonTitleProperty,value);
        }


        public static BindableProperty ChildLayoutProperty =
            BindableProperty.Create(nameof(ChildLayout),typeof(View),propertyChanged: OnChildLayoutChanged);

        public View ChildLayout
        {
            get => (View)GetValue(ChildLayoutProperty);
            set => SetValue(ChildLayoutProperty,value);
        }

        static void OnChildLayoutChanged(BindableObject bindable,object oldValue,object newValue)
        {
            // Property changed implementation goes here

            var Custom = bindable as MyCustomControl;

            //child is the value that pass from viewmodel
            var child = newValue as View;

            // do something you want,like add it to stacklayout
            var stack = Custom.Content as StackLayout;

            stack.Children.Add(child);

            

        }

        //...other bindable property
    }
}

在ContentPage

现在,您可以将值从ViewModel传递到自定义控件,也可以像下面的代码一样传递代码

  <local:MyCustomControl ButtonClickCommand="{Binding xxx}" ButtonClickParameter="{Binding xxx}" ButtonTitle="{Binding xxx}" >

        <local:MyCustomControl.Content>

            <Button  />

        </local:MyCustomControl.Content>

    </local:MyCustomControl>

有关可绑定属性的模式详细信息,您可以检查https://docs.microsoft.com/en-us/xamarin/xamarin-forms/xaml/bindable-properties

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。