I use DrawerLayout widget in Monodroid, ported from this tutorial http://developer.android.com/training/implementing-navigation/nav-drawer.html.
The drawer works fine, however the DrawerOpened and DrawerClosed event handler is never invoked when the drawer is either opened or closed. I use the built-in listener from the widget. The drawer layout is placed in fragment.
Any idea? Help is greatly appreciated.
this.DrawerLayout.DrawerClosed += delegate(object sender, Android.Support.V4.Widget.DrawerLayout.DrawerClosedEventArgs e)
{
this.Activity.ActionBar.SetTitle(Resource.String.ApplicationName);
this.Activity.InvalidateOptionsMenu();
};
this.DrawerLayout.DrawerOpened += delegate(object sender, Android.Support.V4.Widget.DrawerLayout.DrawerOpenedEventArgs e)
{
this.Activity.ActionBar.SetTitle(this.Title);
this.Activity.InvalidateOptionsMenu();
};
this.DrawerLayout.SetDrawerListener(this.DrawerToggle);
I have just come across this problem myself. I believe this event is fired if you set the drawer listener before setting the delegates for the Drawer Opened and Closed events.
So just change your code to this:
this.DrawerLayout.SetDrawerListener(this.DrawerToggle);
this.DrawerLayout.DrawerClosed += delegate(object sender, Android.Support.V4.Widget.DrawerLayout.DrawerClosedEventArgs e)
{
this.Activity.ActionBar.SetTitle(Resource.String.ApplicationName);
this.Activity.InvalidateOptionsMenu();
};
this.DrawerLayout.DrawerOpened += delegate(object sender, Android.Support.V4.Widget.DrawerLayout.DrawerOpenedEventArgs e)
{
this.Activity.ActionBar.SetTitle(this.Title);
this.Activity.InvalidateOptionsMenu();
};
This seemed to work for me.
Related
I work on a Xamarin.Forms app (UWP!). It has a Master-Details architecture. The Master page has a ListView, and each item in it opens a corresponding Detail page. The first Detail page has only a WebView that plays a YouTube video upon loading. The second Detail view has just a placeholder label for now.
Where I switch from first Detail page to the second, the sound of the video from the first Detail page is still heard. And when I switch back to the first Detail page, the video loads again, and now I hear two voices. How can I stop the video upon switching to the second Detail page and resume when going back? If this is not possible, how can I just stop the video upon leaving its Detail page?
I guess I could do something in an overridden OnDisappearing() method of the detail page:
protected override void OnDisappearing()
{
MyWebView.NavigateToString(""); // This does not work, as NavigateToString() is not part of WebView.
base.OnDisappearing();
}
What can I use to stop playing video in WebView?
Could you please tell what I can use to stop playing video in WebView?
For your requirement, you could approach with injecting Eval java script.
private void Button_Clicked(object sender, EventArgs e)
{
string pausefunctionString = #"
var videos = document.querySelectorAll('video');
[].forEach.call(videos, function(video) { video.pause(); });
";
MyWebView.Eval(pausefunctionString);
}
Update
I have re-checked your issue, when you navigate to another page, the WebView has not be released correctly. If you want to stop the WebView video play, you could make it navigate to blank page via custom WebViewRenderer.
[assembly: ExportRenderer(typeof(WebView), typeof(CustomWebViewRender))]
namespace App4.UWP
{
class CustomWebViewRender : WebViewRenderer
{
private Windows.UI.Xaml.Controls.WebView _WebView;
protected override void OnElementChanged(ElementChangedEventArgs<WebView> e)
{
base.OnElementChanged(e);
if (Control != null)
{
var source = Element.Source as UrlWebViewSource;
_WebView = new Windows.UI.Xaml.Controls.WebView();
SetNativeControl(_WebView);
}
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == WebView.SourceProperty.PropertyName)
{
var source = Element.Source as UrlWebViewSource;
if (source.Url == string.Empty)
{
_WebView.NavigateToString(string.Empty);
}
}
}
}
}
Usage
protected override void OnAppearing()
{
base.OnAppearing();
MyWebView.Source = "https://www.youtube.com";
}
protected override void OnDisappearing()
{
MyWebView.Source = string.Empty;
base.OnDisappearing();
}
Xaml
<WebView HeightRequest="500" WidthRequest="500" x:Name="MyWebView"/>
I have a requirement in my app where I need to display an image to indicate whether the app is connected to network or not.I was able to do it using the Connectivity Plugin by James Montemagno.But I want to implement it using Reachability class.When I implement the Reachability class the OnChange method never fires.When I turn ON or turn OFF the wifi the OnChange is never called.Can somebody guide me on how to achieve this?
public static event EventHandler ReachabilityChanged;
static void OnChange(NetworkReachabilityFlags flags)
{
ReachabilityChanged?.Invoke(null, EventArgs.Empty);
}
Put below code in your PCL App()
CrossConnectivity.Current.ConnectivityChanged += (object sender, Plugin.Connectivity.Abstractions.ConnectivityChangedEventArgs e) =>
{
bool IsInternetConnected = e.IsConnected;
}
You can pass connectivity status using Messaging Center refer this
Ok let me explain how use the Rehability.cs class
1) add this file in your project.
https://github.com/xamarin/ios-samples/blob/master/ReachabilitySample/reachability.cs
2) change the namespace for the name of your project .
3) Declare this variables in your ViewController like the image
NetworkStatus remoteHostStatus, internetStatus, localWifiStatus;
4) In your ViewController add this method . The line TableView.ReloadData (); put the name of your table o item that you wanna update.
void UpdateStatus (object sender, EventArgs e)
{
remoteHostStatus = Reachability.RemoteHostStatus ();
internetStatus = Reachability.InternetConnectionStatus ();
localWifiStatus = Reachability.LocalWifiConnectionStatus ();
TableView.ReloadData ();
}
5) In the ViewDidLoad add this two lines
UpdateStatus (null, null);
Reachability.ReachabilityChanged += UpdateStatus;
for a more understand of the code download this example and run the app in you Visual studio . https://developer.xamarin.com/samples/monotouch/ReachabilitySample/
Regards
If I create CLLocationManager instance only on UIThread, LocationUpdated event will fired.
Why does this happen?
There is no any clue in Xamarin and Apple documentation that CLLocationManager must be created on UIThread.
Some code asks locationManager.RequestWhenInUseAuthorization ();
NSLocationWhenInUseUsageDescription is setted in Info.plist
private void CreateLocationManagerWorkingOption () {
ExecuteOnMainThread (() => {
locationManager = new CLLocationManager ();
});
locationManager.LocationsUpdated += (object sender, CLLocationsUpdatedEventArgs e) => {
OnLocationChanged (locationManager,e.Locations [e.Locations.Length - 1]);
};
}
private void CreateLocationManagerNotWorkingOption () {
ExecuteOnSomeThread(()=> {
locationManager = new CLLocationManager ();
});
locationManager.LocationsUpdated += (object sender, CLLocationsUpdatedEventArgs e) => {
OnLocationChanged (locationManager,e.Locations [e.Locations.Length - 1]);
};
}
private void StartTrackingImpl() {
ExecuteOnMainThread (() => locationManager?.StartUpdatingLocation ());
}
I guess I know why.
First of all, I was experiencing the same problem. I could not get LocationUpdates working on three different devices. I've tested a lot, but CLLocationManager event still was not fired. I came across this question and finally figured out why events were not fired on my devices.
I instantiated CLLoationManager in a threadPool. ThreadPools are managed by .NET. So, a thread, where I instantiated CLLoationManager were finished after a while, therefore there's nowhere to fire the events.
Hope my answer helps!
You can create and handle it from every thread that has an active run loop.
From the CLLocationManagerDelegate documentation:
The methods of your delegate object are called from the thread in which you started the corresponding location services. That thread must itself have an active run loop, like the one found in your application’s main thread.
I am using a Map control in my app, and i need to set the visible region in such a way that it should cover all the pins.
Irony is same code doesn't work on both the platform, iOS works awkwardly , below code yield almost the same visible region in both platform.
if(Device.OS == TargetPlatform.iOS)
customMap.MoveToRegion (MapSpan.FromCenterAndRadius (customMap.CustomPins [0].Pin.Position, Distance.FromMiles (0.20)));
if(Device.OS == TargetPlatform.Android)
customMap.MoveToRegion (MapSpan.FromCenterAndRadius (customMap.CustomPins [0].Pin.Position, Distance.FromMiles (55.0)));
Can anyone explains it? why I need to code like it?
i have found a workaround , i am waiting for some explanation before accepting my own answer for it
Device.StartTimer(TimeSpan.FromMilliseconds(500), () =>
{
customMap.MoveToRegion(MapSpan.FromCenterAndRadius(customMap.CustomPins [0].Pin.Position, Distance.FromMiles(55.0)));
return false;
});
I was running into a problem where the MovetoRegion was being delayed (15-30 seconds) when trying to center on the users current location using the Xamarin Geolocator Plugin, on both IOS and Android. Things work alot better with Saket Kumar's approach with the 500ms delay. Here is my code snippet, hope this helps someone.
private void CenterOnMe_Clicked(object sender, EventArgs e)
{
var locator = CrossGeolocator.Current;
var t = Task.Run(async () =>
{
var position = await locator.GetPositionAsync(TimeSpan.FromSeconds(10));
Device.StartTimer(TimeSpan.FromMilliseconds(500), () =>
{
AroundMeMap.MoveToRegion(
MapSpan.FromCenterAndRadius(
new Position(position.Latitude, position.Longitude), Distance.FromMiles(1)));
return false;
});
});
}
when click on app bar button ..clear the text in textbox in windows phone 7 is not working.
private void ApplicationBarIconButton_Click(object sender, EventArgs e)
{
txtMessage.Text = string.Empty;
UpdateLayout();
}
Please suggest any solutions
This seems to be (as you thought) an issue with the Korean IME itself. Try setting focus to it first:
private void ApplicationBarIconButton_Click(object sender, EventArgs e) {
// Set focus to the page ensuring all TextBox controls are properly commited
// and that the IME session has been correctly closed.
this.Focus();
MessageTextBox.Text = "";
}
Shamelessly copy-pasted, read here for a closer look at the problem.