Xamarin.Forms - MKMapView iOS Render - xamarin.forms

I tried using Xamarin.Forms.Maps, and it is missing many features, so I decided to make an iOS ViewRenderer with the MKMapView and adding custom pin images and such, but I am not sure how to make a ViewRenderer and how MKMapView works. Can someone be kind enough to show me how it works and give me a small quick example of just showing the map.
I just want to make a custom render for ios that will show MKMapView, the rest i can probably figure out, but I cant even figure out how to make it show from the viewrenderer.

Simple example on how to build your Custom ViewRenderer
in your PCL project create something that inehrits from view:
public class CustomMap: View
{
public static readonly BindableProperty PinsItemsSourceProperty = BindableProperty.Create ("PinsItemsSource ", typeof(IEnumerable), typeof(CustomMap), null, BindingMode.OneWay, null, null, null, null);
public IEnumerable PinsItemsSource {
get {
return (IEnumerable)base.GetValue (CustomMap.PinsItemsSourceProperty );
}
set {
base.SetValue (CustomMap.PinsItemsSourceProperty , value);
}
}
}
Then on your IOS create your custom renderer for that view like so:
[assembly: ExportRenderer(typeof(CustomMap), typeof(CustomMapRenderer ))]
namespace Xamarin.Forms.Labs.iOS.Controls
{
public class CustomMapRenderer : ViewRenderer<CustomMap,MKMapView >
{
protected override void OnElementChanged (ElementChangedEventArgs<CustomMap> e)
{
base.OnElementChanged (e);
var mapView = new MKMapView (this.Bounds);
mapView.AutoresizingMask = UIViewAutoresizing.FlexibleDimensions;
foreach(item in e.NewElement.PinsItemsSource )
{
//add the points
var annotation = new BasicMapAnnotation (new CLLocationCoordinate2D(x,y), "something", "something");
mapView.AddAnnotation(annotation);
}
base.SetNativeControl(mapView);
}
}
ps: i wrote this code on the fly from my head and i didn't tested but it should help you get going

Related

How to add a custom Mass Action button to a GI in Acumatica

Using Build 21.210.0030
I’m a bit of a ‘noob’ to Acumatica dev and I have a question concerning toolbar buttons on a Generic Inquiry (GI) with the intention of performing a Mass Action. The following PXAction works on the Vendor form (../Main?ScreenId=AP303000&AcctCD=AASERVICES):
public class VendorMaint_Extension : PXGraphExtension<VendorMaint>
{
public SelectFrom<PX.Objects.CR.BAccount>.View suppliers;
#region buttons
public PXAction<PX.Objects.CR.BAccount> myButton;
[PXButton(CommitChanges = true)]
[PXUIField(DisplayName = "Verify Now", IsDirty = true)]
public virtual IEnumerable MyButton(PXAdapter adapter)
{
BAccount supplier = suppliers.Current;
PX.Objects.CR.BAccountExt bAccountExt = PXCache<BAccount>.GetExtension<PX.Objects.CR.BAccountExt>(supplier);
// do my stuff...
//...
//...
return adapter.Get();
}
#endregion buttons
//...
//...
My GI is a filtered copy of AP-Vendors (AP3030PL) but I can’t get my button to display in the list of available Mass Actions. I’ve tried adding the following in the class (above)
public override void Initialize()
{
base.Initialize();
myButton.IsMass = true;
Base.action.AddMenuAction(this.myButton);
}
and I’ve tried many variations of this Initialize method but none seem to make a difference. I've also tried to implement the answers provided in these previous questions (to no avail):
How to add action button in Inquiry page? and
Add Custom action to GI Mass Actions dropdown
I think part of my issue is that I don't fully understand whether to use BAccount, VendorR and/or Vendor yet.
I fully accept that I might be doing it all wrong so any and all help will be gratefully appreciated.
Thanks
Turns out it was relatively straight-forward, very similar to the way processing forms work. I had to change the button from a PXButton to a PXProcessButton in my VendorMaint_Extension class and move the code (that does the actual work) into a static method used by the PXLongOperation e.g.
```
[PXProcessButton(CommitChanges = true)]
[PXUIField(DisplayName = "Verify Now", IsDirty = true)]
public virtual IEnumerable MyButton(PXAdapter adapter)
{
bool isMassProcess = adapter.MassProcess;
List<BAccount> vendorList = new List<BAccount>();
foreach (BAccount bAcc in adapter.Get<BAccount>())
{
PX.Objects.CR.BAccountExt bAccountExt = PXCache<BAccount>.GetExtension<PX.Objects.CR.BAccountExt>(bAcc);
if (bAccountExt.UsrIsSomething == true)
{
vendorList.Add(bAcc);
}
}
// trigger the Save action to save changes in the database.
Base.Save.Press();
PXLongOperation.StartOperation(this, delegate ()
{
DoMyStuff(vendorList, isMassProcess);
});
return vendorList;
}
```

How to set certain icon instead of default pins?

I'm trying to use certain icon instead of default Xamarin.Forms pins on Map.
So I created CustomisePin class that inheritance Pin.
using Xamarin.Forms.Maps;
namespace agroNet.Tools
{
public class CustomPin : Pin
{
public string PinIcon { get; set; }
}
}
Here is what I tried in my ViewModel
private Map _map;
public IrrigNetViewModel(Map map)
{
dialog = UserDialogs.Instance.Loading(AppResource.LocalizationResource.Loading);
TabTappedCommand = new Command((tabName) => OnTapClicked(tabName.ToString()));
HideListOnTapCommand = new Command(HideListOnTap);
_map = map;
GetData();
}
And here is method wor set pins on positions.
public void LoadMapTab()
{
//var irrigNetPins = new List<CustomPin>();
foreach (var item in IrrigNetCollection)
{
//var pins = new CustomPin
//{
// Label = item.StationName,
// Position = new Position(item.StationLatitude, item.StationLongitude),
// PinIcon = "satellite.png"
//};
_map.Pins.Add(new CustomPin
{
Label = item.StationName,
Position = new Position(item.StationLatitude, item.StationLongitude),
//PinIcon = P
});
_map.MoveToRegion(
MapSpan.FromCenterAndRadius(new Position(item.StationLatitude, item.StationLongitude),
Distance.FromKilometers(30)));
//irrigNetPins.Add(pins);
}
//return irrigNetPins;
}
In LoadMapTab under the comment lines is what I have tried to set pin icon.
And here is part of View if it's important because of Binding Context.
public partial class IrrigNetPage : ContentPage
{
public IrrigNetPage()
{
InitializeComponent();
BindingContext = new IrrigNetViewModel(MainMap);
}
}
I find some examples on Google, like:
https://github.com/raechten/BindableMapTest
https://github.com/paulpatarinski/ShouldIWashMyCar
For some reason I can't event run them, but still I tried to use code and no matter what I have Pin are alwas default, or not even show.
What is simple way to set certain icon for pin and is it posiple to render it just with some path to the icon without rendering map also (Because I saw many create costumise map for customised pins).
Also If it's important for someon I'm using MVVM patern.
The official Xamarin sample and documentation is outdated. I modified the official Custom Pin sample in order to use custom pins in iOS and Android. The project is hosted on GitHub.
An issue is reported about the official sample over here.

Xamarin Forms Load Resources on startup

I have an Xamarin Forms app with MvvmCross for Android and IOS and I would like to add a dark theme. My idea was to have to dictionaries with the ressources for either the dark or the light theme and load the one I need on startup.
I added this after I registered the dependencies in my MvxApplication:
if (Mvx.IoCProvider.Resolve<ISettingsManager>().Theme == AppTheme.Dark)
{
Application.Current.Resources.Add(new ColorsDark());
}
else
{
Application.Current.Resources.Add(new ColorsLight());
}
ColorsDark and ColorsLight are my ResourceDictionary. After that i can see the new Dictionary under Application.Current.Resources.MergedDictionaries but the controls can't find the resources as it seems. However it does work when I add it to the App.xaml
<ResourceDictionary Source="Style/ColorsDark.xaml" />
Do I have to put move that another part in the code or is that a wrong approach at all?
Personally don't like this approach at all. What i do: have a static class with all the colors, sizes etc. defined in static fields. At app startup or at app reload after changing skin just call ex: UiSettings.Init() for this ui definitions static class, like follows:
public static class UiSettings
{
public static Init()
{
if (Settings.AppSettings.Skin=="butterfly")
{
ColorButton = Color.Blue;
TitleSize= 12.0;
}
else
if (Settings.AppSettings.Skin=="yammy")
{
ColorButton = Color.Red;
if (Core.IsAndroid)
ButtonsMargin = new Thickness(0.5,0.6,0.7,0.8);
}
// else just use default unmodified field default values
}
public static Color ColorButton = Color.Green;
public static Thickness ButtonsMargin = new Thickness(0.3,0.3,0.2,0.2);
public static double TitleSize= 14.0;
}
in XAML use example:
Color= "{x:Static xam:UiSettings.ColorProgressBack}"
in code use example:
Color = UiSettings.ColorProgressBack;
UPDATE:
Remember that if you access a static class from different assemblies it is possible that you will access a fresh copy of it with default values where Init() didn't happen, if you face such case call Init() from this assembly too.
If you want something to load up when your app loads then you have to code it in App.xaml.cs
protected override void OnStart ()
{
if (Mvx.IoCProvider.Resolve<ISettingsManager>().Theme == AppTheme.Dark)
{
Application.Current.Resources.Add(new Xamarin.Forms.Style(typeof(ContentPage))
{
ApplyToDerivedTypes = true,
Setters = {
new Xamarin.Forms.Setter { Property = ContentPage.BackgroundImageProperty, Value = "bkg7.png"},
}
});
}
else
{
Application.Current.Resources.Add(new Xamarin.Forms.Style(typeof(ContentPage))
{
ApplyToDerivedTypes = true,
Setters = {
new Xamarin.Forms.Setter { Property = ContentPage.BackgroundImageProperty, Value = "bkg7.png"},
}
});
}
}
In this code I'm setting the BackgroungImage of all of my pages. Hope you'll get the idea from this code.

how to update Visual Studio UI when using DynamicItemStart inside a vsix package

I'm implementing a DynamicItemStart button inside a Menu Controller. I'm loading the dynamic items for this button when Visual Studio starts. Everything is loaded correctly so the initialize method is called an I see all the new items in this Dynamic button. After the package is completely loaded I want to add more items to this Dynamic button, but since the package is already loaded the initialize method is not called again and I cannot see the new items in this Dynamic button. I only see the ones that were loaded when VS started.
Is there any way that I can force the update of this Dynamic button so it shows the new items?. I want to be able to update the VS UI after I added more items but outside the Initialize method.
The implementation I did is very similar to the one showed on this msdn example:
http://msdn.microsoft.com/en-us/library/bb166492.aspx
Does anyone know if an Update of the UI can be done by demand?
Any hints are greatly appreciated.
I finally got this working. The main thing is the implementation of a derived class of OleMenuCommand that implements a new constructor with a Predicate. This predicate is used to check if a new command is a match within the DynamicItemStart button.
public class DynamicItemMenuCommand : OleMenuCommand
{
private Predicate<int> matches;
public DynamicItemMenuCommand(CommandID rootId, Predicate<int> matches, EventHandler invokeHandler, EventHandler beforeQueryStatusHandler)
: base(invokeHandler, null, beforeQueryStatusHandler, rootId)
{
if (matches == null)
{
throw new ArgumentNullException("Matches predicate cannot be null.");
}
this.matches = matches;
}
public override bool DynamicItemMatch(int cmdId)
{
if (this.matches(cmdId))
{
this.MatchedCommandId = cmdId;
return true;
}
this.MatchedCommandId = 0;
return false;
}
}
The above class should be used when adding the commands on execution time. Here's the code that creates the commands
public class ListMenu
{
private int _baselistID = (int)PkgCmdIDList.cmdidMRUList;
private List<IVsDataExplorerConnection> _connectionsList;
public ListMenu(ref OleMenuCommandService mcs)
{
InitMRUMenu(ref mcs);
}
internal void InitMRUMenu(ref OleMenuCommandService mcs)
{
if (mcs != null)
{
//_baselistID has the guid value of the DynamicStartItem
CommandID dynamicItemRootId = new CommandID(GuidList.guidIDEToolbarCmdSet, _baselistID);
DynamicItemMenuCommand dynamicMenuCommand = new DynamicItemMenuCommand(dynamicItemRootId, isValidDynamicItem, OnInvokedDynamicItem, OnBeforeQueryStatusDynamicItem);
mcs.AddCommand(dynamicMenuCommand);
}
}
private bool IsValidDynamicItem(int commandId)
{
return ((commandId - _baselistID) < connectionsCount); // here is the place to put the criteria to add a new command to the dynamic button
}
private void OnInvokedDynamicItem(object sender, EventArgs args)
{
DynamicItemMenuCommand invokedCommand = (DynamicItemMenuCommand)sender;
if (null != invokedCommand)
{
.....
}
}
private void OnBeforeQueryStatusDynamicItem(object sender, EventArgs args)
{
DynamicItemMenuCommand matchedCommand = (DynamicItemMenuCommand)sender;
bool isRootItem = (matchedCommand.MatchedCommandId == 0);
matchedCommand.Enabled = true;
matchedCommand.Visible = true;
int indexForDisplay = (isRootItem ? 0 : (matchedCommand.MatchedCommandId - _baselistID));
matchedCommand.Text = "Text for the command";
matchedCommand.MatchedCommandId = 0;
}
}
I had to review a lot of documentation since it was not very clear how the commands can be added on execution time. So I hope this save some time whoever has to implement anything similar.
The missing piece for me was figuring out how to control the addition of new items.
It took me some time to figure out that the matches predicate (the IsValidDynamicItem method in the sample) controls how many items get added - as long as it returns true, the OnBeforeQueryStatusDynamicItem gets invoked and can set the details (Enabled/Visible/Checked/Text etc.) of the match to be added to the menu.

UIImagePickerController Crashing Monotouch

I am trying to write an application, but it is constantly crashing when using the uiimagepickercontroller. I thought that it might be because I was not disposing of the picker after each use, but it will often freeze up on first run as well. Usually I'll take a picture and it just freezes, never asking to "use" the picture.
Do you have any suggestions? Here is my code. Has anyone gotten this to work?
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
myPicker = new UIImagePickerController();
myPicker.Delegate = new myPickerDelegate(this);
myAlbumButton.Clicked += delegate {
if(UIImagePickerController.IsSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary)){
myPicker.SourceType = UIImagePickerControllerSourceType.PhotoLibrary;
myPicker.AllowsEditing = true;
this.PresentModalViewController (myPicker, true);
}else{
Console.WriteLine("cannot get album");
}
};
myCameraButton.Clicked += delegate {
if(UIImagePickerController.IsSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)){
myPicker.SourceType = UIImagePickerControllerSourceType.Camera;
//myPicker.AllowsEditing = true;
this.PresentModalViewController (myPicker, true);
}else{
Console.WriteLine("cannot get camera");
}
};
}
private class myPickerDelegate : UIImagePickerControllerDelegate
{
private TestView _vc;
public myPickerDelegate ( TestView controller):base()
{
_vc = controller;
}
public override void FinishedPickingImage (UIImagePickerController myPicker, UIImage image, NSDictionary editingInfo)
{
// TODO: Implement - see: http://go-mono.com/docs/index.aspx?link=T%3aMonoTouch.Foundation.ModelAttribute
_vc.myImageView.Image = image;
myPicker.DismissModalViewControllerAnimated(true);
}
}
Try to call your event handlers code from the main thread by using BeginInvokeOnMainThread().
So my issue was very similar.
Instead of having a delegate class, I had the delegates inline for the picker.
For some reason the app froze every time after talking the image, not stopping in any breakpoint after that.
The solution that worked for me was to use this book:
http://www.scribd.com/doc/33770921/Professional-iPhone-Programming-with-MonoTouch-and-NET-C

Resources