How can i use the Navigation Page for Android in Xamarin Form ?
I use this but it didn't work:
I want, when i press my button, to switch to my secondPage (DemoPage)
I don't know how i can do this.
Thank you
simply wrap your "main" page in a NavigationPage when you assign it in your App's constructor
public class App : Application
{
public App ()
{
// The root page of your application
MainPage = new NavigationPage(new MyMainPage());
}
}
Related
I have a project using MAUI Hybrid to produce an Android Application (apk).
A blazorWebView is running in MAUI app, so that the code of pages is used by both Web Site (un Blazor Wasm) and MAUI app (MAUI + Blazor WebView).
A blazorWebView is included in MAUI App (code from MainPage.xaml) :
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:b="clr-namespace:Microsoft.AspNetCore.Components.WebView.Maui;assembly=Microsoft.AspNetCore.Components.WebView.Maui"
xmlns:local="clr-namespace:MyMauiBlazor"
x:Class="MyMauiBlazor.MainPage">
<b:BlazorWebView HostPage="wwwroot/index.html">
<b:BlazorWebView.RootComponents>
<b:RootComponent Selector="app" ComponentType="{x:Type local:Main}" />
</b:BlazorWebView.RootComponents>
</b:BlazorWebView>
</ContentPage>
On Android devices, is it possible to handle the Back Button pressed ?
Currently, the App closes, but I want to handle this event (ex : navigate to previous page).
Is there a way to do this ?
I had the same problem as you, officially microsoft doesn't support back key navigation for MAUI blazor yet, but I found a temporary solution for that:
First you have to create a javascript file, and put it in the wwwroot folder, and include it in the index.html file. Inside the js file put this code:
window.goBack = () => {
history.go(-1);
}
Then you have to create a c# class and make it a service, so for example I created a class called GoBack.cs. To set it as a service just put this code in MauiProgram.cs:
builder.Services.AddTransient<GoBack>();
In the GoBack class you have to create a static variable and method, because otherwise you will not be able to use the IJSRuntime interop. My GoBack file is the following:
public class GoBack
{
private static IJSRuntime JSRuntime { get; set; }
public GoBack(IJSRuntime jSRuntime)
{
GoBack.JSRuntime = jSRuntime;
}
public static async Task GoBackInTime()
{
//Microsoft.Maui.Platform;
if (GoBack.JSRuntime != null)
{
await GoBack.JSRuntime.InvokeVoidAsync("goBack");
}
}
}
The method GoBackInTime is going to call the js method goBack through the jsInteroperability. Now you have to inject in the first blazor page that you load this service.
#inject GoBack goBack
This way the jsRuntime in the cs file will be initialized and not null.
Now all you have to do is override the OnBackButtonPressed method in MainPage.xaml.cs:
protected override bool OnBackButtonPressed()
{
GoBack.GoBackInTime();
return true;
}
This way, when the back button is pressed, it won't exit the application (because we are returning true) and at the same time the js function is called so the blazor page will navigate to the previous page.
I want to navigate between pages be starting and finishing new activity for any new page and show this page in own activity. How to achieve this behavior?
The answer is Yes,you could look at Native Forms.
For ios:
in ios project AppDelegate
public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
{
Forms.Init();
...
UIViewController mainPage = new NotesPage().CreateViewController();
mainPage.Title = "Notes";
_navigation = new AppNavigationController(mainPage);
_window.RootViewController = _navigation;
_window.MakeKeyAndVisible();
return true;
}
For Android:
in Android project your activity ,it convert your page to a fragment,and then you could fill it in your activity:
Android.Support.V4.App.Fragment mainPage = new NotesPage().CreateSupportFragment(this);
SupportFragmentManager
.BeginTransaction()
.Replace(Resource.Id.fragment_frame_layout, mainPage)
.Commit();
On Android Xamarin.Forms navigate among pages by using Fragments.
If you don't like it you may fork Xamarin.Forms and change the code to work the way you want it to work: https://github.com/xamarin/Xamarin.Forms
This will make some packages incompatible with your fork of Xamarin.Forms, which you may need to take under consideration if you intend to use them.
Also if you consider the required time and knowledge this is non-trivial. It is highly unlikely that someone will do that job instead of you and provide you with the plug-in solution.
I am a newbie in xamarin forms app development, currently, I am facing an issue in overriding the toolbar back button onclick. In ios, I am able to achieve but in android its not working can anyone help me out on how to achieve this in my project.
By default it works on iOS and on Android physical back button only. If you want to also support the navigation bar button, you need to use custom platform logic. Take a look on this blog post: Let’s Override Navigation Bar back button click in Xamarin For. He creates a common content page with custom action for back button:
public class CoolContentPage : ContentPage
{
/// <summary>
/// Gets or Sets the Back button click overriden custom action
/// </summary>
public Action CustomBackButtonAction { get; set; }
public static readonly BindableProperty EnableBackButtonOverrideProperty =
BindableProperty.Create(
nameof(EnableBackButtonOverride),
typeof(bool),
typeof(CoolContentPage),
false);
/// <summary>
/// Gets or Sets Custom Back button overriding state
/// </summary>
public bool EnableBackButtonOverride
{
get
{
return (bool)GetValue(EnableBackButtonOverrideProperty);
}
set
{
SetValue(EnableBackButtonOverrideProperty, value);
}
}
}
And then he calls CustomBackAction inside OnOptionsItemSelected method in Anroid code.
Best way to intercept Back Navigation ( Navigation in general ) would be by adding your NavigationPageRenderer so you can control the events and cancel or redirect them, look at my answer How to intercept Navigation Bar Back Button Clicked in Xamarin Forms?
I came to this post with same question about Xamarin forms navigation back button and later discovered that since Xamarin.Forms Shell this is done easily by overriding the OnNavigating method in the AppShell.xaml.cs file as I have done here:
protected override void OnNavigating(ShellNavigatingEventArgs e)
{
// Make sure it's safe to examine the current page
if ((Shell.Current != null) &&
(Shell.Current.CurrentPage != null))
{
Console.WriteLine($"{e.Source} {Shell.Current.Title}");
if (
// Detect Back Navigation
e.Source == ShellNavigationSource.Pop &&
// Cancel or Not, based on (for example) the Title of the current page.
(Shell.Current.Title != "My Main Page"))
{
e.Cancel();
Shell.Current.GoToAsync("..");
}
}
base.OnNavigating(e);
}
In case anyone else stumbles upon, I put a sample on GitHub of what worked for me for Android and iOS both.
I noticed the menu on the bottom side of Google Chrome's Android web browser application. It is used for setting language for translation of pages. Can you help me identify what is this control's name?
I'm currently working on Xamarin.Forms project and would like to implement similar menu on the bottom side of my screen for all Android devices. Is that possible in Xamarin.Forms and how would the implementation look like?
I'm currently working on Xamarin.Forms project and would like to implement similar menu on the bottom side of my screen for all Android devices. Is that possible in Xamarin.Forms and how would the implementation look like?
There is no direct control in xamarin.forms that can serve this purpose. So you will need your own Custom View for this:
In PCL, create a custom control class(MyTabLayout):
public class MyTabLayout:View
{
}
In PCL page, using this control in Xaml:
<StackLayout>
<!--EndAndExpand will bottom the tab control-->
<local:MyTabLayout VerticalOptions="EndAndExpand"></local:MyTabLayout>
</StackLayout>
In YourProject.Android project Create your custom renderer:
[assembly:ExportRenderer(typeof(CustomTabsLayout.MyTabLayout),
typeof(MyTabLayoutRenderer))]
namespace CustomTabsLayout.Droid
{
public class MyTabLayoutRenderer:ViewRenderer<MyTabLayout,TabLayout>
{
Context baseContext;
public MyTabLayoutRenderer(Context context) : base(context)
{
baseContext = context;
}
protected override void OnElementChanged(ElementChangedEventArgs<MyTabLayout> e)
{
base.OnElementChanged(e);
//Below are test tabs, you can customize your tabs' styles here.
TabLayout tab = new TabLayout(baseContext);
tab.AddTab(tab.NewTab().SetText("tab1"));
tab.AddTab(tab.NewTab().SetText("tab2"));
tab.AddTab(tab.NewTab().SetText("tab3"));
tab.AddTab(tab.NewTab().SetText("tab4"));
tab.AddTab(tab.NewTab().SetText("tab5"));
SetNativeControl(tab);
}
}
}
Here is the result:
I have made simple navigation pages.
Now I want to add icon for android in navigation bar.
I added an screenshot and highlighted with black circle where i want to add an icon.
I also tried custom renderer.
That is provided answer from this post: Change icon on Navigation bar - Xamarin.Forms Android
But not worked for me.
Here it is
[assembly: ExportRenderer(typeof(NavigationPage), typeof(CustomMapRenderer))]
namespace XamarinFormsMaps.Droid
{
public class CustomMapRenderer : NavigationPageRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<NavigationPage> e)
{
base.OnElementChanged(e);
var bar = (Android.Support.V7.Widget.Toolbar)typeof(NavigationPageRenderer)
.GetField("_toolbar", BindingFlags.NonPublic | BindingFlags.Instance)
.GetValue(this);
bar.SetLogo(Resource.Drawable.icon);
}
}
}
Try this one!
I personally haven't used it but I guess you are looking for this:
NavigationPage.SetTitleIcon (this, "image.png");
This will set the title Icon in Xamarin Forms.
If you want to change the back button Icon, hamalaiv gave you this link
Thank you Dilmah.
It work for me.
With Xamarin.Forms 2.5, I change the constructor by
public CustomMapRender(Context context) : base(context)
to avoid warning of obsolete constructor.