Xamarin.Forms Prism - Master not dismissing when selecting master page item - c#

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?

Related

PopAsync not working when i use it in a TabbedPage ViewModels

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];
}

Hide Nav Bar on Main Page - Xamarin Forms

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

creating textbox when button click in WPF?

i already searched few stackoverflow threads related thisbut i have problem in code plz review my code and tell me where is the problem i am stuck and do not where is the problem i am new here please help me out
public partial class MainWindow : Window
{
Grid MainGrid = new Grid();
public MainWindow()
{
InitializeComponent();
}
private void button_Click(object sender, RoutedEventArgs e)
{
TextBox dynamicTextBox = new TextBox();
dynamicTextBox.Text = "Type Partnumber";
Grid.SetRow(dynamicTextBox, 1);
Grid.SetColumn(dynamicTextBox, 0);
this.MainGrid.Children.Add(dynamicTextBox);
}
}
By using the button_Click event you are adding the dynamic TextBox to the MainGrid and that code looks fine. But the problem is that this.MainGrid is not in the Present UI, its similar to the dynamically created TextBox, since you ware defined it in the code-behind(see the definition above the constructor Grid MainGrid = new Grid();).
Consider that canContainer is a canvas defined in the xaml as like the following,
<Canvas Height="319" Margin="0"
Width="517"
Name="canContainer"/>
To overcome this you can choose any of the following method.
1.Add The MainGrid to the UI. it will add the grid to the canvas, keep in mind the dynamic textBox already added to the Grid.
which means the code should be like this -->
this.canContainer.Children.Add(MainGrid);
2.Add dynamicTextBox to any other parent which is present in the UI.
this.canContainer.Children.Add(dynamicTextBox);
You you are using this method then need not to define and adding MainGrid

How do you make a popup menu on a ListView item?

How do you make a menu popup appear when you click a listview item, and have the menu popup under the item that you clicked?
Is it possible to do this using the ListView.ItemClick event to do this?
This question is for api 22+.
There have been many answers for this question but all that I have found are on old APIs, so this is how I did it.
It is possible to do it using the ListView.ItemClick event, and achieve a menu as shown.
(Names deleted)
[Activity(Label = "Employee Management", Theme = "#android:style/Theme.Material")]
public class EmpMgmtActivity : Activity
{
ListView empListView;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.EmpMgmtLayout);
empListView = FindViewById<ListView>(Resource.Id.EmpMgmtList);
GenerateEmpList(EmployeeStorage.employeeList);
empListView.ItemClick += EmpListView_ItemClick;
}
private void EmpListView_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
{
var menu = new PopupMenu(this, empListView.GetChildAt(e.Position));
menu.Inflate(Resource.Layout.popup_menu);
menu.MenuItemClick += (s, a) =>
{
switch (a.Item.ItemId)
{
case Resource.Id.pop_button1:
// update stuff
break;
case Resource.Id.pop_button2:
// delete stuff
break;
}
};
menu.Show();
}
Most of this information is fairly easy to find, the part I had trouble with isnt getting the menu to popup but to have it popup on the right line item. The key for me was to lookup the individual view from the list.
var menu = new PopupMenu(this, empListView.GetChildAt(e.Position));
If you use
(View)Sender
Which comes in the even args, it will place the menu near the top of the page, which is not ideal.
Therefore using
ListView.GetChildAt(e.Position)
Which returns the actual view of the list item, you can have the menu popup in the correct location.
XML Code for the popup menu:
<?xml version="1.0" encoding="utf-8" ?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/pop_button1" android:title="Edit Employee" showAsAction="always" />
<item android:id="#+id/pop_button2" android:title="Delete Employee" showAsAction="always" />
</menu>
Hope this helps!

How to load content page into mainPage from another xaml page?

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

Categories