Xamarin avplayercontroller custom controls - c#

I am trying to add some controls to a sample from https://github.com/pro777s/Xam.Forms.VideoPlayer
Modifying these codes from here. I have a created a custom controller.
Not sure how I am suppose to do it. Help will be appreciated. I need to add two buttons, Previous and Next to play and next and previous track.
public class CustomButton : UIButton
{
public CustomButton() : base(UIButtonType.System)
{ }
public int someValue;
protected override void Dispose(bool disposing)
{
Console.WriteLine("Disposed button");
base.Dispose(disposing);
}
~CustomButton()
{
Console.WriteLine("Finalized button");
}
}
public class CustomPlayerController : AVPlayerViewController
{
private AVPlayer _Player;
private AVAudioUnitEQ aVAudioUnitEQ;
// IMusicManager musicManager;
static CustomPlayerController playerController;
public static CustomPlayerController GetInstance(AVPlayer player)
{
if (playerController == null)
{
playerController = new CustomPlayerController(player);
}
//DependencyService.RegisterSingleton<IBackGroundPlayer>(playerController);
return playerController;
}
private CustomPlayerController(AVPlayer player)
{
_Player = player;
}
private void buttonTapped(object sender, EventArgs e)
{
//print("button was tapped")
// replay/comment logic here
}
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
var customView = this.View.Subviews[0] as CustomButton;
Console.WriteLine("Magic number: " + customView.someValue);
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
var btn = new CustomButton()
{
Frame = new CoreGraphics.CGRect(0, 20, 320, 40),
someValue = 4711
};
btn.SetTitle("Click me", UIControlState.Normal);
btn.TouchUpInside += (sender, e) => {
//btn.RemoveFromSuperview();
};
this.Add(btn);
}
}
This is the renderer, i have changed this little bit.
AVPlayer player;
AVPlayerItem playerItem;
CustomPlayerController _playerViewController; // solely for ViewController property
NSObject playCompleteNotification, playerItemFailedToPlayToEndTimeNotification;
NSObject playbackBufferEmptyObserver, playbackLikelyToKeepUpObserver, playbackBufferFullObserver;
public override UIViewController ViewController => _playerViewController;
protected override void OnElementChanged(ElementChangedEventArgs<VideoPlayer> args)
{
base.OnElementChanged(args);
if (args.NewElement != null)
{
if (Control == null)
{
// Create AVPlayerViewController
// Set Player property to AVPlayer
player = new AVPlayer();
_playerViewController = CustomPlayerController.GetInstance (player);
_playerViewController.Player = player;
// End of play notification
playCompleteNotification = NSNotificationCenter.DefaultCenter.AddObserver(
AVPlayerItem.DidPlayToEndTimeNotification,
OnAVPlayerItemDidPlayToEndTime, player.CurrentItem);
// Play error occured notification
playerItemFailedToPlayToEndTimeNotification =
AVPlayerItem.Notifications.ObserveItemFailedToPlayToEndTime(OnAVPlayerItemFailedToPlayToEndTime);
// Use the View from the controller as the native control
SetNativeControl(_playerViewController.View);
}
SetAreTransportControlsEnabled();
SetSource();
args.NewElement.UpdateStatus += OnUpdateStatus;
args.NewElement.PlayRequested += OnPlayRequested;
args.NewElement.PauseRequested += OnPauseRequested;
args.NewElement.StopRequested += OnStopRequested;
args.NewElement.ShowTransportControlsRequested += OnShowTransportControls;
args.NewElement.HideTransportControlsRequested += OnHideTransportControls;
}
if (args.OldElement != null)
{
args.OldElement.UpdateStatus -= OnUpdateStatus;
args.OldElement.PlayRequested -= OnPlayRequested;
args.OldElement.PauseRequested -= OnPauseRequested;
args.OldElement.StopRequested -= OnStopRequested;
args.OldElement.ShowTransportControlsRequested -= OnShowTransportControls;
args.OldElement.HideTransportControlsRequested -= OnHideTransportControls;
}
}

If you put the button in the View, it will be overlapped. So one workaround I thought is add the button to the window and remove it when the controller dismiss:
UIButton btn { get; set; }
public override void ViewWillDisappear(bool animated)
{
base.ViewWillDisappear(animated);
btn.RemoveFromSuperview();
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
btn.RemoveFromSuperview();
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
btn = new CustomButton()
{
Frame = new CoreGraphics.CGRect(40, 20, 320, 40),
someValue = 4711
};
btn.BackgroundColor = UIColor.Red;
btn.SetTitleColor(UIColor.Blue,UIControlState.Normal);
btn.SetTitle("Click me", UIControlState.Normal);
btn.TouchUpInside += (sender, e) => {
//btn.RemoveFromSuperview();
};
UIApplication.SharedApplication.KeyWindow.Add(btn);
}
I did not test the remove part as I can't play a video on my side.

Related

Navigation with custom renderer of Exoplayer in xamarin

I have a listview in mainpage,when i click listview it goes to 2nd page where it has Exoplayer integration with a custom renderer,so the problem is how to release the player properly,as currently _player is equals to null always ,multiple audios are played,If navigate from first page to 2nd page multiple times.
Here is the code of custom renderer-
public class VideoPlayerRenderer : ViewRenderer<VideoPlayer, SimpleExoPlayerView>, IExoPlayerEventListener
{
private SimpleExoPlayerView _playerView;
private SimpleExoPlayer _player;
private DefaultTrackSelector trackSelector;
DefaultHttpDataSourceFactory defaultHttpDataSourceFactory;
DefaultDataSourceFactory defaultDataSourceFactory ;
ExtractorMediaSource extractorMediaSource ;
public VideoPlayerRenderer(Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<VideoPlayer> e)
{
base.OnElementChanged(e);
if (_player == null)
{
InitializePlayer();
}
Play();
}
private void InitializePlayer()
{
trackSelector = new DefaultTrackSelector();
_player = ExoPlayerFactory.NewSimpleInstance(Context,trackSelector);
_player.PlayWhenReady = true;
_playerView = new SimpleExoPlayerView(Context) { Player = _player };
SetNativeControl(_playerView);
SetBackgroundColor(Android.Graphics.Color.Green);
}
private void Play()
{
Uri sourceUri = Uri.Parse(Element.SourceUrl);
var mediaUri = Android.Net.Uri.Parse(Element.SourceUrl);
var userAgent = Util.GetUserAgent(Context, "App1");
defaultHttpDataSourceFactory = new DefaultHttpDataSourceFactory(userAgent);
defaultDataSourceFactory = new DefaultDataSourceFactory(Context, null, defaultHttpDataSourceFactory);
extractorMediaSource = new ExtractorMediaSource(mediaUri, defaultDataSourceFactory, new DefaultExtractorsFactory(), null, null);
_player.Prepare(extractorMediaSource);
_player.AddListener(this);
}
In your custom renderer, override OnDetachedFromWindow to stop the player:
protected override void OnDetachedFromWindow()
{
base.OnDetachedFromWindow();
_player.Stop();
_player.Release();
}
Refer: onDetachedFromWindow
Update:
To control the player, you should create a singleton of SimpleExoPlayer , make sure there is only one _player in your entire project:
class ExoPlayerInstance
{
private static SimpleExoPlayer _player;
public static SimpleExoPlayer player
{
get
{
if (_player == null)
{
_player = ExoPlayerFactory.NewSimpleInstance(Application.Context, new DefaultTrackSelector());
_player.PlayWhenReady = true;
}
return _player;
}
}
}
And then in your custom render, remove the InitializePlayer part and use the ExoPlayerInstance:
protected override void OnElementChanged(ElementChangedEventArgs<VideoPlayer> e)
{
base.OnElementChanged(e);
//remove the InitializePlayer part
//if (_player == null)
// InitializePlayer();
_player = ExoPlayerInstance.player;
_playerView = new SimpleExoPlayerView(Context) { Player = _player };
SetNativeControl(_playerView);
if (_player.IsPlayingAd || _player.IsLoading)
{
//maybe something more need to do , stop() here is an example
_player.Stop();
}
Play();
}

Event Handler returning null c#?

I am implementing a swipe cards view in Xamarin.Android. I am firing an event from RatingCardAdapter to reset the swipe cards view after all the cards have been swiped. For the first time, the event is not null and the swipe cards get reset but on the second try, the event handler returns null. As a result, I cannot set the value of shouldResetSwipe. How can i solve this problem?
Adapter
public class RatingCardAdapter : BaseCardAdapter
{
private Context context;
public event EventHandler OnLastCardSwiped;
public RatingCardAdapter(Context context, SwipeCardsView SwipeView)
{
this.context = context;
this.SwipeView = SwipeView;
SwipeView.SetCardsSlideListener(this);
}
public void OnCardVanish(int p0, SwipeCardsView.SlideType p1)
{
if (p0 == (Count - 1)) // p0 becomes 4 when last card is swiped
{
if (OnLastCardSwiped != null) //becomes null when rating adapter called 2nd time
OnLastCardSwiped(this, new OnLastCardSwipeArgs
{
shouldResetSwipe = true });
}
}
public class OnLastCardSwipeArgs : EventArgs
{
public bool shouldResetSwipe { get; set; }
}
Activity
private SwipeCardsView swipeCardsView;
RatingCardAdapter ratingCardAdapter;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.activity_rating_session);
swipeCardsView = FindViewById<SwipeCardsView>
(Resource.Id.swipeCardsRating);
swipeCardsView.RetainLastCard(false);
swipeCardsView.EnableSwipe(true);
setSwipeData();
}
void setSwipeData() {
ratingCardAdapter = new RatingCardAdapter(this, swipeCardsView);
swipeCardsView.SetAdapter(ratingCardAdapter);
ratingCardAdapter.OnLastCardSwiped += (sender, e) =>
{
if (e.shouldResetSwipe)
{
Console.WriteLine("restart set " + e.shouldResetSwipe);
restartSwipeCard();
}};
}
void restartSwipeCard()
{
Console.WriteLine("restartswipe");
ratingCardAdapter = new RatingCardAdapter(this,swipeCardsView);
swipeCardsView.SetAdapter(ratingCardAdapter);
}
Since you are creating a new instance of RatingCardAdapter in restartSwipeCard method, you need to subscribe too it's event also, since its tied to RatingCardAdapter intance. So move your lambda method to intance method to prevent code duplication and do same subscription inside restartSwipeCard method:
void restartSwipeCard()
{
Console.WriteLine("restartswipe");
ratingCardAdapter = new RatingCardAdapter(this,swipeCardsView);
swipeCardsView.SetAdapter(ratingCardAdapter);
// need this subscription here too
ratingCardAdapter.OnLastCardSwiped += (sender, e) =>
{
if (e.shouldResetSwipe)
{
Console.WriteLine("restart set " + e.shouldResetSwipe);
restartSwipeCard();
}};
}
Or even better rename setSwipeData to initSwipeData and update code like this:
private SwipeCardsView swipeCardsView;
RatingCardAdapter ratingCardAdapter;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.activity_rating_session);
swipeCardsView = FindViewById<SwipeCardsView>
(Resource.Id.swipeCardsRating);
swipeCardsView.RetainLastCard(false);
swipeCardsView.EnableSwipe(true);
initSwipeData();
}
private void initSwipeData()
{
ratingCardAdapter = new RatingCardAdapter(this, swipeCardsView);
swipeCardsView.SetAdapter(ratingCardAdapter);
ratingCardAdapter.OnLastCardSwiped += (sender, e) =>
{
if (e.shouldResetSwipe)
{
Console.WriteLine("restart set " + e.shouldResetSwipe);
Console.WriteLine("restartswipe");
initSwipeData();
}};
}

Custom image renderer on iOS to get x,y coordinates of the tapped location

I have a xamarin forms image, and when the user taps on the image I want to get the x,y coordinates of where the user tapped. Not the x,y coordinates of the view per se, but the coordinates within the image. I have this working on Android below. What would the iOS custom renderer be like?
Create a interface for touch event:
public interface IFloorplanImageController
{
void SendTouched();
}
Create a custom control for image:
public class FloorplanImage : Image, IFloorplanImageController
{
public event EventHandler Touched;
public void SendTouched()
{
Touched?.Invoke(this, EventArgs.Empty);
}
public Tuple<float, float> TouchedCoordinate
{
get { return (Tuple<float, float>)GetValue(TouchedCoordinateProperty); }
set { SetValue(TouchedCoordinateProperty, value); }
}
public static readonly BindableProperty TouchedCoordinateProperty =
BindableProperty.Create(
propertyName: "TouchedCoordinate",
returnType: typeof(Tuple<float, float>),
declaringType: typeof(FloorplanImage),
defaultValue: new Tuple<float, float>(0, 0),
propertyChanged: OnPropertyChanged);
public static void OnPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
}
}
Implement the custom renderer:
[assembly: ExportRenderer(typeof(FloorplanImage), typeof(FloorplanImageRenderer))]
namespace EmployeeApp.Droid.Platform
{
public class FloorplanImageRenderer : ImageRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Image> e)
{
base.OnElementChanged(e);
if (e.NewElement != null)
{
if (Control != null)
{
Control.Clickable = true;
Control.SetOnTouchListener(ImageTouchListener.Instance.Value);
Control.SetTag(Control.Id, new JavaObjectWrapper<FloorplanImage> { Obj = Element as FloorplanImage });
}
}
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (Control != null)
{
Control.SetOnTouchListener(null);
}
}
base.Dispose(disposing);
}
private class ImageTouchListener : Java.Lang.Object, Android.Views.View.IOnTouchListener
{
public static readonly Lazy<ImageTouchListener> Instance = new Lazy<ImageTouchListener>(
() => new ImageTouchListener());
public bool OnTouch(Android.Views.View v, MotionEvent e)
{
var obj = v.GetTag(v.Id) as JavaObjectWrapper<FloorplanImage>;
var element = obj.Obj;
var controller = element as IFloorplanImageController;
if (e.Action == Android.Views.MotionEventActions.Down)
{
var x = e.GetX();
var y = e.GetY();
element.TouchedCoordinate = new Tuple<float, float>(x, y);
controller?.SendTouched();
}
else if (e.Action == Android.Views.MotionEventActions.Up)
{
}
return false;
}
}
}
public class JavaObjectWrapper<T> : Java.Lang.Object
{
public T Obj { get; set; }
}
}
Use this control like this:
<local:FloorplanImage HeightRequest="300" x:Name="image" WidthRequest="300"
Aspect="AspectFit" Touched="image_Touched" />
code behind:
private void image_Touched(object sender, EventArgs e)
{
var cor = image.TouchedCoordinate;
}
I found this Customer success example on github that does exactly this.
[assembly: ExportRenderer(typeof(CustomImage), typeof(CustomImageRenderer))]
namespace FormsImageTapGesture.iOS
{
public class CustomImageRenderer : ImageRenderer
{
#region properties & fields
// ---------------------------------------------------------------------------
//
// PROPERTIES & FIELDS
//
// ---------------------------------------------------------------------------
private UIImageView nativeElement;
private CustomImage formsElement;
#endregion
#region methods
// ---------------------------------------------------------------------------
//
// METHODS
//
// ---------------------------------------------------------------------------
//
// Set up the custom renderer. In this case, that means set up the gesture
// recognizer.
//
protected override void OnElementChanged(ElementChangedEventArgs<Image> e) {
base.OnElementChanged (e);
if (e.NewElement != null) {
// Grab the Xamarin.Forms control (not native)
formsElement = e.NewElement as CustomImage;
// Grab the native representation of the Xamarin.Forms control
nativeElement = Control as UIImageView;
// Set up a tap gesture recognizer on the native control
nativeElement.UserInteractionEnabled = true;
UITapGestureRecognizer tgr = new UITapGestureRecognizer (TapHandler);
nativeElement.AddGestureRecognizer (tgr);
}
}
//
// Respond to taps.
//
public void TapHandler(UITapGestureRecognizer tgr) {
CGPoint touchPoint = tgr.LocationInView (nativeElement);
formsElement.OnTapEvent ((int)touchPoint.X, (int)touchPoint.Y);
}
#endregion
}
}

Swipe Refresh not working in xamarin.android webview

I am trying to implement SwipeRefreshLayout in a webview in Xamarin.android but the layout keeps loading infinitely only works well as a progress bar but when swiping to refresh it doesn't work. Please help me fix it.
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.gameofthrones);
myWebClient = new WebClient();
myWebView = FindViewById<WebView>(Resource.Id.webView);
myProgressBar = FindViewById<ProgressBar>(Resource.Id.progressBar);
myswipeRefreshLayout = FindViewById<SwipeRefreshLayout>(Resource.Id.refresher);
myswipeRefreshLayout.SetColorScheme(Resource.Color.Red, Resource.Color.Orange,
Resource.Color.Yellow, Resource.Color.Green,
Resource.Color.Blue, Resource.Color.Indigo,
Resource.Color.Violet);
var toolbar = FindViewById<Toolbar>(Resource.Id.toolbar);
myWebView.Settings.JavaScriptEnabled = true;
myWebView.LoadUrl("http://www.google.com");
myWebView.SetWebViewClient(myWebClient);
//Toolbar will now take on default actionbar characteristics
SetSupportActionBar(toolbar);
SupportActionBar.Title = "GAME OF THRONES";
SupportActionBar.SetDisplayHomeAsUpEnabled(true);
SupportActionBar.SetHomeButtonEnabled(true);
myswipeRefreshLayout.Refresh += MyswipeRefreshLayout_Refresh;
}
private void MyswipeRefreshLayout_Refresh(object sender, EventArgs e)
{
myWebClient.myOnProgressChanged += (int state) =>
{
if (state == 0)
{
(sender as SwipeRefreshLayout).Refreshing = false;
myswipeRefreshLayout.Refreshing = false;
//page loaded no progress bar visible
//myProgressBar.Visibility = ViewStates.Invisible;
}
else
{
(sender as SwipeRefreshLayout).Refreshing = true;
myswipeRefreshLayout.Refreshing = true;
//myProgressBar.Visibility = ViewStates.Visible;
}
};
}
public override bool OnKeyDown(Android.Views.Keycode keyCode, Android.Views.KeyEvent e)
{
if (keyCode == Keycode.Back && myWebView.CanGoBack())
{
myWebView.GoBack();
return true;
}
return base.OnKeyDown(keyCode, e);
}
public override bool OnOptionsItemSelected(IMenuItem item)
{
if (item.ItemId == Android.Resource.Id.Home)
Finish();
return base.OnOptionsItemSelected(item);
}
}
public class WebClient : WebViewClient
{
public delegate void ToggleProgreeBar(int state);
public ToggleProgreeBar myOnProgressChanged;
public override bool ShouldOverrideUrlLoading(WebView view, string url)
{
view.LoadUrl(url);
return true;
}
public override void OnPageStarted(WebView view, string url, Bitmap favicon)
{
if (myOnProgressChanged != null)
{
myOnProgressChanged.Invoke(1);
}
base.OnPageStarted(view, url, favicon);
}
public override void OnPageFinished(WebView view, string url)
{
if (myOnProgressChanged != null)
{
myOnProgressChanged.Invoke(0);
}
base.OnPageFinished(view, url);
}
SwipeRefreshLayout.Refresh get triggered when user down swipes. According to your requirement, when user swipes, you need to manually refresh the webview:
private void SwipeRefresh_Refresh(object sender, System.EventArgs e)
{
webview.LoadUrl(webview.Url);
}
And register myWebClient.myOnProgressChanged event inside OnCreate. So codes should looks like below:
public class MainActivity : Activity
{
WebView webview;
SwipeRefreshLayout mySwipeRefresh;
WebClient myWebClient;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
webview = FindViewById<WebView>(Resource.Id.webview);
webview.Settings.JavaScriptEnabled = true;
webview.LoadUrl("http://www.google.com");
mySwipeRefresh = FindViewById<SwipeRefreshLayout>(Resource.Id.swiperefresh);
myWebClient = new WebClient();
//register myOnProgressChanged here
myWebClient.myOnProgressChanged += (int state) =>
{
if (state == 0)
{
mySwipeRefresh.Refreshing = false;
}
else
{
mySwipeRefresh.Refreshing = true;
}
};
webview.SetWebViewClient(myWebClient);
mySwipeRefresh.Refresh += SwipeRefresh_Refresh;
}
private void SwipeRefresh_Refresh(object sender, System.EventArgs e)
{
//refresh the webview when user swipes.
webview.LoadUrl(webview.Url);
}
}

Xamarin UISwipeGestureRecognizer Renderer

In Xamarin.Forms I want to be able to swipe left and right to navigate a list of pictures. At the moment I just want to be able to fire an event each time a swipe is detected.
child class to be used in the renderer:
public class LRMasterDetailPage : ContentView
{
}
I have a ContentPage that uses the LRM class like this:
public class ImagePage : ContentPage
{
public ImagePage(Photo photo)
{
_image = new WebImage
{
Url = photo.Url,
Placeholder = "placeHolder2.png"
};
var imageView = new LRMasterDetailPage {
Content = _image
};
this.Content = imageView;
}
}
This is my Renderer:
[assembly:ExportRenderer (typeof(LRMasterDetailPage), typeof(LRMDPRenderer))]
namespace Project.iOS
{
public class LRMDPRenderer : ViewRenderer<LRMasterDetailPage,UIView>
{
UISwipeGestureRecognizer swipe;
protected override void OnElementChanged (ElementChangedEventArgs<LRMasterDetailPage> e)
{
base.OnElementChanged (e);
// Do someting else, init for example
swipe = new UISwipeGestureRecognizer();
this.AddGestureRecognizer (swipe);
if (swipe.Direction == UISwipeGestureRecognizerDirection.Left)
{
UpdateLeft ();
}
if (swipe.Direction == UISwipeGestureRecognizerDirection.Right)
{
UpdateRight ();
}
}
protected override void OnElementPropertyChanged (object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName == "Renderer")
return;
base.OnElementPropertyChanged (sender, e);
}
private void UpdateLeft(){
// Insert view of DetailLeft element into subview
// Add button to open Detail to parent navbar, if not yet there
// Add gesture recognizer for left swipe
Console.WriteLine ("Left swipe");
}
private void UpdateRight(){
// same as for left, but flipped
Console.WriteLine ("Right swipe");
}
}
}
When the ContentPage is display the swipe right event is activated, but nothing happens when I try to swipe on top of the image. I am guessing my logic on the renderer is wrong?
After much pain and searching around the web I found the solution.
What you need to do is simple: in the renderer declare and add the gesture. Make sure you create a swipe gesture for right and left each by declaring their direction. from there use a lambda to call the function you want activated for the specific swipe:
Placeholder class for the Swipe Gestures.
public class LRMasterDetailPage : ContentView
{
}
Image Page that holds one image at the time
public class ImagePage : ContentPage
{
//view holding the image
LRMasterDetailPage imageView;
//collection of images using the photo.Url
ObservableCollection<Image> images;
//current image index
int index = 0;
public ImagePage(){
images = new ObservableCollection<Image> ();
imageView = new LRMasterDetailPage {
Content = this.images [index]
};
this.Content = imageView;
}
//Subscribe to the swipeLeft and swipeRight message
protected override void OnAppearing ()
{
base.OnAppearing ();
MessagingCenter.Subscribe <string> (this,"LeftSwipe", (sender) => {
//Do something
if(index < images.Count-1){
index++;
}
imageView.Content = this.images[index];
});
MessagingCenter.Subscribe <string> (this, "RightSwipe", (sender) => {
//Do something
if(index > 0){
index--;
}
imageView.Content = this.images[index];
});
}
protected override void OnDisappearing()
{
base.OnDisappearing();
//this._image = null;
images = null;
MessagingCenter.Unsubscribe<string>(this,"LeftSwipe");
MessagingCenter.Unsubscribe<string>(this, "RightSwipe");
MessagingCenter.Unsubscribe<string>(this, "LongPress");
//GC.Collect();
}
}
Renderer for LRMasterDetailPage
[assembly:ExportRenderer (typeof(LRMasterDetailPage), typeof(LRMDPRenderer))]
namespace Manager.iOS
{
public class LRMDPRenderer : ViewRenderer<LRMasterDetailPage,UIView>
{
UILongPressGestureRecognizer longPressGestureRecognizer;
UIPinchGestureRecognizer pinchGestureRecognizer;
//UIPanGestureRecognizer panGestureRecognizer;
UISwipeGestureRecognizer swipeRightGestureRecognizer;
UISwipeGestureRecognizer swipeLeftGestureRecognizer;
UIRotationGestureRecognizer rotationGestureRecognizer;
protected override void OnElementChanged (ElementChangedEventArgs<LRMasterDetailPage> e)
{
base.OnElementChanged (e);
longPressGestureRecognizer = new UILongPressGestureRecognizer (() => Console.WriteLine ("Long Press"));
pinchGestureRecognizer = new UIPinchGestureRecognizer (() => Console.WriteLine ("Pinch"));
//panGestureRecognizer = new UIPanGestureRecognizer (() => Console.WriteLine ("Pan"));
swipeRightGestureRecognizer = new UISwipeGestureRecognizer ( () => UpdateRight()){Direction = UISwipeGestureRecognizerDirection.Right};
swipeLeftGestureRecognizer = new UISwipeGestureRecognizer ( () => UpdateLeft()){Direction = UISwipeGestureRecognizerDirection.Left};
rotationGestureRecognizer = new UIRotationGestureRecognizer (() => Console.WriteLine ("Rotation"));
if (e.NewElement == null) {
if (longPressGestureRecognizer != null) {
this.RemoveGestureRecognizer (longPressGestureRecognizer);
}
if (pinchGestureRecognizer != null) {
this.RemoveGestureRecognizer (pinchGestureRecognizer);
}
/*
if (panGestureRecognizer != null) {
this.RemoveGestureRecognizer (panGestureRecognizer);
}
*/
if (swipeRightGestureRecognizer != null) {
this.RemoveGestureRecognizer (swipeRightGestureRecognizer);
}
if (swipeLeftGestureRecognizer != null) {
this.RemoveGestureRecognizer (swipeLeftGestureRecognizer);
}
if (rotationGestureRecognizer != null) {
this.RemoveGestureRecognizer (rotationGestureRecognizer);
}
}
if (e.OldElement == null) {
this.AddGestureRecognizer (longPressGestureRecognizer);
this.AddGestureRecognizer (pinchGestureRecognizer);
//this.AddGestureRecognizer (panGestureRecognizer);
this.AddGestureRecognizer (swipeRightGestureRecognizer);
this.AddGestureRecognizer (swipeLeftGestureRecognizer);
this.AddGestureRecognizer (rotationGestureRecognizer);
}
}
private void UpdateLeft(){
MessagingCenter.Send ("Swiped to the left", "LeftSwipe");
}
private void UpdateRight(){
// same as for left, but flipped
MessagingCenter.Send ("Swiped to the Right", "RightSwipe");
}
}
}

Categories