Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.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
C# 如何使用";。isFocused";ViewModel中我的条目的属性?_C#_Xamarin.forms - Fatal编程技术网

C# 如何使用";。isFocused";ViewModel中我的条目的属性?

C# 如何使用";。isFocused";ViewModel中我的条目的属性?,c#,xamarin.forms,C#,Xamarin.forms,在我的主页中,我使用一个自定义控件,它是一个条目和两个按钮。我需要对ViewModel中的条目使用“.isFocused”属性来执行某些操作,我该如何执行 我必须得到x:条目的名称?我不知道我能不能这样做,但我需要使用.is聚焦于条目,这样当我点击条目时,我拥有的菜单就会关闭 我的代码: CustomContol.xaml: 您可以使用EventToCommand行为 定义以下类 public class EventToCommandBehavior : BehaviorBase<Vis

在我的主页中,我使用一个自定义控件,它是一个条目和两个按钮。我需要对ViewModel中的条目使用“.isFocused”属性来执行某些操作,我该如何执行

我必须得到x:条目的名称?我不知道我能不能这样做,但我需要使用.is聚焦于条目,这样当我点击条目时,我拥有的菜单就会关闭

我的代码:

CustomContol.xaml:


您可以使用EventToCommand行为

定义以下类

public class EventToCommandBehavior : BehaviorBase<VisualElement>
{
    Delegate eventHandler;

    public static readonly BindableProperty EventNameProperty = BindableProperty.Create("EventName", typeof(string), typeof(EventToCommandBehavior), null, propertyChanged: OnEventNameChanged);
    public static readonly BindableProperty CommandProperty = BindableProperty.Create("Command", typeof(ICommand), typeof(EventToCommandBehavior), null);
    public static readonly BindableProperty CommandParameterProperty = BindableProperty.Create("CommandParameter", typeof(object), typeof(EventToCommandBehavior), null);
    public static readonly BindableProperty InputConverterProperty = BindableProperty.Create("Converter", typeof(IValueConverter), typeof(EventToCommandBehavior), null);

    public string EventName
    {
        get { return (string)GetValue(EventNameProperty); }
        set { SetValue(EventNameProperty, value); }
    }

    public ICommand Command
    {
        get { return (ICommand)GetValue(CommandProperty); }
        set { SetValue(CommandProperty, value); }
    }

    public object CommandParameter
    {
        get { return GetValue(CommandParameterProperty); }
        set { SetValue(CommandParameterProperty, value); }
    }

    public IValueConverter Converter
    {
        get { return (IValueConverter)GetValue(InputConverterProperty); }
        set { SetValue(InputConverterProperty, value); }
    }

    protected override void OnAttachedTo(VisualElement bindable)
    {
        base.OnAttachedTo(bindable);
        RegisterEvent(EventName);
    }

    protected override void OnDetachingFrom(VisualElement bindable)
    {
        DeregisterEvent(EventName);
        base.OnDetachingFrom(bindable);
    }

    void RegisterEvent(string name)
    {
        if (string.IsNullOrWhiteSpace(name))
        {
            return;
        }

        EventInfo eventInfo = AssociatedObject.GetType().GetRuntimeEvent(name);
        if (eventInfo == null)
        {
            throw new ArgumentException(string.Format("EventToCommandBehavior: Can't register the '{0}' event.", EventName));
        }
        MethodInfo methodInfo = typeof(EventToCommandBehavior).GetTypeInfo().GetDeclaredMethod("OnEvent");
        eventHandler = methodInfo.CreateDelegate(eventInfo.EventHandlerType, this);
        eventInfo.AddEventHandler(AssociatedObject, eventHandler);
    }

    void DeregisterEvent(string name)
    {
        if (string.IsNullOrWhiteSpace(name))
        {
            return;
        }

        if (eventHandler == null)
        {
            return;
        }
        EventInfo eventInfo = AssociatedObject.GetType().GetRuntimeEvent(name);
        if (eventInfo == null)
        {
            throw new ArgumentException(string.Format("EventToCommandBehavior: Can't de-register the '{0}' event.", EventName));
        }
        eventInfo.RemoveEventHandler(AssociatedObject, eventHandler);
        eventHandler = null;
    }

    void OnEvent(object sender, object eventArgs)
    {
        if (Command == null)
        {
            return;
        }

        object resolvedParameter;
        if (CommandParameter != null)
        {
            resolvedParameter = CommandParameter;
        }
        else if (Converter != null)
        {
            resolvedParameter = Converter.Convert(eventArgs, typeof(object), null, null);
        }
        else
        {
            resolvedParameter = eventArgs;
        }

        if (Command.CanExecute(resolvedParameter))
        {
            Command.Execute(resolvedParameter);
        }
    }

    static void OnEventNameChanged(BindableObject bindable, object oldValue, object newValue)
    {
        var behavior = (EventToCommandBehavior)bindable;
        if (behavior.AssociatedObject == null)
        {
            return;
        }

        string oldEventName = (string)oldValue;
        string newEventName = (string)newValue;

        behavior.DeregisterEvent(oldEventName);
        behavior.RegisterEvent(newEventName);
    }
}
在ContentPage中
这回答了你的问题吗?请看那篇文章中被接受的答案,它应该会有所帮助。此外,将您的虚拟机和您尝试过的内容包括在内也会有所帮助,这样我们就可以进一步帮助您。您可以将
IsFocused
绑定到虚拟机中的属性,或者使用
Focused
事件
public class BehaviorBase<T> : Behavior<T> where T : BindableObject
{
    public T AssociatedObject { get; private set; }

    protected override void OnAttachedTo(T bindable)
    {
        base.OnAttachedTo(bindable);
        AssociatedObject = bindable;

        if (bindable.BindingContext != null)
        {
            BindingContext = bindable.BindingContext;
        }

        bindable.BindingContextChanged += OnBindingContextChanged;
    }

    protected override void OnDetachingFrom(T bindable)
    {
        base.OnDetachingFrom(bindable);
        bindable.BindingContextChanged -= OnBindingContextChanged;
        AssociatedObject = null;
    }

    void OnBindingContextChanged(object sender, EventArgs e)
    {
        OnBindingContextChanged();
    }

    protected override void OnBindingContextChanged()
    {
        base.OnBindingContextChanged();
        BindingContext = AssociatedObject.BindingContext;
    }
}
<StackLayout>
            <Entry
                        x:Name="EntryControl"
                        Grid.Column="1"
                        Margin="0,0,50,0"
                        HorizontalOptions="Fill"
                        Keyboard="Chat"
                        Placeholder="{Binding Placeholder}"
                        Text="{Binding EntryText}"
                        TextColor="Black" >

                <Entry.Behaviors>
                    <local:EventToCommandBehavior EventName="Focused" Command="{Binding Source={x:Reference CKEditorView},Path=FocusedCommand}" CommandParameter="True"/>
                    <local:EventToCommandBehavior EventName="Unfocused" Command="{Binding Source={x:Reference CKEditorView},Path=FocusedCommand}" CommandParameter="False"/>
                </Entry.Behaviors>

            </Entry>
        </StackLayout>
public static readonly BindableProperty FocusedCommandProperty =
                   BindableProperty.Create(nameof(FocusedCommand), typeof(ICommand), typeof(CKEditor));

public ICommand FocusedCommand
{
   get => (ICommand)GetValue(FocusedCommandProperty);
   set => SetValue(FocusedCommandProperty, value);
}
 <local:CKEditor  FocusedCommand="{Binding FocusedCommand}" />
public ICommand FocusedCommand { get; set; }
FocusedCommand = new Command((arg)=> {

    bool isFocused = (bool)arg;
            
    if(isFocused)
    {
         //focused
    }

    else
    {
       //unfocused
    }
});