使用绑定

时间:2016-06-23 17:13:43

标签: c# xaml xamarin xamarin.forms

目标:

我想在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>

ExtendableButtonList:

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);
    }

}

1 个答案:

答案 0 :(得分:1)

以下是他们(XF)的做法,

using System;
using System.Windows.Forms;

namespace TestApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            this.webBrowser1.Url = new Uri("http://google.com");
            this.webBrowser1.DocumentCompleted += browser_DocumentCompleted;
        }

        private void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            HtmlDocument doc = this.webBrowser1.Document;
            HtmlElement head = doc.GetElementsByTagName("head")[0];
            HtmlElement scriptEl = doc.CreateElement("script");

            string jsScript = @"function openApp (urlToOpen) {
                                    window.open(urlToOpen, '_blank');
                                    // do a couple other things
                                }";

            scriptEl.SetAttribute("text", jsScript);
            head.AppendChild(scriptEl);

            webBrowser1.Document.InvokeScript("openApp", new object[] { "http://google.com" });
        }
    }
}

从中复制 enter image description here

在viewmodel或其他任何内容中,创建一个ICommand属性并将其绑定到Command。