Ok so in my previous design, on my MainPage which is called TasksGroupPage, i had a "+" icon that was taking me to a NewTasksGroupPage to fill a form and then click the save command to create a new TasksGroup ( so i don't need the + icon function to load the page anymore).
This was working perfectly and after clicking the save button it would take me to the MainPage.
But my problem is that i changed my design, i now removed the + icon and instead i created a new tab which now takes me to the NewTasksGroupPage.
To create this tab i simply added to my TasksGroupPage( the main page) the following code : <views:NewTasksGroupPage></views:NewTasksGroupPage>
The problem now is that :
When i click the save command in my NewTaskPageViewModel , the page doesn't take me to the last page like it did before ALSO it's leaving all the text in the entry box there and it only create my TasksGroupPage after a while.
How should i set up this new design to work properly ? If impossible to do, is there a way to add a GestureRecognizers to one of the tab ?
Thanks.
Here you can see my code right now :
TasksGroupPage.xaml
<TabbedPage>
<ContentPage>
// some xaml
</contentPage>
<views:NewTasksGroupPage></views:NewTasksGroupPage>
</TabbedPage>
TasksGroupPage.xaml.cs this is the function that i don't need anymore but this is what i was using with the + icon
protected async void GoToNewTaskPage(object sender, EventArgs e)
{
await Navigation.PushAsync(new Views.NewTasksGroupPage());
}
NewTaskPageViewModel.cs this is the end of my save command which before took me to the main page ( TasksGroupPage) but now it just stays there and doesn't reset my entries to blank ( even when switching between other tabbed pages)
async Task SaveNewTask()
{
//some code
await Application.Current.MainPage.DisplayAlert("Save", "La tâche a été enregistrée", "OK");
await Application.Current.MainPage.Navigation.PopAsync();
NotifyPropertyChanged();
}
If you read the document Xamarin.Forms Navigation:
You will find that NavigationPage and TabbedPage are two different ways to navigate pages in Xamarin.forms.
Navigation.PushAsync and Navigation.PopAsync only works in NavigationPage, it does not work in TabbedPage, that's why the page doesn't take me to the last page like it did before.
If you use a TabbedPage, you can use below code to switch page:
async void SaveNewTask(object sender, EventArgs args)
{
//this here means the current ContentPage
//this.Parent = NavigationPage
var tabbedPage = this.Parent.Parent as TabbedPage;
//if you don't have navigationPage, just use:
//var tabbedPage = this.Parent as TabbedPage;
//Switch to first page in tabs
tabbedPage.CurrentPage = tabbedPage.Children[0];
}
Related
I'm building my first full-scale Xamarin.Forms app and trying to figure out how to keep user input between navigation. After doing some searching online I've read that the default behavior is to completely reload pages each time you navigate, but you can change the default behavior by setting the NavigationCacheMode to true or required, but I've tried to set this attribute in both xaml and C# with no success - it seems like the property is not recognized.
Is there a simple way to make it so that user input does not disappear when navigating between pages? If anyone can show me how to set the NavigationCacheMode that would be great, but I'm also open to any reasonable solution that will keep the user input from disappearing during navigation.
Additional details: My app has a UWP and Android project. I am using a master detail page for navigation. Here is my MenuList_ItemSelected event handler:
private void MenuList_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
var item = (MenuItem)e.SelectedItem;
var title = item.Title;
var page = (Page)Activator.CreateInstance(item.TargetType);
Detail = new NavigationPage(page); //TODO: when menu item is clicked and you're already on that page, the menu should just slide back. (currently it does nothing and stays out).
IsPresented = false;
}
Finally was able to solve this! I adapted this code from a related post which implements a Dictionary that keeps track of the navigation stack:
In the constructor for my Master Detail Page:
public partial class MenuPage : MasterDetailPage
{
Dictionary<Type, Page> menuCache = new Dictionary<Type, Page>();
}
Then in the ItemSelected method:
private void MenuList_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
if (menuCache.Count == 0)
menuCache.Add(typeof(AttendancePage), Detail);
var item = (MenuItem)e.SelectedItem;
if (item != null)
{
if (menuCache.ContainsKey(item.TargetType))
{ Detail = menuCache[item.TargetType]; }
else
{
Detail = new NavigationPage((Page)Activator.CreateInstance(item.TargetType));
menuCache.Add(item.TargetType, Detail);
}
menuList.SelectedItem = null; //solves issue with nav drawer not hiding when same item is selected twice
IsPresented = false;
}
}
I want to hide Nav bar on the main-start page of my app. but want to make it display with a title and back button on the second page. This is something I did, but it appears on the main page too.
In App.xaml.cs, I used this:
MainPage = new NavigationPage(new MainPage());
In corresponding login page, I created this:
private async void LoginPage_Clicked(object sender, EventArgs e)
{
await Navigation.PushAsync(new LoginPage());
}
When the user will click it'll take to the second page(login page).
Also, how can I change the color of the nav bar?
In the constructor of the page you want to hide the navigation bar, use the following code.
Xamarin.Forms.NavigationPage.SetHasNavigationBar(this, false);
To change the color of the NavigationBar, you can do this.
new NavigationPage(new MainPage())
{
BarBackgroundColor = Color.FromHex("#000000"),
BarTextColor = Color.White
};
As a bonus, you can also remove it via xaml:
<ContentPage ...namespaces... NavigationPage.HasNavigationBar="False">...
In App.cs use
MainPage = new YourProjectNamespace.MainPage();
This will hide your navigation bar on your first page
In my Xamarin.Forms Prism app, I have a MasterDetailPage to handle navigation. The MasterBehavior is set to Popover.
<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:views="clr-namespace:My.Mobile.Application.Views;assembly=My.Mobile.Application"
x:Class="My.Mobile.Application.Frame.MainPage"
MasterBehavior="Popover">
<MasterDetailPage.Master>
<views:MasterPage x:Name="masterPage" />
</MasterDetailPage.Master>
</MasterDetailPage>
When the master is visible, if I select the menu item that is already being displayed as the detail item, the master does not automatically dismiss; I have to swipe the master closed if I want to stay on the same page.
I tried manually registering to the ItemSelected event on my master's list view to be able to explicitly set IsPresented = false on the MasterDetailPage, but the event does not fire when selecting the item that is already being displayed:
public partial class MainPage : MasterDetailPage
{
public MainPage()
{
InitializeComponent();
masterPage.ListView.ItemSelected += OnItemSelected;
}
private void OnItemSelected(object sender, SelectedItemChangedEventArgs e)
{
IsPresented = false;
}
}
This differs from the behavior of the Xamarin.Forms sample Master-Detail app, which closes the master when a selection is made (even if it is the same item that is already being displayed).
Is this something that is being controlled by Prism?
I just started creating a simple application in Xamarin C#.
I have two views : first view have a "login" button and a second view have a webView inside.
I have created the modal segue which connect those two views and my question is :
How can I get if second view is showing it's content and it's webView can load this url : "http://vk.com" ?
Well, at first, you need to add two views.
On the first view place a button. Connect this button to a second view(create a modal segue)
On the second view you can place a label or something different, it doesn't matter.
Zoom out your storyboard(in Xcode storyboard editor, "-" icon), select and set the second view class, for example, "view_two". Save and return to Xamarin Studio. Restart Xcode. Now, in Xamarin Studio select Your class file(in this case - view_two.cs) and open it. Then, insert this code in the "center" of the document :
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
this.webView_login.LoadRequest (new NSUrlRequest (new NSUrl ("http://vk.com")));
}
In the View's ViewDidLoad method:
string url = "http://vk.com";
webView.LoadRequest(new NSUrlRequest(new NSUrl(url)));
See also: http://docs.xamarin.com/recipes/ios/content_controls/web_view/load_a_web_page/
This below piece will will help.
UIWebView webView = new UIWebView(View.Bounds);
View.AddSubview(webView);
var documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
var yourWonFolderPath = Path.Combine(documents, "YourWonFolder");
var localDocUrl = Path.Combine(yourWonFolderPath, TicketInfo.TicketNumber + ".pdf");
webView.LoadRequest(new NSUrlRequest(new NSUrl(localDocUrl, false)));
webView.ScalesPageToFit = true;
I have 3 separate pages MainPage.xaml, Content.xaml and Nav.xaml. I need to load a Content.xaml in 'ucMainContent' userControl that sits inside MainPage.xaml by clicking a button from Nav.xaml. I am wondering what I am doing wrong. Any advice is highly appreciated.
MainPage.xaml where I set content container with this code:
<UserControl x:Name="ucMainContent" />
I am trying to load Content.xaml into mainPage.xaml from Nav.xaml. The code from Nav.xaml page where is the button:
private void LoadContent_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
var contentPage = new Content();
var ucMainPage = new MainPage();
ucMainPage.ucMainContent.Content = contentPage;
}
If I understand you correctly you need to find the parent Control of Nav.xaml in this case Main.
Check out http://forums.silverlight.net/forums/t/55369.aspx - there is an excellent generic utility which allows you to find the parent of any usercontrol