【问题标题】:Passing event to XAML object using binding使用绑定将事件传递给 XAML 对象
【发布时间】:2016-06-24 05:36:38
【问题描述】:

目标:

我想从 ExtendableButtonList(自定义视图)中的 ContentPage(管理 XAML 的类)运行一些方法。

问题:

ActionHandler - 表示要在 ExtendableButtonList 中调用的事件 - 始终等于 null。其他所有属性都以相同的方式配置,而不是这样。

甚至可以使用绑定来传递方法吗?

我确定我没有将空值传递给我的对象。

XAML:

<ListView x:Name="emView">
    <ListView.ItemTemplate>
      <DataTemplate>
        <ViewCell>
          <StackLayout Padding="20">
            <StackLayout BackgroundColor="Silver" Padding="20">
              [...]
              <customControls:ExtendableButtonList ActionHandler="{Binding ActionHandler}"></customControls:ExtendableButtonList>
            </StackLayout>
          </StackLayout>
        </ViewCell>
      </DataTemplate>
    </ListView.ItemTemplate>
  </ListView>

可扩展按钮列表:

public class ExtendableButtonList : StackLayout
{
    public static readonly BindableProperty ActionHandlerProperty = BindableProperty.Create(
      propertyName: "ActionHandler",
      returnType: typeof(OneOfButtonClickedHandler),
      declaringType: typeof(ExtendableButtonList),
      defaultValue: default(OneOfButtonClickedHandler));

    public OneOfButtonClickedHandler ActionHandler
    {
        get { return (OneOfButtonClickedHandler)GetValue(ActionHandlerProperty); }
        set { SetValue(ActionHandlerProperty, value); }
    }

    public delegate void OneOfButtonClickedHandler(int buttonId, int action);
    public event OneOfButtonClickedHandler OneOfButtonsClicked;

    public ExtendableButtonList()
    {
        [...]


        PropertyChanged += CheckIfPropertyLoaded;
    }

    void CheckIfPropertyLoaded(object sender, PropertyChangedEventArgs e)
    {
        //wait until property ActionHandler will be loaded
        if(e.PropertyName == "ActionHandler")
        {
            OneOfButtonsClicked += ActionHandler;
        }
    }

    [...]
    void CalculationsFinished(){
        [...]
        OneOfButtonsClicked(buttonId, action);
    }

}

【问题讨论】:

    标签: c# xaml xamarin xamarin.forms


    【解决方案1】:

    这是他们 (XF) 的做法,

    public static readonly BindableProperty CommandProperty = BindableProperty.Create("Command", typeof(ICommand), typeof(Button), null, propertyChanged: (bo, o, n) => ((Button)bo).OnCommandChanged());
    
    public static readonly BindableProperty CommandParameterProperty = BindableProperty.Create("CommandParameter", typeof(object), typeof(Button), null,
                propertyChanged: (bindable, oldvalue, newvalue) => ((Button)bindable).CommandCanExecuteChanged(bindable, EventArgs.Empty));
    
    public ICommand Command
            {
                get { return (ICommand)GetValue(CommandProperty); }
                set { SetValue(CommandProperty, value); }
            }
    
            public object CommandParameter
            {
                get { return GetValue(CommandParameterProperty); }
                set { SetValue(CommandParameterProperty, value); }
            }
    
    public event EventHandler Clicked;
    void IButtonController.SendClicked()
            {
                ICommand cmd = Command;
                if (cmd != null)
                    cmd.Execute(CommandParameter);
    
                EventHandler handler = Clicked;
                if (handler != null)
                    handler(this, EventArgs.Empty);
            }
    

    复制自 https://github.com/xamarin/Xamarin.Forms/blob/2d9288eee6e6f197364a64308183725e7bd561f9/Xamarin.Forms.Core/Button.cs

    在您的视图模型或其他内容中,创建一个 ICommand 属性并将其绑定到 Command。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-08
      • 2017-10-10
      • 2011-03-12
      • 1970-01-01
      • 2016-09-23
      • 1970-01-01
      相关资源
      最近更新 更多