How move the scroll only when there are the keyboard - xamarin.forms

I used this function to move my scroll, but this function is activated when I don't have a keyboard, I only want to use it when there is a keyboard, how can I solve this?
<Entry Placeholder="entry" Focused="EntryKeyboardHandle_Focused"
void EntryKeyboardHandle_Focused(object sender, FocusEventArgs e)
{
Device.BeginInvokeOnMainThread(async () =>
{
await Task.Delay(10);
await MainScroll.ScrollToAsync(0, 100, true);
});
}
I found this thread Xamarin forms check if keyboard is open or not
I have my entry with name "Entry" and in my code behind Entry.Focused += keyboardService.KeyboardIsShown; but I get this error.
The event 'IKeyboardService.KeyboardIsShown' can only appear on the
left hand side of += or -=

ok accourding to the thread that you found you could try this code.
in the contructor add this code
private bool _keyboardIsOn;
cto(){
// Initio
keyboardService.KeyboardIsShown += (sender, e){ _keyboardIsOn = true; }
keyboardService.KeyboardIsHidden += (sender, e){ _keyboardIsOn = false; }
}
No you could check if _keyboardIsOn and add your coditions.

Related

How to use MediaManager plugin and play and pause icon in Xamarin.Forms project

Update:
It's playing well, but when finished playing it's not showing the play button. I have tried I couldn't get it well. Maybe I spiked something else were.
How can I show the play button when a player finished playing?
HomePage.xaml
<Button ImageSource="{Binding PlayIcon}"
Command="{Binding PlayCommand}"
HorizontalOptions="End"
VerticalOptions="End"/>
HomePage.xaml.cs
public HomePage()
{
InitializeComponent();
isPlaying = true;
}
private bool isPlaying;
public bool IsPlaying
{
get { return isPlaying; }
set
{
isPlaying = value;
OnPropertyChanged(nameof(PlayIcon));
}
}
public string PlayIcon { get => isPlaying ? "play.png" : "pause.png"; }
public ICommand PlayCommand => new Command(Play);
private async void Play()
{
if (isPlaying)
{
await CrossMediaManager.Current.Play("file:///android_asset/running.mp3");
IsPlaying = true; ;
}
else
{
await CrossMediaManager.Current.Pause();
IsPlaying = false; ;
}
}
Thank you for your contribution.
to play an asset, use this syntax
private async void PlayButtonClicked(object sender, EventArgs e)
{
// update the button's image
((Button)sender).ImageSource = ImageSource.FromFile("pause.png");
wait CrossMediaManager.Current.Play("file:///android_asset/long-test.mp3");
}
ref: https://github.com/Baseflow/XamarinMediaManager/issues/840
the docs contains a list of events supported by the control, including a MediaItemFinished event

changing mouse cursor in chromiumwebbrowser DragEnter event in WPF

we are using chromiumwebbrowser in WPf application, added DragEnter event handler to the chromiumwebbrowser and call come to us.
wanted to change the cursor,
private void Browser_DragEnter(object sender, DragEventArgs e)
{
var currCursor = Browser.Cursor;
if (e.Effects == DragDropEffects.Move)
{
//Mouse.OverrideCursor = Cursors.Hand;
// Mouse.SetCursor(Cursors.Hand);
Debug.Write("DragDropEffects.Move\n");
}
else
Mouse.SetCursor(Cursors.Hand);
Mouse.OverrideCursor = Cursors.Pen;
Browser.Cursor = Cursors.Pen;
//Mouse.Capture(Browse);
e.Handled = true;
}
nothing works, could some help would solve this issue

how to implement tap gesture in webview to display html not website

I need my web view to be tappable and scrolable. Once I implement on touch the scroll doesnt work. This way i managed to get it working however now i dont know how to make the web view tappable? the ButtonPress does nothing and if i use Move then i am just scrolling
This my my render in mu droid project
class ExtendedWebViewClient : WebViewClient
{
WebView _webView;
public async override void OnPageFinished(WebView view, string url)
{
try
{
_webView = view;
if (_xwebView != null)
{
view.Settings.JavaScriptEnabled = true;
await Task.Delay(100);
string result = await _xwebView.EvaluateJavaScriptAsync("(function(){return document.body.scrollHeight;})()");
_xwebView.HeightRequest = Convert.ToDouble(result);
}
base.OnPageFinished(view, url);
}
catch (Exception ex)
{
Console.WriteLine($"{ex.Message}");
}
}
public override bool ShouldOverrideUrlLoading(Android.Webkit.WebView view, IWebResourceRequest request)
{
return true;
}
}
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.WebView> e)
{
base.OnElementChanged(e);
_xwebView = e.NewElement as ExtendedWebView;
_webView = Control;
if (e.OldElement == null)
{
_webView.SetWebViewClient(new ExtendedWebViewClient());
}
if (e.OldElement != null)
{
Control.Touch -= ControlOnTouch;
Control.ScrollChange -= ControlOnScrollChange;
}
if (e.NewElement != null)
{
Control.Touch += ControlOnTouch;
Control.ScrollChange += ControlOnScrollChange;
}
}
private void ControlOnScrollChange(object sender, ScrollChangeEventArgs scrollChangeEventArgs)
{
if (scrollChangeEventArgs.ScrollY > 0 && scrollChangeEventArgs.OldScrollY == 0)
{
Control.Parent.RequestDisallowInterceptTouchEvent(true);
}
}
private void ControlOnTouch(object sender, Android.Views.View.TouchEventArgs e)
{
// Executing this will prevent the Scrolling to be intercepted by parent views
switch (e.Event.Action)
{
case MotionEventActions.Down:
Control.Parent.RequestDisallowInterceptTouchEvent(true);
break;
case MotionEventActions.Up:
Control.Parent.RequestDisallowInterceptTouchEvent(false);
break;
case MotionEventActions.ButtonPress:
Console.WriteLine("press");
break;
case MotionEventActions.Mask:
Console.WriteLine("mask");
break;
}
// Calling this will allow the scrolling event to be executed in the WebView
Control.OnTouchEvent(e.Event);
}
Instead of using the gesture recognizer on your webview, you can use the Focused event like following . It will been invoked when you tap the WebView .
var wb = new WebView
{
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.FillAndExpand,
Source = "xxx",
};
wb.Focused += (sender, event) =>
{
//Handle your logic here!
wb.Unfocus();
};
Unfocus() is used if you want to implement your logic everytime the webview is tapped.

Allow user to zoom with mousewheel using the cefsharp browser

I am running cefsharp/75. I want to turn on zooming with the ctrl key and the mousewheel. My event handler is never triggered. And if you hold ctrl and use the mouse will the screen doesn't move. So something inside the control and handling the event. Is there just a setting I am missing?
I added mouse and keyboard zoom. In init section subscribe for events
cefBrowser.PreviewMouseWheel += CefBrowser_PreviewMouseWheel;
cefBrowser.KeyUp += CefBrowser_KeyUp;
I used PreviewMouseWheel to avoid scrolling during zoom (e.Handled = true).
private void CefBrowser_PreviewMouseWheel(object sender, MouseWheelEventArgs e) {
if (Keyboard.Modifiers != ModifierKeys.Control)
return;
if (e.Delta > 0)
cefBrowser.ZoomInCommand.Execute(null);
else
cefBrowser.ZoomOutCommand.Execute(null);
e.Handled = true;
}
private void CefBrowser_KeyUp(object sender, KeyEventArgs e) {
if (Keyboard.Modifiers != ModifierKeys.Control)
return;
if (e.Key == Key.Add)
cefBrowser.ZoomInCommand.Execute(null);
if (e.Key == Key.Subtract)
cefBrowser.ZoomOutCommand.Execute(null);
if (e.Key == Key.NumPad0)
cefBrowser.ZoomLevel = 0;
}
So now cef zooming almost like chrome

How to use both GestureRecognizers and Effects in Xamarin Forms at the same time?

I have added a tap gesture recognizer to my StackLayout and I want to change the background color when it is tapped so that the user recognizes that the layout has been tapped
<StackLayout.GestureRecognizers>
<TapGestureRecognizer
Tapped="Preferences_Clicked"
NumberOfTapsRequired="1"/>
</StackLayout.GestureRecognizers>
Should I use animation for this?
UPDATE:
by changing background color, I mean an effect, something like a highlight, just like when you selsct an item in a ListView
Use this code for toggle color
int tapCount=0;
void Preferences_Clicked(object sender, EventArgs args)
{
tapCount++;
var stackLayout = (StackLayout)sender;
if (tapCount % 2 == 0) {
stackLayout.BackgroundColor = Color.Default;
} else {
stackLayout.BackgroundColor = Color.Accent;
}
}
I could find my answer, I simply added this bit of code to the Tapped method, and got what I wanted
public async void Preferences_Clicked(object sender, EventArgs e)
{
const int _animationTime = 50;
try
{
var layout = (StackLayout)sender;
await layout.FadeTo(0.5, _animationTime);
await layout.FadeTo(1, _animationTime);
}
catch (Exception ex)
{
}
}

Resources