Skip to content

Instantly share code, notes, and snippets.

@hermanbanken
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hermanbanken/dae58aa97f5dc8478371 to your computer and use it in GitHub Desktop.
Save hermanbanken/dae58aa97f5dc8478371 to your computer and use it in GitHub Desktop.
MasterDetailPage example
root = true
[*]
end_of_line = lf
insert_final_newline = true
[*.cs]
indent_style = space
indent_size = 2
using System;
using Xamarin.Forms;
using System.Collections.Generic;
using System.Linq;
namespace Example.FormsApp
{
public class App
{
public static Page GetMainPage (IEnumerable<IModule> modules)
{
return new RootPage (modules);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Maps;
using Xamarin;
namespace Example.FormsApp
{
[Register ("AppDelegate")]
public partial class AppDelegate : UIApplicationDelegate
{
UIWindow window;
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
Forms.Init ();
FormsMaps.Init();
//TinyIoC.TinyIoCContainer.Current.Register<IModule,ContactModule> ("contact");
//TinyIoC.TinyIoCContainer.Current.Register<IModule,SecondModule> ("second");
var modules = new List<IModule> { new ContactModule(), new SecondModule() };
window = new UIWindow (UIScreen.MainScreen.Bounds);
window.RootViewController = App.GetMainPage (modules).CreateViewController ();
window.MakeKeyAndVisible ();
return true;
}
}
}
using System;
using System.Diagnostics;
using Xamarin.Forms;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;
namespace Example.FormsApp
{
public class ContactModule : IModule
{
private Page contactPage;
public ContactModule ()
{
menu = new List<MenuItem> () {
new MenuItem () {
Label = "Contacts",
Icon = new Image() { Source = FileImageSource.FromResource( "map.png" ) },
}
};
contactPage = new ContentPage () {
Title = "Contacts",
Content = new Label (){
Text = "Contact list here",
VerticalOptions = LayoutOptions.CenterAndExpand,
HorizontalOptions = LayoutOptions.CenterAndExpand,
},
};
}
#region IModule implementation
public void Init ()
{
}
public Page Open (MenuItem item)
{
return contactPage;
}
private IList<MenuItem> menu;
public System.Collections.Generic.IList<MenuItem> MenuItems {
get {
return menu;
}
}
#endregion
}
}
using System;
using System.Collections.Generic;
using Xamarin.Forms;
namespace Example.FormsApp
{
public interface IModule
{
void Init();
IList<MenuItem> MenuItems { get; }
Page Open(MenuItem item);
}
public class MenuItem
{
public string Label { get; set; }
public Image Icon { get; set; }
public int NotificationCount { get; set; }
}
}
using System;
using Xamarin.Forms;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
namespace Example.FormsApp
{
public class RootPage : MasterDetailPage
{
private readonly IEnumerable<IModule> Modules;
public RootPage (IEnumerable<IModule> modules)
{
Modules = modules;
Master = new NavigationPage(new ContentPage (){
Title = "App Name",
Icon = "map.png",
Content = getListView(),
}) {
Title = "|||",
StyleId = "ShadowedMasterPage",
};
NavigateTo (modules.First (), modules.First ().MenuItems.First ());
}
private ListView getListView()
{
var cellTemplate = new DataTemplate (typeof(TextCell));
cellTemplate.SetBinding (TextCell.TextProperty, "Item2.Label");
cellTemplate.SetBinding(ImageCell.ImageSourceProperty, "Item2.Icon");
var menuListView = new ListView () {
ItemsSource = Modules.SelectMany (module => module.MenuItems.Select(item => Tuple.Create(module, item))),
ItemTemplate = cellTemplate,
BackgroundColor = Color.Transparent,
};
menuListView.ItemSelected += (sender, e) => {
var item = e.SelectedItem as Tuple<IModule, MenuItem>;
menuListView.SelectedItem = null;
if(item == null)
return;
NavigateTo(item.Item1, item.Item2);
};
return menuListView;
}
IDictionary<MenuItem,NavigationPage> cachedPages = new Dictionary<MenuItem,NavigationPage>();
void NavigateTo(IModule module, MenuItem item){
if (!cachedPages.ContainsKey (item)) {
var page = module.Open (item);
#if WINDOWS_PHONE
Detail = new ContentPage(); //work around to clear current page.
#endif
try {
cachedPages[item] = new NavigationPage(page)
{
BarBackgroundColor = Helper.Color.Tint,
BarTextColor = Helper.Color.TextColorOnTint,
StyleId = "ShadowedDetailPage",
};
} catch (NullReferenceException e) {
}
}
Detail = (Page) cachedPages [item] ?? (Page) new ContentPage () { Title = "404" };
IsPresented = false;
}
}
}
using System;
using System.Diagnostics;
using Xamarin.Forms;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;
namespace Example.FormsApp
{
public class SecondModule : IModule
{
private Page contactPage;
public SecondModule ()
{
menu = new List<MenuItem> () {
new MenuItem () {
Label = "Second",
Icon = new Image() { Source = FileImageSource.FromResource( "map.png" ) },
}
};
contactPage = new ContentPage () {
Title = "Second",
Content = new Label (){
Text = "Second screen here",
VerticalOptions = LayoutOptions.CenterAndExpand,
HorizontalOptions = LayoutOptions.CenterAndExpand,
},
};
}
#region IModule implementation
public void Init ()
{
}
public Page Open (MenuItem item)
{
return contactPage;
}
private IList<MenuItem> menu;
public System.Collections.Generic.IList<MenuItem> MenuItems {
get {
return menu;
}
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment