Xamarin Community Toolkit TabView - Navigate within Tabs - xamarin

For the bottom navigation bar, I'm using a Tabbed Page, and inside of a Tabbed page, I'm using a Tabview from the Xamarin Community Toolkit. On each XCT tab, I'm loading a list of items using the collection view (Xamarin Community Toolkit TabView).  I need to click an item in collection view and  move to another page (detail page) within the same tab of the tabbed page. Although I am aware that Shell might be used, I prefer to use the method described above.

You can use the SelectionChangedCommand to bind with the ItemSelectedCommand command and then use Navigation.PushAsync(new DetailPage()); to navigate to the detail page as Jason suggested.
Here's the code snippet below for your reference:
INavigation Navigation => Application.Current.MainPage.Navigation;
public ICommand ItemSelectedCommand
{
get
{
return new Command(async () =>
{
if (SelectedItem == null) return;
if(SelectedItem.Id ==1)
{
await Navigation.PushAsync(new DetailPage());
}
//else if
SelectedItem = null;
});
}
}

Related

Xamarin not updating bindings when page is in background

I have a viewmodel bound to a page. The viewmodel is updating a property reacting to an event. When the page in is in background (covered by other pages) it doesn't update its UI, even when i execute the viewmodel code on UI thread:
Device.BeginInvokeOnMainThread(() =>
{
MyProperty = false;
}
Observing this on Android at the moment.
Solved but my own answer below.
If i add a single line to this, it works, the page UI gets updated in background:
Device.BeginInvokeOnMainThread(async() =>
{
await Task.Delay(10);
MyProperty = false;
}

Is it possible to use modal page in Tabbed pages in Xamarin Forms?

As what my title says, is it possible to use modal page when you click a tab in Xamarin Forms?
What I would like to do it when I click one of my 5 tabs it would open a Modal page. If it's possible can someone please show my some example of how it is done or point me to the right way.
Sure, it is possibile but a bit tricky.
Create a blank page (for example FakePage) and add it to the TabbedPage.Children
In the TabbedPage code behind override OnCurrentPageChanged, check when your fakepage is selected, then open a Modal.
When the modal is opened tell your TabbedPage to go back to the previous tab (so when the user closes the modal it doesnt see a blank page)
Your tabbed page childrens
<views:Page1 Title="First tab" />
<views:Page2 Title="Second Tab" />
<views:FakePage Title="Modal tab" />
Tabbed page code behind
private Page _lastPage;
protected override async void OnCurrentPageChanged()
{
if (CurrentPage is FakePage)
{
await Navigation.PushModalAsync(new YourModalPage());
//Wait a bit for the modal to open, then reselect the last tab
//This is useful when a user close the modal: you dont want to show a blank page
await Task.Delay(150);
CurrentPage = _lastPage;
}
else
{
_lastPage = CurrentPage;
}
base.OnCurrentPageChanged();
}
Going off of what Giampaolo said, the following method might be a bit smoother:
protected override async void OnCurrentPageChanged()
{
if (CurrentPage is FakePage)
{
CurrentPage = _lastPage;
await Navigation.PushModalAsync(new YourModalPage());
}
else
{
_lastPage = CurrentPage;
}
base.OnCurrentPageChanged();
}
That way, you won't ever see the blank fake page during the transition.

Xamarin Forms Prism : Issue with Deeplinking, navigating to another tab and opening a page

In my xamarin forms application,
I have a tabbed page and on a button click event of Tab1 I want to open a page on a different tab say Tab2. For that I'm using the following code:
VIEWMODEL -- Not Working
private async Task NavigatePage()
{
Device.BeginInvokeOnMainThread(async () => {
await _navigationService.NavigateAsync("MainPage?selectedTab=Tab2Page/TargetPage");
});
}//This returns a blank page
Works
private async Task TryNavigating()
{
Device.BeginInvokeOnMainThread(async () =>
{
await _navigationService.NavigateAsync("/NavigationPage/MainPage?selectedTab=Tab2Page/TargetPage");
});
}
NavigatePage() opens the selected tab(Tab2Page) but unable to navigate to TargetPage but instead displays white screen, where as TryNavigating() opens the selected tab and goes to Tab2Page but if you navigate back you will see extra navigation bar on top of selected tab which I don't want to display.
Can anyone please help? Am i not using the navigation service the right way or is there issue with the way I'm navigating?

Show modal page on only a portion of the screen

I am developing an iPad app using Xamarin.Forms.
I would like my settingspage to be modal so it lay over the previous page like a popup.
I have been trying all solutions I could find and most of them seems to recommend me to call this from a button:
await Navigation.PushModalAsync(ModalSettingsPage);
What happens when I use it is that my settingspage comes in from below as a modal page but not as a popup, it covers the entire screen.
This is my current code:
//Setup button (action bar)
ToolbarItems.Add(new ToolbarItem
{
// Text = "Setup",
Icon = "settings1.png",
Order = ToolbarItemOrder.Default,
Command = new Command(() => Navigation.PushModalAsync(new ModalSettingsPage())) //Action to perfome on click , open modal view
});
Also, does anyone now if there is any good way to positions the ToolbarItems? I have two items and would like each one to be positioned at each side, but by default they are both positioned to the right.
With the evolution of Forms (currently 4.5.0), it has now become simple to push a modalpage which is not fullscreen. Use the following in your code-behind of your xaml contentpage:
using Xamarin.Forms;
using Xamarin.Forms.PlatformConfiguration;
using Xamarin.Forms.PlatformConfiguration.iOSSpecific;
namespace YourAppWithAModalPage
{
public class ModalPage : ContentPage
{
public ModalPage()
{
// iOS Solution for a ModalPage (popup) which is not fullscreen
On<iOS>().SetModalPresentationStyle(UIModalPresentationStyle.FormSheet);
}
}
}
There is nothing pre-made in Forms to give you a popup like I think you want. Instead, you would have to create this or use a third-party solution. For example, here is a "popup" implementation that might work for you.

Showing different toolbar buttons on each page with Xamarin Forms

I have 2 pages in my Xamarin Forms app. My first page has 4 icons in the toolbar. My second page is a login page and has a tick and a cross in the toolbar.
I can't get the login page to show any icons unless I make it a navigation page. I also have to clear ToolBarItems on the first page before calling PushAsync() otherwise it complains there are too many toolbar items.
If I call PopAsync() on the login page it does not return to the first page. I'm guessing this is due to their being 2 navigation pages. I also tried PopToRootAsync().The back button works however.
My question is - how do I show different toolbar icons on 2 different pages in a way that allows navigation to work?
I'm testing this on Windows Phone 8.0
Here is the code calling the login page:
private async void ShowLoginPage()
{
ToolbarItems.Clear();
var page = new NavigationPage(new LoginPage());
await Navigation.PushAsync(page);
}
and here is the code to return to the first page:
private void Cancel()
{
Navigation.PopToRootAsync();
}
I'm running Xamarin.Forms v1.2.2.6243
One thing you could try is to keep your Login Page inside of a NavigationPage, and then instead of running PopAsync() within the Login Page after they have logged in successfully, simply replace the MainPage with your old Navigation page:
In your App class:
public NavigationPage AppNavPage = new NavigationPage(new FirstPage());
public App() {
MainPage = AppNavPage;
}
In your FirstPage:
private async void ShowLoginPage() {
ToolbarItems.Clear();
var page = new NavigationPage(new LoginPage());
await Navigation.PushAsync(page);
}
In Login Page:
private async void OnCreateClicked(object sender, EventArgs e) {
bool loginInfoIsGood = CheckLoginInfo(); //Check their login info
if(loginInfoIsGood) {
Application.Current.MainPage = App.AppNavPage;
}
}
Otherwise, I have also done a custom renderer for the NavigationRenderer on iOS to insert toolbar items onto the right side of the Navigation Bar and have overridden some Menu related stuff on Android to change the icon text/colors.
One option that you have, and one that I implemented in my own app, is a custom renderer that removes the navigation header from the app and then you could build your own custom header. With this approach, you do lose some of the native feel of the app, and you have to implement much of the transitional functionality your self. However, it gives you alot more control over the look.
CustomRenderer that removes the navigationBar:
//add using statements
// add all view here that need this custom header, might be able to build a
//base page that others inherit from, so that this will work on all pages.
[assembly: ExportRenderer(typeof(yourView), typeof(HeaderRenderer))]
class HeaderRenderer : PageRenderer
{
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
this.NavigationController.SetNavigationBarHidden(true, true);
}
}
After this you can build a header view that can be placed on the top of every page (I am using xaml) so I don't know if it is relevant in you application.
Edit: You might need to change this renderer for differnt page types.

Resources