Hello all great people in here
I am heading for creation of a Xamarin.Forms app.
I have taken a look on this link https://developer.xamarin.com/guides/xamarin-forms/custom-renderer/entry/, where they are going to make the renderer for components platform-specific.
An example can be TwinTechs Controls, which is only available for iOS and Android. How can this be done?
Is that possible? And if it, how?
The Renderer class is the custom platform-specific that you need to customize per platform.
In that particular example they are looking at changing an existing renderer.
You will be more focused in writing your own platform-specific components from scratch.
To do so you will want to inherit from ViewRenderer and supply reference to the View and also the platform-specific native element that will be the root of the control on the platform level.
Don't forget to export the renderer also, for it to be used in the rendering process of the page in Xamarin.Forms, from the platform level along the lines:-
[assembly: ExportRendererAttribute(typeof({ViewNameHere}), typeof({RendererClassNameHere}))]
There is a useful tutorial on writing custom renderers here that will help you further.
Related
I have Xamarin Cross Platform Application in Visual Studio 2022. I have a Main Page where i want 2 buttons.
"Take me to Android Page" Button
"Take me to iOS Page" Button
I want to create a sample NATIVE page specifically in Android Project of this Cross Platform Application and one sample NATIVE page specifically in iOS Project within this Xamarin Cross Platform Application.
When I will click on "Take me to Android Page Button", it should take me to Natively designed Page of Android and when I will click on "Take me to iOS Page Button" then it should take me to Natively designed page in iOS Project.
I hope anyone can give me any reference or sample codes to achieve this goal.
Thank You
Xamarin.Forms is designed to prevent the need to design each page for both platforms by using XAML to define shared layouts rendered at runtime using native controls. If you can expand your question to include reasons why you need to define fully native page layouts for each platform, that will help better inform answerers.
If you need the ability to create custom controls for each platform, consider custom renderers which will allow you to define platform-specific behavior for individual views in your XAML pages.
When I will click on "Take me to Android Page Button", it should take
me to Natively designed Page of Android and when I will click on "Take
me to iOS Page Button" then it should take me to Natively designed
page in iOS Project.
You can first find out what Xamarin Forms is and how it works.
From document What is Xamarin.Forms?,we know:
Xamarin.Forms is an open-source UI framework. Xamarin.Forms allows
developers to build Xamarin.Android, Xamarin.iOS, and Windows
applications from a single shared codebase.
Xamarin.Forms allows developers to create user interfaces in XAML with
code-behind in C#. These interfaces are rendered as performant native
controls on each platform.
And part Who Xamarin.Forms is for mentions that :
Xamarin.Forms is for developers with the following goals:
Share UI layout and design across platforms
Share code, test and business logic across platforms.
Write cross-platform apps in C# with Visual Studio.
So, the requirement you mentioned above is not invalid.
In addition,if you want to use native controls of the target platform in xamarin forms, you can use Xamarin.Forms Custom Renderers to achieve this.
Xamarin.Forms user interfaces are rendered using the native controls
of the target platform, allowing Xamarin.Forms applications to retain
the appropriate look and feel for each platform. Custom Renderers let
developers override this process to customize the appearance and
behavior of Xamarin.Forms controls on each platform.
Note:
We have Payment Gateway SDK for Android specific only that's why we
need to use Native forms. Can you suggest or give me some hint how we
can use them ?
Xamarin.Android offers two ways to use these libraries:
1. Create a Bindings Library that automatically wraps the library with
C# wrappers so you can invoke Java code via C# calls.
2. Use the Java Native Interface (JNI) to invoke calls in Java library
code directly. JNI is a programming framework that enables Java code
to call and be called by native applications or libraries.
For more details, check: https://learn.microsoft.com/en-us/xamarin/android/platform/binding-java-library/ .
I will make an app where I need the native map. Either I do the app with Xamarin native and Embedding Xamarin.Forms in Xamarin Native on all pages without the page with the map. Or if it's possible I'll do an Xamarin Forms app and embedding the native map control, if it's possible? I read Adding Platform-Specific Controls to a Xamarin.Forms Layout but is the map included? Or is it only "small" controls as the label?
What should I use?
Well Native Embedding is hard to implement ( you basically need to use the shared project instead of PCL), therefore for many it is easier to user CustomRenderers.
Xamarin.Forms Embedding in the otherhand, is still preview looks like easy to use , but to be honest I have no idea what advantages it gives us. Like if we are making a native app why do we need to have a xamarin.forms loaded to our projects and not only it will make our app bigger but it will make it harder to follow.
The way I see it from your question is to use the maps given by Xamarin.Forms here and you can access it's custom renderer if it is necessary. Or use TKCustomMap which they have already extended the map
I was helping in a project when developer did the follow and it seems worked for him. He did Xamarin Forms, created
public class CustomMap : Map
where Map is Xamarin.Forms.Maps.Map. Then created custom renderers
[assembly: ExportRenderer(typeof(CustomMap), typeof(CustomMapRenderer))]
namespace CustomRenderer.iOS
{
public class CustomMapRenderer : MapRenderer
and
[assembly:ExportRenderer (typeof(CustomMap), typeof(CustomMapRenderer))]
namespace CustomRenderer.Droid
{
public class CustomMapRenderer : MapRenderer, GoogleMap.IInfoWindowAdapter, IOnMapReadyCallback
Then you can do a lot of things with CustomPins, InfoWindow, etc
You can embed any native control in a xamarin forms app. This is a feature already released while the xamarin.forms embedding into a native app is still in preview.
I would either embed the native map as you suggested or do a custom renderer for the map control already available in forms.
I am new to Xamarin, and am setting up a Xamarin Forms app, and triggering the device's camera after creating the intent and starting an activity for it.
Pretty much what Xamarin's recipe looks like, but I am using Forms.Context as the Activity object (in order to place all the hookings inside my content page, as opposed to having everything within MainActivity.cs, as in Xamarin's recipe).
It works, the camera is opened.
Intent intent = new Intent(MediaStore.ActionImageCapture);
_file = new File(_dir, String.Format("myPhoto_{0}.jpg", Guid.NewGuid()));
intent.PutExtra(MediaStore.ExtraOutput, Uri.FromFile(_file));
Forms.Context.StartActivity(intent, Bundle.Empty);
The thing is I want the same as the recipe's feature of putting the picture just taken into an ImageView.
The recipe's code generates the UI (and everything else) within MainActivity.cs. I wanted to keep the view declared separately, in its XAML file.
As far as I understand, I need to be able to embed a axml android resource file into the xaml, because code in MainActivity is already Android-ported, and Android can reference views/controls only by their resource id (which are automatically generated after creating the view/ui container as an axml file under Resources/layout folder.
How do I complete this last step in order to have the Take a Picture sample working - coded outside the MainActivity?
Screenshots:
Here's how the Xamarin recipe puts the picture taken into the image viewer.
if you are working with Xamarin Forms and want to take a picture and then display it with an Image - I suggest you use https://github.com/jamesmontemagno/MediaPlugin which allows to let the user take a photo (or pick one from the phone). The result can be set as the ImageSource of an Image.
About the original question, whether you can embedd a native android element into xamarin Forms - you can check out https://developer.xamarin.com/guides/xamarin-forms/user-interface/layouts/add-platform-controls/
I think the right way to deal this is to use DependencyService to access Android native feature, to be specific here, to call the native method of taking a picture and save the picture. If you just want take a picture and display it with Image, you can use DependencyService to implement this platform specific feature and design your layout in xaml, there is no need to embed axml in XAML.
If you want to embed the camera preview in Xaml, the suggested way is to use Custom Renderers to render the interface using the native controls of target platform. It means, you don't need to create a axml in Android project to display camera preview, you can create a custom renderer (control/ view) in PCL, and implement this control in your Android project. There is official doc and demo which guides us to implement a view to display the camera preview.
I apologize in advance for my question, but I start with Xamarin and need help to create a particular architecture.
I should use Xamarin forms with MVVM Light , but with a large part of native pages (UWP).
For this, I wondered if I had to place my View-Models in the portable class or in the UWP Project ? And if in the portable class, what navigation use to navigate between my native pages ?
The Logic should be placed inside the shared code so put your ViewModels inside the portable or shared project.
For navigation between pages you could use:
conditional compilation
if mobile
navigation droid/iOS
else
navigation UWP
(Use with underscores, entering here is using formatting, see link
Condotional Compilation for more details)
DependencyService as described here DependencyService
I'm sure there are other ways but I think DependencyService is the most elegant, as those #if#else stuff makes the code confusing and unreadable.
I see there is an iOS and iPad, PIN Code Control xamarin component, that will give your a programmatic numeric keypad for PIN Entry. Short of coding the entire thing using C# in Xamarin I would prefer to find a component.
I need a PIN code control, xamarin component that is cross platform, which is the purpose of xamarin forms. We will be using this on Android and iOS devices.
Has anyone see such a component on Xamarin's Component Store and I am using the wrong search terms, PIN, Keypad, Numeric?
For future googlers: you might be interested in a small Xamarin.Forms library XamarinFormsPinView. I'm the author.
It's not very configurable, but should fill most common requirements.
Sorry, I have not seen a component that does what you need.
This Xamarin.Forms calculator sample might be a useful starting point for writing a custom one (since it already implements a numeric keyboard entry with display and backspace).
Obviously there's a lot of styling work required to get it looking like any of the native PIN entry screens - but it should save you writing from scratch.
If you need attractive xamarin forms pin passcode form with high control, check out this repo. It is not a library and it does not use MVVM models. It is just .XAML page with code behind it.
https://github.com/Akezh/XamarinFormsPinPassword