How can I implement SfMap in Xamarin - xamarin

How can I implement SfMaps in my project which is based on MVVM structure? 1st I have to get my current location and show it on the map then I have to drag that marker point anywhere and set it as the current location

Add SfMaps to your project from NuGet. In android no need for native configurations, but to launch the SfMaps in iOS, call the SfMapsRenderer.Init() in the FinishedLaunching overridden method of the AppDelegate class
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
…
global::Xamarin.Forms.Forms.Init ();
Syncfusion.SfMaps.XForms.iOS.SfMapsRenderer.Init();
LoadApplication (new App ());
…
}
Add the following namespace in your view file.
xmlns:maps="clr-namespace:Syncfusion.SfMaps.XForms;assembly=Syncfusion.SfMaps.XForms"
And add the below code to initializing map in view
<maps:SfMaps >
</maps:SfMaps>
this is a very basic way to add SFMap in Xamarin. forms for more details and help
please visit this link: https://help.syncfusion.com/xamarin/maps/getting-started?cs-save-lang=1&cs-lang=xaml

Related

Xamarin Forms - How to open specific page after clicking on notification when the app is closed?

I'm actually working on a Xamarin Forms application that need push notifications. I use the Plugin.PushNotification plugin.
When the app is running in the foreground or is sleeping (OnSleep), I have no problem to open a specific page when I click on a notification that I receive. But I was wondering how can I do that when the app is closed. Thanks!
I finally found the answer by myself and I want to share it in case someone needs it.
Nota bene: according to the official documentation of the plugin, it's Xam.Plugin.PushNotification that is deprecated. I use the new version of this plugin, Plugin.PushNotification which uses FCM for Android and APS for iOS.
There is no significant differences to open a notif when the app is running, is sleeping or is closed. Just add the next callback method in the OnCreate method (MyProject.Droid > MainApplication > OnCreate) and FinishedLaunching method (MyProject.iOS > AppDelegate > FinishedLaunching):
CrossPushNotification.Current.OnNotificationOpened += (s, p) =>
{
// manage your notification here with p.Data
App.NotifManager.ManageNotif(p.Data);
};
Common part
App.xaml.cs
// Static fields
// *************************************
public static NotifManager NotifManager;
// Constructor
// *************************************
public App()
{
...
NotifManager = new NotifManager();
...
}
NotifManager.cs
public class NotifManager
{
// Methods
// *************************************
public void ManageNotif(IDictionary<string, object> data)
{
// 1) switch between the different data[key] you have in your project and parse the data you need
// 2) pass data to the view with a MessagingCenter or an event
}
}
Unfortunately there is no succinct answer for either platform. Generally speaking, you need to tell the OS what to do when it starts the app as a result of the push notification. On both platforms, you should also consider what API level you are targeting, otherwise it won't work or even crash the app.
On iOS, you will need to implement this method in AppDelegate appropriately: FinishedLaunching(UIApplication application, NSDictionary launchOptions). The launchOptions will have the payload from the push notification for you to determine what to do with it (e.g. what page to open). For more information on iOS, Xamarin's documentation is a good place to start.
Android has a more complicated topology in terms of more drastic differences between API levels, whether you are using GCM/FCM, as well as requiring more code components. However, to answer the question directly, you will need to handle this in OnCreate(Bundle savedInstanceState) of your main Activity. If you are using Firebase, the push notification payload is available in Intent.Extras. Again, Xamarin's documentation has a good walkthrough.
Finally, note that the Plugin.PushNotification library you are using has been deprecated. I suggest you either change your library and/or your implementation soon. Part of the reason that library has been deprecated is because Google has deprecated the underlying Google Cloud Messaging (GCM) service, which will be decommissioned on April 11, 2019.

Xamarin.Forms How to switch pages using MVVMLight

I'm currently working on a Xamarin.forms project using .NET Standard as code sharing strategy. I try to use the MVVM pattern by using the MvvmLightLibsStd10 library. I already successfully setup the MVVM structure by using this tutorial:
https://www.linkedin.com/pulse/build-xamarinforms-net-standard-mvvm-light-app-rafael-carvalho
I can't use Navigation.PushAsync(new Page()); because it only works in code behind and not in the ViewModel.
I already tried to Pass Navigation trough the VM constructor, like describe over here:
Xamarin.form Page Navigation in mvvm
But when I try this method, an error occurred at "LoadApplication(new DemoMVVM2.App());" in MainPage.
How can I switch pages using MVVM Xamarin.Forms with MVVMLight (based on the code from my first url)?
but I have no Idea how I can switch Pages via the ViewModel and keeping the header with back button.
Generally when working with MVVMLight you'll be using a NavigationService.
This class can be constructor injected in your VM, thanks to the build in IOC in MVVMLight.
With it you can do Navigate and GoBack in your VM, triggering a real navigation on the current stack.
Only thing that you maybe missed, is the fact that you need to write one yourself for Xamarin forms.
But Laurent Bugnion ( the owner of MVVMLight ) supplied an example available here:
https://github.com/lbugnion/sample-2016-vslive-crossplatform/blob/master/Flowers/Flowers.Forms/Flowers.Forms/Helpers/NavigationService.cs
You can pass a callback to your ViewModel(VM) and on Command or whatever action call your navigation code which is in your page (View). this way you can keep your navigation code in your page and your binding logic in your ViewModel.
interface NavHandler{
void navigateToSomeView();
}
public class MyPage : ContentPage,NavHandler{
public MyPage(){
BindingContext = new MyViewModel(this);
}
void navigateToSomeView(){
Navigation.PushAsync(new Page2());
}
}
public class MyViewModel{
NavHandler handler;
public MyViewModel(NavHandler handler){
this.handler = handler
}
//Your action
this.btnClicked = new Command(async()=>{
handler.navigateToSomeView()
}
}

Custom video page for xamarin ios

I create some project that consists from videoplayer. For Xamarin android I implemented through messaging center.
On android:
Page(xamarin forms) -> (click button and sending message to MainActivity) -> calling MyVideoActivity via intent
With android all works perfectly.
But I am newbie with ios and xamarin ios also.
And I don't know is this real to implement the same in xamarin ios through messaging center ? Also I would like use .xib file(with ui, taken from Xcode). I am not sure how to implement it. I would like to do it through messaging center -> calling custom ViewController with my .xib file. Is it real?
p.s. I've found some example with PageRenderer, but don't investigate it. I am not sure that it will be suitable for me.
Call the following methods in your MessagingCenter.Subscribe lambda:
XIB-based ViewController:
var vc = new XIBBasedViewController();
// Set any public properties / call methods on your `XIBBasedViewController`
InvokeOnMainThread(() => UIApplication.SharedApplication.Windows[0].RootViewController.PresentViewController(vc, true, null));
Storyboard-based ViewController:
var storyboard = UIStoryboard.FromName("StoryBoardFileNameMinusExtension", null);
var vc = storyboard.InstantiateViewController("AStoryBoardIDAssignedToYourViewController") as AViewControllerSubClass;
// Set any public properties / call methods on your `AViewControllerSubClass`
InvokeOnMainThread(() => UIApplication.SharedApplication.Windows[0].RootViewController.PresentViewController(vc, true, null));
Note: A good place to setup your messaging center subscribes is in the FinishedLaunching of the AppDelegate class.

Xamarin Forms Notification

I try to realize a shared project with Xamarin Forms for Android, ios and WinPhone. I want to use notifications and I followed this sample (https://github.com/edsnider/Xamarin.Plugins/tree/master/Notifier) and was able to create a local notification in Android. But i still do not understand how to work with that like Android application. Open a page or check if the page is already active or abort a broadcast for all platforms.
Any links that could help me? Am I correct that it only works with an Interface that i have to implement in each platform or does exist any other solution?
My basic problem is that I get messages from the xmpp-protocol and want to send message notifications to the user or update the ui if open...
Thanks for your help...
You do not have to add any interfaces to your platform projects, Just intall nuget package to both projects Android/IOS.
on IOS you should add:
// Ask the user for permission to get notifications on iOS 8.0+
if (UIDevice.CurrentDevice.CheckSystemVersion (8, 0)) {
var settings = UIUserNotificationSettings.GetSettingsForTypes (
UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound,
new NSSet ());
UIApplication.SharedApplication.RegisterUserNotificationSettings (settings);
}
to your method:
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
in AppDelegate:
Then in your shared project you call:
using Plugin.LocalNotifications.Abstractions;
CrossLocalNotifications.Current.Show("You've got mail", "You have 793 unread messages!");
As of replacing your current page, you do not have to do anything, Xamarin Forms will handle that for you.
Also on android, you can controll, you application relaunch behavior by setting LaunchMode of your MainActivity:
Example:
LaunchMode = Android.Content.PM.LaunchMode.SingleInstance, AlwaysRetainTaskState = true
To read more about LauchModes on Android, please read:
https://developer.android.com/guide/components/tasks-and-back-stack.html
And check additionl activity flags:
FLAG_ACTIVITY_NEW_TASK
FLAG_ACTIVITY_CLEAR_TOP
FLAG_ACTIVITY_SINGLE_TOP
Currently this plugin does not allow to catch any notification inside app.
But you can do this, by using platform specific functionality
IOS:
Please refer to
https://developer.xamarin.com/guides/cross-platform/application_fundamentals/notifications/ios/remote_notifications_in_ios/
public override void ReceivedLocalNotification(UIApplication application, UILocalNotification notification)
{
MessagingCenter.Send<MainPage> (this, "IOS notification received");
}
Android:
After reading through source of this plugin, i've discovered, that youcan check Bundle in your on create method for key ScheduledAlarmHandler.LocalNotificationKey
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
if(bundle.ContainsKey(ScheduledAlarmHandler.LocalNotificationKey)){
//App launched form notification
MessagingCenter.Send<MainPage> (this, "Android notification received");
}
}
Also you can try to use BroadcastReceiver class;
https://developer.xamarin.com/api/type/Android.Content.BroadcastReceiver/

Xamarin.forms OnAppLinkRequestReceived

I'm trying to use https://developer.xamarin.com/api/member/Xamarin.Forms.Application.OnAppLinkRequestReceived/p/System.Uri/ but can't seem to get it to be called in any way.
Did anyone ever tried to implement this new method?
I've tried several ways to implement different deeplinks, all of them open the app fine so they're correctly configured but that method never gets called.
Thanks.
This is how I set it up in Android.
Put this above your MainActivity.cs
[IntentFilter(new[] { Android.Content.Intent.ActionView },
Categories = new[]
{
Android.Content.Intent.CategoryDefault,
Android.Content.Intent.CategoryBrowsable
},
DataScheme = "http",
DataPathPrefix = "/tesla/",
DataHost = "exrin.net")]
This registers the activity when the app is installed. Change the URL to your desired URL.
For Android only (no need to do this with iOS) you need to also install the Nuget Xamarin Forms AppLinks
In your OnCreate make sure you do this after your Xamarin Forms Init
AndroidAppLinks.Init(this);
Then when you load the URL in a browser (in my example http://exrin.net/tesla) you will get this:
Then if you open the app it will enter here with the full URL as the URI parameter. This is in the App.cs (Xamarin.Forms.Application)
protected override void OnAppLinkRequestReceived(Uri uri)
{
base.OnAppLinkRequestReceived(uri);
}
You can then decode the URI as you see fit to move to the specific page in your app that the URL relates to/
More details at Xamarin Forms AppLinks
Also, your MainActivity must be derived from FormsAppCompatActivity for it to work. Once I changed it to that from FormsApplicationActivity, it started working.

Resources