C# Xamarin EventToCommandBehavior不会连接关联的对象

C# Xamarin EventToCommandBehavior不会连接关联的对象,c#,xamarin,xamarin.forms,C#,Xamarin,Xamarin.forms,我在Xamarin,我试图将一个事件转化为一个命令。下面的示例代码是。我遇到的问题是,BehaviorBase的OnAttachedTo覆盖没有触发,这意味着事件连线没有发生 如何使这些命令生效? 第二个问题是,存在此问题时,不会加载整个视图。如果我注释掉XAML,视图就会工作。也许这是个线索?为什么整个视图不会因此而加载?我认为只有行为不起作用,而不是整个视图 详情如下: 我从样本中复制了两个类BehaviorBase和EventToCommandBehavior 我的观点 <?xml

我在Xamarin,我试图将一个事件转化为一个命令。下面的示例代码是。我遇到的问题是,
BehaviorBase
OnAttachedTo
覆盖没有触发,这意味着事件连线没有发生

如何使这些命令生效?

第二个问题是,存在此问题时,不会加载整个视图。如果我注释掉XAML,视图就会工作。也许这是个线索?为什么整个视图不会因此而加载?我认为只有行为不起作用,而不是整个视图

详情如下:

我从样本中复制了两个类
BehaviorBase
EventToCommandBehavior

我的观点

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="MyApp.Views.MyPage" xmlns:v="clr-namespace:MyApp.Views;assembly=MyApp" xmlns:b="clr-namespace:MyApp.Behaviors;assembly=MyApp" xmlns:c="clr-namespace:MyApp.Converters;assembly=MyApp">
    <ContentPage.Behaviors>
        <b:EventToCommandBehavior EventName="Disappearing" Command="{Binding DisappearingCommand}" />
        <b:EventToCommandBehavior EventName="Appearing" Command="{Binding AppearingCommand}" />
    </ContentPage.Behaviors>-->
    <ContentPage.Content>
        <StackLayout>
            <Entry Text="{Binding Name}"></Entry>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>
但这永远不会:

protected override void OnAttachedTo(View bindable)
{
    base.OnAttachedTo(bindable);
    RegisterEvent(EventName);
}
base.OnAttachedTo
是将
behavior.AssociatedObject
设置为某物的工具。这是它的副本:

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

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

    bindable.BindingContextChanged += OnBindingContextChanged;
}

我也面临同样的问题。解决方案是将
EventToCommandBehavior
类的基类从
BehaviorBase
更改为
BehaviorBase
。因此,它应该是以下内容:

公共类事件到CommandBehavior:BehaviorBase
{
...
}

我也面临同样的问题。解决方案是将
EventToCommandBehavior
类的基类从
BehaviorBase
更改为
BehaviorBase
。因此,它应该是以下内容:

公共类事件到CommandBehavior:BehaviorBase
{
...
}

您在哪里将ViewModel的实例设置为页面的BindingContext?它在别处,是通常的代码
var v=new MyView();v、 BindingContext=新的MyViewModel()您在哪里将ViewModel的实例设置为页面的BindingContext?它在别处,是通常的代码
var v=new MyView();v、 BindingContext=新的MyViewModel()
protected override void OnAttachedTo(T bindable)
{
    base.OnAttachedTo(bindable);
    AssociatedObject = bindable;

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

    bindable.BindingContextChanged += OnBindingContextChanged;
}