Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xaml 表单绑定到绑定属性或其他东西_Xaml_Xamarin_Xamarin.forms - Fatal编程技术网

Xaml 表单绑定到绑定属性或其他东西

Xaml 表单绑定到绑定属性或其他东西,xaml,xamarin,xamarin.forms,Xaml,Xamarin,Xamarin.forms,我有一页 <?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:controls="clr-namespace:PassSystem.Controls;assemb

我有一页

<?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:controls="clr-namespace:PassSystem.Controls;assembly=PassSystem"
             x:Class="PassSystem.Views.CreatePassPage"
             Title="Оформление пропуска">
    <ScrollView HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
        <StackLayout>
            <FlexLayout Direction="Column" BackgroundColor="White">
                <controls:ActionOption Title="Название" Value="1" LeftMargin="18">
                </controls:ActionOption>
            </FlexLayout>
        </StackLayout>
    </ScrollView>
</ContentPage>
补充资料。点击效果

{
        public ClickableEffect() : base("PassSystem.ClickableEffect")
        {
        }

        #region Click

        public static readonly BindableProperty ClickCommandProperty = BindableProperty
            .CreateAttached("ClickCommand", typeof(ICommand), typeof(ClickableEffect), (object)null);

        public static ICommand GetClickCommand(BindableObject view)
        {
            return (ICommand)view.GetValue(ClickCommandProperty);
        }

        public static void SetClickCommand(BindableObject view, ICommand value)
        {
            view.SetValue(ClickCommandProperty, value);
        }


        public static readonly BindableProperty ClickCommandParameterProperty = BindableProperty
            .CreateAttached("ClickCommandParameter", typeof(object), typeof(ClickableEffect), (object)null);

        public static object GetClickCommandParameter(BindableObject view)
        {
            return view.GetValue(ClickCommandParameterProperty);
        }

        public static void SetClickCommandParameter(BindableObject view, object value)
        {
            view.SetValue(ClickCommandParameterProperty, value);
        }

        #endregion
}
Android上可点击效果的实现:

public class ClickableListener : Java.Lang.Object, View.IOnTouchListener, View.IOnClickListener, View.IOnLongClickListener
    {
        private Element Element { get; }
        private View View { get; }

        public ClickableListener(Element element, View view)
        {
            Element = element;
            View = view;
        }

        ...

        public void OnClick(View v)
        {
            Tap();
        }

        private void Tap()
        {
            var command = ClickableEffect.GetClickCommand(Element);
            var parameter = ClickableEffect.GetClickCommandParameter(Element);
            command?.Execute(parameter);
        }
    }

[assembly: ResolutionGroupName("PassSystem")]
[assembly: ExportEffect(typeof(AndroidClickableEffect), "ClickableEffect")]
namespace PassSystem.Droid.Native.Effects
{
    public class AndroidClickableEffect : PlatformEffect
    {
        private bool _attached;

        protected override void OnAttached()
        {
            //because an effect can be detached immediately after attached (happens in listview), only attach the handler one time.
            if(!_attached)
            {
                var control = Control ?? Container;

                var listener = new ClickableListener(Element, control);
                control.SetOnClickListener(listener);
                ...
                _attached = true;
            }
        }
}
如何实现这一点?非常感谢。
另外,我很难解释,希望你能理解。:)

为了达到预期的行为:

  • ClickCommand
    必须是
    ClickableEffect
    的附加属性。更多信息可在官方文件中找到:
  • 由于在效果和页面之间有一个
    ContentView
    ,因此必须确保
    ContentView
    BindindContext
    设置正确。恐怕您必须在
    ContentView
    级别上定义
    ClickCommand
    bindable属性,并将其绑定到效果的命令。所以:
    Page.BindingContext.ClickCommand
    =>
    ContentView.ClickCommand
    =>
    ClickableEffect.ClickCommand
    (其中=>是绑定)

  • 这基本上提出了一个问题——为什么需要效果?

    1。是的,我有。2.我试图实现它。在ViewModel的页面上,我创建了一个ICommand“ClickCommand”,在ContentView中也创建了“ClickCommand”。但这对我不起作用。很可能是我错误地连接了数据。因为通过ContentView->СlickbleEffect的绑定,它可以工作。我需要这种效果来识别手势。很可能您没有将ContentView的BindingContext设置为self。不,我编写了BindingContext=this。不要在ContentView级别执行此操作,而是在使用此效果的按钮上执行此操作。可以更详细吗?ContentView本身就是我的按钮。
    <controls:ActionOption Title="TitleHere" Value="1" LeftMargin="18" Clicked="{Binding ClickedCommand}">
    </controls:ActionOption>
    
    `effects:ClickableEffect.ClickCommand="{Binding ClickCommand (FromPage)}"`
    
    {
            public ClickableEffect() : base("PassSystem.ClickableEffect")
            {
            }
    
            #region Click
    
            public static readonly BindableProperty ClickCommandProperty = BindableProperty
                .CreateAttached("ClickCommand", typeof(ICommand), typeof(ClickableEffect), (object)null);
    
            public static ICommand GetClickCommand(BindableObject view)
            {
                return (ICommand)view.GetValue(ClickCommandProperty);
            }
    
            public static void SetClickCommand(BindableObject view, ICommand value)
            {
                view.SetValue(ClickCommandProperty, value);
            }
    
    
            public static readonly BindableProperty ClickCommandParameterProperty = BindableProperty
                .CreateAttached("ClickCommandParameter", typeof(object), typeof(ClickableEffect), (object)null);
    
            public static object GetClickCommandParameter(BindableObject view)
            {
                return view.GetValue(ClickCommandParameterProperty);
            }
    
            public static void SetClickCommandParameter(BindableObject view, object value)
            {
                view.SetValue(ClickCommandParameterProperty, value);
            }
    
            #endregion
    }
    
    public class ClickableListener : Java.Lang.Object, View.IOnTouchListener, View.IOnClickListener, View.IOnLongClickListener
        {
            private Element Element { get; }
            private View View { get; }
    
            public ClickableListener(Element element, View view)
            {
                Element = element;
                View = view;
            }
    
            ...
    
            public void OnClick(View v)
            {
                Tap();
            }
    
            private void Tap()
            {
                var command = ClickableEffect.GetClickCommand(Element);
                var parameter = ClickableEffect.GetClickCommandParameter(Element);
                command?.Execute(parameter);
            }
        }
    
    [assembly: ResolutionGroupName("PassSystem")]
    [assembly: ExportEffect(typeof(AndroidClickableEffect), "ClickableEffect")]
    namespace PassSystem.Droid.Native.Effects
    {
        public class AndroidClickableEffect : PlatformEffect
        {
            private bool _attached;
    
            protected override void OnAttached()
            {
                //because an effect can be detached immediately after attached (happens in listview), only attach the handler one time.
                if(!_attached)
                {
                    var control = Control ?? Container;
    
                    var listener = new ClickableListener(Element, control);
                    control.SetOnClickListener(listener);
                    ...
                    _attached = true;
                }
            }
    }