将方法作为参数传递给XAML对象

本文关键字:XAML 对象 参数传递 方法 | 更新日期: 2023-09-27 18:13:12

我目前正在使用CustomObject,需要每个平台的CustomObjectRenderer

我想传递一个方法作为参数到这个对象,从XAML方面,所以我将能够使用这个回调,从我的渲染器。

<control:CustomObject Callback="CallbackFunction"/>

然后在PCL部分的 mainpage . example .cs中声明CallbackFunction(object param)

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        base.BindingContext = this;
    }
    public void CallbackFunction(object param)
    {
        Debug.WriteLine((object as Element).Name);
    }
}

所以,如果我理解得好,我的CustomObject必须是这样的:

public CustomObject : Object
{
    public Action<object> Callback { get; set; }
}

但是我有一个关于XAML解析的错误…我不明白为什么会抛出这个错误。

最后,我想做的是,从渲染器中调用这个方法,然后处理事情,从mainpage . example .cs,从PCL部分执行操作。

public class CustomObjectRenderer : ObjectRenderer
{
    NativeObject nativeObject;
    CustomObject customObject;
    protected override void OnElementChanged(ElementChangedEventArgs<CustomObject> e)
    {
        base.OnElementChanged(e);
        if (e.NewElement != null)
        {
            customObject = e.NewElement as CustomObject;
            nativeObject = Control as NativeObject;
        }
    }
    protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        base.OnElementPropertyChanged(sender, e);
    }
    // Etc etc .... 
    private void METHOD_CALLED_BY_EVENT(object o)
    {
        // This method get call by the renderer event and then, I want to call 
        // the method CallbackFunction(object); and do actions.
        customObject.Callback(o as OBJECT_PARAM);
    }
}

好吧,向你解释我的问题对我来说有点困难,所以如果你有什么不明白的,请告诉我。

将方法作为参数传递给XAML对象

您可以通过使用事件来实现这一点。

MyView

public class MyView : View
{
    public event EventHandler<string> MyEvent;
    public void RaiseEvent(string parameter)
    {
        MyEvent?.Invoke(this, parameter);
    }
}

Page.xaml

<local:MyView MyEvent="MyView_OnMyEvent"></local:MyView>

Page.xaml.cs

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
    }
    private void MyView_OnMyEvent(object sender, string e)
    {
        Debug.WriteLine(e);
    }
}

渲染器

public class MyViewRenderer : ViewRenderer<MyView, SomeNativeView>
{
    private void METHOD_CALLED_BY_EVENT(string param)
    {
        Element.RaiseEvent(param);
    }
}

经过大量的尝试,没有工作,我有一个想法,我尝试了,它的工作,因为我想问我的问题。


首先,创建自定义对象!

CustomView

public class CustomView : View
{
    public static readonly BindableProperty MainPageCallbackProperty =
        BindableProperty.Create(nameof(MainPageCallback), typeof(Action<object>), typeof(CustomMap), null);
    public Action<object> MainPageCallback
    {
        get { return (Action<object>)GetValue(MainPageCallbackProperty); }
        set { SetValue(MainPageCallbackProperty, value); }
    }
}

我们因此使用Action,它是方法/回调的容器。但在我的例子中,我们将使用Action<object>。为什么?因为它将允许我们有一个对象的参数回调,所以我们将能够从渲染器带回数据。

然后,创建一个名为MainPage的页面。xaml的示例。在新页面的XAML部分,添加以下代码:
<?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:control="clr-namespace:Project.CustomControl;assembly=Project"
             x:Class="Project.Page.MainPage">
  <ContentPage.Content>
    <control:CustomView MainPageCallback="{Binding MainPageCallbackAction}"
                        VerticalOptions="Fill" HorizontalOptions="Fill"/>
  </ContentPage.Content>
</ContentPage>

关于这个XAML,有两个部分让我们感兴趣。

  • XAML"引用"

    xmlns:control="clr-namespace:Project.CustomControl;assembly=Project"

通过这些xmlns,您可以访问您的自定义控件。

  • 页面内容

    <ContentPage.Content> <control:CustomView MainPageCallback="{Binding MainPageCallbackAction}" VerticalOptions="Fill" HorizontalOptions="Fill"/> </ContentPage.Content>

现在,将对象的MainPageCallback绑定到在 c# 端声明的MainPageCallbackAction

之后,我们的 mainpage . example .cs看起来像:

public partial class MainPage : ContentPage
{
    public Action<object> MainPageCallbackAction { get; set; }
    public MainPage()
    {
        base.BindingContext = this;
        MainPageCallbackAction = MainPageCallbackMethod;
        InitializeComponent();
    }
    private void MainPageCallbackMethod(object param)
    {
        Device.BeginInvokeOnMainThread(() =>
        {
            Debug.WriteLine("Welcome to the Callback :)");
            Debug.WriteLine("Emixam23 - Example");
        });
    }
}

现在,最后要看的是CustomViewRenderer !

public class CustomViewRenderer : ViewRenderer<CustomView, NativeView>
{
    CustomView customView;
    NativeView nativeView;
    protected override void OnElementChanged(ElementChangedEventArgs<CustomView> e)
    {
        base.OnElementChanged(e);
        if (e.NewElement != null)
        {
            customView = e.NewElement as CustomView;
            nativeView = Control as NativeView;
            NativeView.CLicked += METHOD_CALLED_BY_EVENT;
        }
    }
    protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        base.OnElementPropertyChanged(sender, e);
    }
    private void METHOD_CALLED_BY_EVENT(object sender, EventArgs ea)
    {
        customView.MainPageCallback(ea.something.information);
    }
}

然后,看看输出,您将能够看到以下内容:

  • 欢迎回调:)
  • Emixam23 -示例