Xamarin Forms slow navigating - c#

I have a search page that have a Button to navigate to another page . When I click on that Button it takes about 4 seconds to navigate .
SearchPage Command :
Device.BeginInvokeOnMainThread(async () =>
{
await (Application.Current.MainPage as NavigationPage).PushAsync(new OtherPage());
});
OtherPage Code Behind :
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class OtherPage : ContentPage
{
SearchStateViewModel Model;
public SearchEstatePage()
{
InitializeComponent();
Model = new SearchStateViewModel();
Layout.BindingContext = Model;
}
protected override void OnAppearing()
{
base.OnAppearing();
Model.GetCities();
}
}
What is the problem ?

As #MichaelRandall advice I have checked OtherPage's XAML and finde that the Syncfusion's components in XAML slows down the navigation .
But what what is the replacement ?

Related

How could I close my sidebar without closing my app in C# xamarin forms android?

I'm working with C# xamarin forms android and I'm trying to close my sidebar when the user touches de backbuttom but I'm not able to do it. Instead, my aplications closeses.
HomePage.xaml.cs code:
public partial class HomePage : ContentPage
{
string[] subs;
public HomePage()
{
//NavigationPage.SetHasBackButton(this, false);
InitializeComponent();
}
protected override bool OnBackButtonPressed()
{
return true;
}
}
my sidebar AppShell.xaml.cs code:
public partial class AppShell : Xamarin.Forms.Shell
{
public AppShell()
{
InitializeComponent();
nameuser.Title = Preferences.Get("displayName", "Default");
versionadoAPP.Text = "v" + $"{VersionTracking.CurrentBuild}.{VersionTracking.CurrentVersion}";
}
protected override bool OnBackButtonPressed()
{
return true;
}
}
Image of my app:
How could I resolve that?
Thank tou very much!
The easiest way that do this is to set the flyout presented property.
Try something like this:
Shell.Current.FlyoutIsPresented = false;
Good luck!

How to remove huge title bar from Xamarin.Forms WPF application?

I've got a Xamarin.Forms WPF application running, but there is a huge title bar.
I have tried the solution of adding NavigationPage.SetHasNavigationBar(this, false); inside my MainPage class's constructor
Here is the code:
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
NavigationPage.SetHasNavigationBar(this, false);
}
}
Still I am getting huge title bar size in WPF application, on the other hand, while running on UWP, it is working fine.
You could try to add below codes in your MainWindow.xaml.cs in wpf project:
protected override void OnActivated(EventArgs e)
{
base.OnActivated(e);
if (!topBarsRemoved) RemoveTopBars();
}
private bool topBarsRemoved = false;
private void RemoveTopBars()
{
var topAppBar = this.Template.FindName("PART_TopAppBar", this) as FormsAppBar;
if (topAppBar != null)
(topAppBar.Parent as System.Windows.Controls.Grid)?.Children.Remove(topAppBar);
topBarsRemoved = true;
}

Not navigating from one page to another page in Xamarin.Forms while using using Xamarin.Essentials shake detect event

I have two class MainPage.xaml and Home.xaml. I have written Home.xaml using Xamarin.Essentials shake detect code and want to move from Home to MainPage.xaml but it is not working.
Home.xaml.cs
namespace StarRatingApp
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Home : ContentPage
{
public Home()
{
InitializeComponent();
}
protected override void OnAppearing()
{
base.OnAppearing();
Accelerometer.ShakeDetected += Accelerometer_ShakeDetected;
Accelerometer.Start(SensorSpeed.Game);
}
private void Accelerometer_ShakeDetected(object sender, EventArgs e)
{
MainThread.BeginInvokeOnMainThread(() =>
{
new NavigationPage(new MainPage());
});
}
}
What am I doing wrong in Accelerometer_ShakeDetected method ??
You are not navigating in the method. You just create a new NavigationPage. And this won't do anything. You have to tell the system, where you want to navigate and how.
You have two options:
1) Navigate to your page (this adds the page to the Navigation-Stack)
await Navigation.PushAsync(new MainPage());
or 2) Overwrite your mainpage with the new NavigationPage (this removes the whole NavigationStack and starts a "new" navigation)
Application.Current.MainPage = new NavigationPage(new MainPage());
You can find more infos about navigation in the documentation >> https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/navigation/hierarchical

How to navigate from one screen to another automatically in Xamarin cross platform app?

I am not able to find out how to navigate from one screen to another automatically without having to click on any button or perform any action. I want to start another screen just after my splash screen shows up. As i am new on this tool or technology. Please help. Thank you in advance.
My code is:
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
protected async override void OnAppearing()
{
base.OnAppearing();
await Task.Delay(5000); // Simulate a bit of startup work.
await this.Navigation.PushAsync(new HomePage());
}
}
Working and tested code:
protected override void OnAppearing()
{
base.OnAppearing();
GoToMaster();
}
private async void GoToMaster()
{
await Task.Delay(5000);
await this.Navigation.PushAsync(new SignUpPage());
}
I made my splash activity under xamarin.droid file and i want it to display the xaml file that i made in portable portion before launching the home page activity.
You can't skip the MainActivity as it is the container of your pages in Portable lib, So if you want to redirect to your xaml page after the splash screen, you can make SplashScreenActivity navigate to your MainActivity and make your xaml file the MainPage of App.cs:
public partial class App : Application
{
public App()
{
InitializeComponent();
MainPage = new YourPage();
}
....
}
And in your SplashScreenActivity you need to navigate to the MainActivity:
[Activity(Theme = "#style/MyTheme.Splash", MainLauncher = true, NoHistory = true)]
public class SplashActivity : AppCompatActivity
{
static readonly string TAG = "X:" + typeof(SplashActivity).Name;
public override void OnCreate(Bundle savedInstanceState, PersistableBundle persistentState)
{
base.OnCreate(savedInstanceState, persistentState);
}
protected override void OnResume()
{
base.OnResume();
Task startupWork = new Task(() => { SimulateStartup(); });
startupWork.Start();
}
async void SimulateStartup()
{
await Task.Delay(8000);
StartActivity(new Intent(Application.Context, typeof(MainActivity)));
}
}
here is the complete demo:SplashScreenDemo.

OnAppearing in children of TabbedPage is triggered only once

I have a TabbedPage in Xamarin.Forms:
public partial class MainPage : TabbedPage
{
public MainPage()
{
InitializeComponent();
var playPage = new NavigationPage(new PlayPage())
{
Title = "Play",
Icon = "play1.png"
};
var settingsPage = new NavigationPage(new SettingsPage())
{
Title = "Settings",
Icon = "settings.png"
};
var aboutPage = new NavigationPage(new AboutPage())
{
Title = "About",
Icon = "about.png"
};
Children.Add(playPage);
Children.Add(settingsPage);
Children.Add(aboutPage);
}
Each of the child pages are a ContentPage that overrides the OnAppearing method. The content of my child pages are not updating properly when I navigate between the tabs and further debugging tells me that the OnAppearing method for child pages are only called once (when the MainPage is first loaded).
Anyone have any idea why the OnAppearing method is only called once? And how can I solve this issue?
More Info
My child pages such SettingsPage contains events that when fired open another ContentPage using Navigation.PushAsync method. I expected the OnAppearing method to also get called when switching from tab pages and the navigation pages within those tab pages. (Hope this makes sense)
It only triggers once since the View is not destroyed when you navigate between tabs.
You can use the CurrentPageChanged event to figure our that a page has changed and signal the view it changes to and do whatever update of the view you need.
this.CurrentPageChanged += PageChanged;
void PageChanged(object sender, EventArgs args)
{
var currentPage = CurrentPage as MyTabPage;
currentPage?.UpdateView();
}
Then in your page:
public void UpdateView()
{
// do whatever you need here to update the page
}
Do not embedded your ContentPages within a "single" level NavigationPage.
The following works fine in terms of the OnAppearing event firing when switching between tabs:
public class PlayPage : ContentPage
{
protected override void OnAppearing()
{
System.Diagnostics.Debug.WriteLine("PlayPage");
base.OnAppearing();
}
}
public class AboutPage : ContentPage
{
protected override void OnAppearing()
{
System.Diagnostics.Debug.WriteLine("AboutPage");
base.OnAppearing();
}
}
public class SettingsPage : ContentPage
{
protected override void OnAppearing()
{
System.Diagnostics.Debug.WriteLine("SettingPage");
base.OnAppearing();
}
}
public partial class MainPage : TabbedPage
{
public MainPage()
{
var playPage = new PlayPage() { Title = "Play" };
var settingsPage = new SettingsPage() { Title = "Settings" };
var aboutPage = new AboutPage() { Title = "About" };
Children.Add(playPage);
Children.Add(settingsPage);
Children.Add(aboutPage);
}
}
public class App : Application
{
public App()
{
this.MainPage = new MainPage();
}
}

Categories