I've custom renderer in Xamarin and I wonder how to dynamically update its value.
Here is my control in the main class:
public class MainControl : View
{
public double A
{
get;
set;
}
}
Here is my custom renderer, defined in Android:
[assembly: Xamarin.Forms.ExportRenderer(typeof(MainApplication.MainControl), typeof(MainApplication.Droid.CustomRenderer))]
namespace MainApplication.Droid
{
public class CustomRenderer : ViewRenderer<MainControl,
MainApplication.Droid.ControlAndroid>
{
private ControlAndroid control;
public CustomRenderer(Context context) : base(context)
{
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
}
protected override void OnElementChanged(ElementChangedEventArgs<MainControl> e)
{
base.OnElementChanged(e);
if (Control == null)
{
control = new ControlAndroid(Context);
SetNativeControl(control);
}
}
}
}
The method OnElementChanged does only update when creating the object. OnElementPropertyChanged are not trigged.
I expected that something should be trigged when changing the value of the property A from the main class.
I found the answer by my own. I figured out that I needed a bindable property (connected to my regular property "A") in order to get a call on OnElementPropertyChanged.
Related
I'm displaying an OAtuh2 HTML page in WebView that returns me, after clicking on a validation button that is on this page, a redirect URL that I would like to intercept to use the information from it.
in XML file
<ContentPage.Content>
<WebView x:Name="browser"></WebView>
</ContentPage.Content>
in CS file
browser.Source = "https://myUrl";
My low knowledge in Xamarin doesn't allow me to know how to do it
Thanks for your help
You can do as ToolmakerSteve mentioned using WebNavigatingEventArgs.
And for details you can implement this on each specific platform with custom renderer .
iOS
[assembly: ExportRenderer(typeof(WebView), typeof(MyRenderer))]
namespace FormsApp.iOS
{
class MyRenderer : WkWebViewRenderer
{
protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);
this.NavigationDelegate = new MyDelegate();
}
}
public class MyDelegate : WKNavigationDelegate
{
public override void DecidePolicy(WKWebView webView, WKNavigationAction navigationAction, Action<WKNavigationActionPolicy> decisionHandler)
{
if(navigationAction.NavigationType == WKNavigationType.Other)
{
if(navigationAction.Request.Url != null)
{
//do something
}
decisionHandler(WKNavigationActionPolicy.Cancel);
return;
}
decisionHandler(WKNavigationActionPolicy.Allow);
}
}
}
Android
[assembly: ExportRenderer(typeof(Xamarin.Forms.WebView), typeof(MyRenderer))]
namespace FormsApp.Droid
{
class MyRenderer : WebViewRenderer
{
public MyRenderer(Context context):base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.WebView> e)
{
base.OnElementChanged(e);
if (Control != null)
{
Control.SetWebViewClient(new MyClient());
}
}
}
public class MyClient : WebViewClient
{
public override bool ShouldOverrideUrlLoading(Android.Webkit.WebView view, IWebResourceRequest request)
{
//do something
return true;
}
}
}
Refer to
https://stackoverflow.com/a/45604360/8187800
https://stackoverflow.com/a/4066497/8187800
I'm trying to create an extended version of the standard XF Map object:
public class RRMap: Map
{
public void DoSomethingOnMap() {
/* ... */
}
}
I also created an Android renderer (iOS will come later):
[assembly: ExportRenderer(typeof(RRMap), typeof(RRMapRendererAndroid))]
namespace MyApp.Droid.Renderers
{
public class RRMapRendererAndroid : MapRenderer
{
public RRMapRendererAndroid(Context context) : base(context) { }
protected override void OnElementChanged(Xamarin.Forms.Platform.Android.ElementChangedEventArgs<Map> e)
{
base.OnElementChanged(e);
if (e.NewElement != null)
{
Control.GetMapAsync(this);
}
}
protected override MarkerOptions CreateMarker(Pin pin)
{
var marker = new MarkerOptions();
marker.SetPosition(new LatLng(pin.Position.Latitude, pin.Position.Longitude));
marker.SetTitle(pin.Label);
marker.SetSnippet(pin.Address);
marker.SetIcon(BitmapDescriptorFactory.DefaultMarker(210));
return marker;
}
}
}
Everything is working fine so far: the map is rendered and pins are created with a custom color.
Unfortunately, I'm stuck on the implementation of DoSomethingOnMap method: it should be a method in the shared code, but it should be implemented in different ways, depending on the platform.
In other circumstances, I would create an interface using DependencyService for implementation, but in this particular case I can't figure out how to proceed.
The first solution is you can use a messaging-center, this can communicate between shared project and iOS/Android project.
Publish a message in the doSomethingOnMap method and anywhere you subscribed to the message will be triggered.
The second is create an event in your shared project and subscribe to that event in the renderer, I wrote both two solutions below:
In your shared project:
public class CustomMap : Map
{
public List<CustomPin> CustomPins { get; set; }
public event EventHandler CallToNativeMethod;
public void doSomething()
{
if (CallToNativeMethod != null)
CallToNativeMethod(this, new EventArgs());
}
public void doSomething(CustomMap myMap) {
MessagingCenter.Send<CustomMap>(this, "Hi");
}
}
In the renderer:
protected override void OnElementChanged(ElementChangedEventArgs<View> e)
{
base.OnElementChanged(e);
if (e.OldElement != null)
{
}
if (e.NewElement != null)
{
MessagingCenter.Subscribe<CustomMap>(this, "Hi", (sender) =>
{
// Do something whenever the "Hi" message is received
Console.WriteLine("hi");
});
((CustomMap)e.NewElement).CallToNativeMethod += (sender, arg) =>
{
Console.WriteLine("native method");
};
}
}
At anywhere you want to call this method:
private void Button_Clicked(object sender, System.EventArgs e)
{
customMap.doSomething();
customMap.doSomething(customMap);
}
I have a Xamarin page in which I had to use Android native page renderer in order to support platform specific API.
BasePage.xaml passes control to MyPage.xaml with Navigation.PushAsync()
XAML page : MyPage.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"
x:Class="Views.MyPage" Title="My Page">
<ContentPage.Content>
</ContentPage.Content>
</ContentPage>
Android Custom page renderer for the above is something like below.
[assembly: ExportRenderer(typeof(MyPage), typeof(MyPageRenderer))]
namespace MyApp.Droid.Renderers
{
public class MyPageRenderer : PageRenderer
{
private Context _localContext;
private global::Android.Views.View view;
private Activity activity;
public event EventHandler ItemAdded;
public MyPageRenderer(Context context) : base(context)
{
_localContext = context;
}
protected override void OnElementChanged(ElementChangedEventArgs<Page> e)
{
base.OnElementChanged(e);
if (e.OldElement != null || Element == null)
{
return;
}
try
{
SetupUserInterface();
SetupEventHandlers();
AddView(view);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(#"ERROR: ", ex.Message);
}
}
private void SetupUserInterface()
{
activity = this.Context as Activity;
view = activity.LayoutInflater.Inflate(Resource.Layout.axml_layout, this, false);
}
protected override void OnLayout(bool changed, int l, int t, int r, int b)
{
base.OnLayout(changed, l, t, r, b);
var msw = MeasureSpec.MakeMeasureSpec(r - l, MeasureSpecMode.Exactly);
var msh = MeasureSpec.MakeMeasureSpec(b - t, MeasureSpecMode.Exactly);
view.Measure(msw, msh);
view.Layout(0, 0, r - l, b - t);
}
private void SetupEventHandlers()
{
//blah blah
}
private void ButtonTapped(object sender, EventArgs e)
{
//do something
//Here Navigate back to page which triggered this with outcome parameter or some event
ItemAdded(this, EventArgs.Empty);
}
}
}
My intention is send back control to MyPage.xaml.cs or BasePage.xaml.cs from MyPageRenderer with outcome of ButtonTapped.I am using event ItemAdded and handle it in code behind of that page. I can not access ItemAdded event which is in android specific renderer only from shared project.
I have to update ViewModel of BasePage so that I update the content of the items there when MyPage has been popped after adding new item by back button.
Problem:
I can access MyPage and BasePage but can not access renderer method and variables from Shared project because Android project depends on shared not vice versa.
I have to do something like below which is working for non-native render page
BasePage:
var myPage = new MyPage();
myPage.ItemAdded += OnItemAdded;
await Navigation.PushAsync(myPage);
MyPage:
public event EventHandler ItemAdded;
.
.
void SomeMethod(){
ItemAdded(this, EventArgs.Empty);
}
Question: How do we pass control from NativeRenderer back to Xamarin Forms shared code?
I know we can pass control to MainActivity class but I want to pass control to BasePage.xaml.cs which I did not get from documentation. If anyone has worked on PageRenderer please suggest.
in "MyPage" Class
public class MyPage : ContentPage
{
public void RaiseSomeButtonClicked() => OnSomeButtonClickeded();
private void OnSomeButtonClicked()
{
//by using aggregators you can publish any event and subscribe it in you BasePage.xaml.cs
((App)App.Current).Container.Resolve<IEventAggregator>()
.GetEvent<SomeButtonClickedEvent>().Publish(new SomeButtonClickedEvent());
}
}
in "MyPageRenderer" Class :
public class MyPageRenderer : PageRenderer
{
MyPage myPage;
//...
protected override void OnElementChanged(ElementChangedEventArgs<Page> e)
{
myPage = (MyPage)e.NewElement;
//...
}
private void ButtonTapped(object sender, EventArgs e)
{
//do something
myPage.RaiseSomeButtonClicked();
}
}
in "BasePage.xaml.cs", subscribe this event.
public partial class BasePage : ContentPage
{
private readonly SubscriptionToken _SomeButtonClickedEventSubscription;
public BasePage()
{
InitializeComponent();
_SomeButtonClickedEventSubscription = eventAggregator.Value.GetEvent<SomeButtonClickedEvent>().SubscribeAsync(async e =>
{
//anything you want to do when button clicked!
}, threadOption: ThreadOption.UIThread, keepSubscriberReferenceAlive: true);
}
}
You should define Your event class in this way:
public class SomeButtonClickedEvent : PubSubEvent<SomeButtonClickedEvent>
{
//you can define parameters here, if the event needs to pass a parameter.
}
With reference to zohre moradi's answer I could achieve this as below.
This does not use IEventAggregator -Subscribe/Publish of events methods. If event is only required at one page IEventAggregator can be avoided.
MyPage.xaml.cs
public event EventHandler ItemAdded;
public void RaiseItemAdded()
{
ItemAdded(this, EventArgs.Empty);
}
//have to close return call back after item addition in MyPage
public async void CallPopAsync()
{
await Navigation.PopAsync();
}
MyPageRenderer.cs
MyPage mypage;
protected override void OnElementChanged(ElementChangedEventArgs<Page> e)
{
base.OnElementChanged(e);
mypage= (MyPage)e.NewElement;
if (e.OldElement != null || Element == null)
{
return;
}
try
{
SetupUserInterface();
SetupEventHandlers();
AddView(view);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(#"ERROR: ", ex.Message);
}
}
private void ButtonTapped(object sender, EventArgs e)
{
//do something
myPage.RaiseItemAdded();
//notify
Toast.MakeText(this.Context, "Item created", ToastLength.Long).Show();
myPage.CallPopAsync();
}
And in BasePage.xaml.cs
//in some method
var myPage = new MyPage();
myPage.ItemAdded += OnItemAdded;
await Navigation.PushAsync(myPage);
private void OnItemAdded(object sender, EventArgs e)
{
//call method to update binding object of viewmodel
}
I'm porting to Xamarin.IOS a swift library that makes some material design animation with UIButton.
The swift counterpart is a subclass of UIButton and overrides layoutSublayersOfLayer.
Swift:
public override func layoutSublayersOfLayer(layer: CALayer) {
super.layoutSublayersOfLayer(layer)
if self.layer == layer {
layoutShape()
layoutVisualLayer()
}
}
On Xamarin side I noted that this method is called through CALayerDelegate, which is associated with CALayer through de property Delegate.
I tried to subclass CALayerDelegate and replace de Delegate property, but when I did that the button didn't rendered correctly and stopped respond to events.
Is there a way to override layoutSublayersOfLayer on Xamarin.Ios ? Is there another method that I can override to prepare stuff before a particular layer is drawn ?
public class MaterialButtonLayerDelegate : CALayerDelegate
{
private readonly MaterialButton _button;
public MaterialButtonLayerDelegate(MaterialButton button):base()
{
_button = button;
}
public override void LayoutSublayersOfLayer(CALayer layer)
{
if (_button.Layer == layer)
{
_button.LayoutShape();
_button.LayoutVisualLayer();
}
}
}
[Register("MaterialButton")]
public class MaterialButton : UIButton
{
public CAShapeLayer VisualLayer { get; private set; } = new CAShapeLayer();
public MaterialButton(CGRect frame):base(frame)
{
PrepareView();
}
protected void PrepareView()
{
Layer.Delegate = new MaterialButtonLayerDelegate(this);
ContentScaleFactor = MaterialDevice.Scale();
PrepareVisualLayer();
}
protected virtual void PrepareVisualLayer()
{
VisualLayer.ZPosition = 0;
VisualLayer.MasksToBounds = true;
Layer.AddSublayer(VisualLayer);
}
protected virtual void LayoutShape()
{
//...
}
protected virtual void LayoutVisualLayer()
{
//...
}
}
Thanks!
The only possible way to do it is extending CALayerDelegate class and setting it as your CAShapeLayer delegate. Be sure to call delegate's base members wherever possible as it performs required bindings behind the scenes.
public override void LayoutSublayersOfLayer(CALayer layer)
{
base.LayoutSublayersOfLayer(layer);
if (_button.Layer == layer)
{
_button.LayoutShape();
_button.LayoutVisualLayer();
}
}
If I pass the derived class testA a PlaceHolder that contains a Hyperlink, with a url that starts with
a tilde, it resolves it correctly.
However, when I pass testB
(identical apart from it inherits from
System.Web.UI.UserControl) the same PlaceHolder It
renders it literally (doesn't
transform / resolve the '~')
Any ideas?
public class testA : System.Web.UI.Control
{
public System.Web.UI.WebControls.PlaceHolder plc { get; set; }
protected override void OnLoad(EventArgs e)
{
if (plc != null)
this.Controls.Add(plc);
base.OnLoad(e);
}
}
public class testB : System.Web.UI.UserControl
{
public System.Web.UI.WebControls.PlaceHolder plc { get; set; }
protected override void OnLoad(EventArgs e)
{
if (plc != null)
this.Controls.Add(plc);
base.OnLoad(e);
}
}
This is ASP.NET
When you inherit from System.Web.UI.UserControl and do not associate your control with an ascx file then your control TemplateSourceVirtualDirectory will not be set, this is required by the ResolveClientUrl method - if its null or empty the url will be returned AS IS.
To solve your problem, just set AppRelativeTemplateSourceDirectory :
public class testB : System.Web.UI.UserControl
{
public System.Web.UI.WebControls.PlaceHolder plc { get; set; }
protected override void OnLoad(EventArgs e)
{
if (plc != null)
{
this.AppRelativeTemplateSourceDirectory =
plc.AppRelativeTemplateSourceDirectory;
this.Controls.Add(plc);
}
base.OnLoad(e);
}
}
A UserControl is normally associated with an ascx file that defines its markup. Such controls should be instantiated using TemplateControl.LoadControl() before they're added to the page, in order to perform event catch-up.
I suspect that event catch-up does not take place since you don't call LoadControl(), so the Hyperlink's NavigateUrl never gets a chance to be properly resolved.