How to Manage Tabs in Tabbedpage from ViewModel - xamarin

I'm developing application, where I have to dynamicaly create/delete tabs in TabbedPage.
Let say that I have workout with excercises, each excercise has to have own page. When I'm passing workoutViewModel by contructor I can create tabs in code behind, rest code I have in viewModel. User can add or remove some excercise this cause that tabs have to change.
How can I manage tabs in tabbedPage from ViewModel? This ViewModel is BindingContext in this page.

How can I manage tabs in tabbedPage from ViewModel? This ViewModel is BindingContext in this page.
If you want to display tab in Tabbedpage with ViewModel, I think you can assign a DataTemplate to the ItemTemplate property to return pages for objects in the collection.
Like this sample:
https://github.com/xamarin/xamarin-forms-samples/tree/master/Navigation/TabbedPage

Related

Binding in navigation bar breaks when I do PushAsync

I have a MasterDetail page. The Detail is a NavigationPage. In the NavigationPage I put a ContentPage. So the hierarchy is:
MasterDetail
- Master
- Detail
- NavigationPage
- ContentPage
The navigation bar in my NavigationPage has an ActivityIndicator in it. The IsRunning property of the ActivityIndicator is bound to the IsRunning property of my ContentPage's view model. So when I load the ContentPage I see a spinning ActivityIndicator in the navigation bar. This works fine.
When I navigate to a new page, I call PushAsync on the NavigationPage to push a new ContentPage on the stack. The new page I push also has a view model with an IsRunning property. The problem is that the binding in the navigation bar is now broken and never shows the ActivityIndicator as running even if I set IsRunning to true.
So it feels like the binding gets broken on navigation. Any ideas on the root cause or how to fix this? Thanks in advance!
Mistake on my end, I wasn't calling SetTitleView when the new ContentPage was loaded. Turned out it wasn't a binding issue.

Xamarin Forms Prism Navigation from TabbedPage behaves as PushModelAsync or the navigation bar disappears

In Tabbed page sample given by Prism, I want to navigate from ViewA (first tab) to ViewD(not the next tab but next navigation page).
I don't understand why this removes the navigation bar on the top:
_navigationService.NavigateAsync(nameof(ViewD));
Like PushModelAsync instead of PushAsync. So that I loose the back button on the navigation tab which is not intended.
Am I missing something here?
The reason for this is that ViewA’s parent is not a NavigationPage, the result is that the Navigation Service assumes you want modal Navigation. You simply need to add useModalNavigation: false, this will make the Navigation Service push ViewD correctly inside the Navigation Page.

Binding to value in parent page from in usercontrol in listview datatemplate in Windows Phone 7

I have a ViewCharacter page that has a View Model (CharacterViewModel) as its DataContext, in that CharacterViewModel I have an ObservableCollection of WeaponViewModel that a ListBox is using as an ItemSource. The ListBox's DataTemplate contains a UserControl that is designed for the WeaponViewModels in the parent page's View Model's ObservableCollection. I need to bind a CommandParameter of a button in the UserControl to a property on the Parent page's view model (CharacterViewModel). I've tried using {Binding DataContext.TargetProperty, RelativeSource={RelativeSource TemplatedParent}} with no success and am at a loss of what to do without outright breaking all MVVM patterns.
Windows Phone 7 lacks a relative source binding. I have created a Silverlight implementation here:
http://www.scottlogic.co.uk/blog/colin/2009/02/relativesource-binding-in-silverlight/
But for WP7, where performance is critical, I would avoid using it!
I would instead recommend making the relationship between CharacterViewModel and WeaponViewModel bi-directional. In other words, add a Parent property to WeaponViewModel which will be a reference to the owning CharacterViewModel. You can then bind to properties on CharacterViewModel via this property.

How do I create a dynamic page based on a selection?

I am trying to dynamically fill a second panorama page based on what item was selected from the home application screen.
On the application's first start screen there is a listbox if items each with text. If a user taps on an item with text "foobar" a template page should load and the title of the template page should be set to "foobar" and this second panorama page should know that it's data should be related to "foobar".
Is there anyway to do this?
I currently have my MainPage navigate to a new page (DynamicPage.xaml). This navigation is triggered when a ListBox_SelectionChanged event occurs. I have the title text of the DynampicPage.xaml Binding to a TitleText variable that is located in MainPage.xaml.cs. However, when I do this the title of DynamicPage.xaml is ever only set to my initialization value for the titleText variable even though I am updating this variable right before I navigate to the page.
If anyone can provide some help I would be very grateful as I am just a beginner on the WP7 platform. Thanks!
The Binding you're using for the title is only going to update if the TitleText property is a dependency property or if your MainPage is implementing the INotifyPropertyChanged interface so your class can notify the UI when one of its properties changed.
http://windowsphonegeek.com/articles/All-about-Dependency-Properties-in-Silverlight-for-WP7
http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged(v=vs.95).aspx
But I think this is not the best way for you to achieve this. For now a much better way is to store your data somewhere in a static class, in the main page's constructor load these data into the listbox, and when the user selected an item, navigate the user to the second page like this:
NavigationService.Navigate(new Uri("/DynamicPage.xaml?Item=" + selectedItem.Id, UriKind.Relative));
When you navigate like this a new instance of DynamicPage is created, and in the OnNavigatedTo method you can access the navigation parameters and populate your page with the selected data. For example:
<controls:Panorama x:Name="MyPanorama" Title="TitleHere">...</controls:Panorama>
Item selectedItem = StaticData.GetItem(NavigationContext.QueryString["Item"]);
MyPanorama.Title = selectedItem.Name.ToUpper();
Description.Text = selectedItem.Description;
This way you can use secondary tiles and toast notifications to directly point to a specific content in your application.
If you're getting to understand the navigation you should definitely use the pattern called Model-View-ViewModel which is about to solve these problems mostly with bindings, but trust me, probably this is the easier way for now.

Sharing viewmodel to multiple views using Caliburn.Micro in WP7

I am currently working on a project which requires multiple views of the same viewmodel. Let me describe this way:
ViewModel: CustomerDetailsViewModel.cs (inherited from Screen class)
View: CustomerDetails.cs (this view has CustomerDetailsViewModel as datacontext and this set automatically by Caliburn.Micro)
View: CustomerInfo.cs (now this is the view where I want to share CustomerDetailsViewModel, which could have some data already modifed via CustomerDetails view)
I am currently using NavigationService to navigate to CustomerInfo view. Is there any way to pass the reference of current viewmodel to the view which user is navigating to in caliburn.micro?
Thanks in advance
idev
Use the attached property cal:Bind.Model="{Binding}" to bind the view to the view model.
See http://caliburnmicro.codeplex.com/wikipage?title=All%20About%20Actions "View First" section.
Alternatively you can also look into the cal:View.Context="MyContext" attached property as described here: http://caliburnmicro.codeplex.com/wikipage?title=Screens%2c%20Conductors%20and%20Composition "Multiple Views over the Same ViewModel" section.
Add a property or two to your App.xaml.cs. What ever you put in here will persist throughout the lifetime of the application (keep in mind that tombstoning will cause this property's value to be lost though). If you want to pass a ViewModel then setyour associated property in App.xaml.cs to the view model and then when the new page loads have it read from that same property.

Resources