如何从Xamarin表单的自定义渲染器获取回调

时间:2019-04-24 10:46:18

标签: xamarin xamarin.forms xamarin.android

我有一个用于Android和ios的自定义hybridWebView来加载URL。我需要的是在URL完成加载后将回调传递给内容页面。下面的代码,将不胜感激。

内容页面


    public partial class ConveyancingLeadPage : ContentPage
    {
        DashboardViewModel viewmodel;
        StorageService storage = new StorageService();

        public ConveyancingLeadPage()
        {
            InitializeComponent();
            GetUserAvatar();
        }

        protected async override void OnAppearing()
        {  
            // I need the callback to be execute here 

            customView.weblink = viewmodel.BrokerData.config.conveyancing.listing_webview;
        }


    }

Android HybridView

         [assembly: ExportRenderer(typeof(HCHybridWebview), typeof(HCHybridWebviewRendererAndroid))]
     namespace HashChing.Droid.CustomRenderers
       {
        public class HCHybridWebviewRendererAndroid : ViewRenderer<HCHybridWebview, Android.Webkit.WebView>
        {

        Context _context;
        public HCHybridWebviewRendererAndroid(Context context) : base(context)
        {
            _context = context;
        }

        protected override void OnElementChanged(ElementChangedEventArgs<HCHybridWebview> e)
        {
            base.OnElementChanged(e);

            const string JavascriptFunction = "function invokeCSharpAction(data){jsBridge.invokeAction(data);}";


            if (Control == null)
            {
                //Do something

            if (e.NewElement != null)
            {
               //Load URL
                Control.AddJavascriptInterface(new JSBridge(this), "jsBridge");
                var hybridWebView = e.NewElement as HCHybridWebview;
                if (hybridWebView != null)
                {
                    hybridWebView.RefreshView += LoadUrl;
                }

            }

加载网址

public void LoadUrl(object sender, EventArgs e)
{
    Control.LoadUrl(webView.weblink, headers);
}

一旦加载了URL,它将在同一类中导航至该方法,并且一旦在“ OnPageFinished”方法内完成加载,我就在这里向我的内容页面传递回调。帮助将不胜感激。

     public class JavascriptWebViewClient : WebViewClient
    {
      string _javascript;

    public JavascriptWebViewClient(string javascript)
    {
        _javascript = javascript;
    }

    public override void OnPageFinished(Android.Webkit.WebView view, string url)
    {
        base.OnPageFinished(view, url);
        view.EvaluateJavascript(_javascript, null);
    }
}

2 个答案:

答案 0 :(得分:1)

您可以使用 MessagingCenter 发送并获取回叫:

在您的ContentPage中,例如Page1.xaml.cs

public Page1 ()
    {
        InitializeComponent ();
        //here you could get the callback,and arg = "this is call back"
        MessagingCenter.Subscribe<Page1,string>(this,"callback", (send, arg) =>
        {
            Console.WriteLine(arg);
        });
    }

然后使用您的OnPageFinished方法:

public override void OnPageFinished(Android.Webkit.WebView view, string url)
{
    base.OnPageFinished(view, url);
    view.EvaluateJavascript(_javascript, null);
    //send the callback content,Parameters can be defined by yourself
    MessagingCenter.Send<Page1,string>(new Page1(), "callback","this is call back");     
}

更多信息:MessagingCenter

答案 1 :(得分:0)

在customrender中创建您自己的事件LoadCompleted,并在加载完成后从您的自定义类调用它。

在JavascriptWebViewClient类中,您可以订阅该事件,并在那时做您想做的事。

以防您需要:Events in c#

public partial class ConveyancingLeadPage : ContentPage
    {
         protected async override void OnAppearing()
        {  
            customView.weblink = viewmodel.BrokerData.config.conveyancing.listing_webview;

            // I need the callback to be execute here 

            customView.LoadCompleted+=LoadCompleted;

        }
    }