This commit is contained in:
TheJoKlLa 2020-11-12 20:04:31 +01:00
parent 2d071865e5
commit a5559eebe6
19 changed files with 519 additions and 4 deletions

View File

@ -18,7 +18,7 @@ namespace Borepin
Task InitNavigation() Task InitNavigation()
{ {
var navService = PageModelLocator.Resolve<INavigationService>(); var navService = PageModelLocator.Resolve<INavigationService>();
return navService.NaviagteToAsync<LoginPageModel>(); return navService.NaviagteToAsync<BorpinMasterDetailPageModel>(null, true);
} }
protected override async void OnStart() protected override async void OnStart()

View File

@ -18,9 +18,15 @@ namespace Borepin.Base
_ViewLookUp = new Dictionary<Type, Type>(); _ViewLookUp = new Dictionary<Type, Type>();
// Register Pages and PageModels // Register Pages and PageModels
Register<BorpinMasterDetailPageModel, BorpinMasterDetailPage>();
Register<BorpinMasterPageModel, BorpinMasterPage>();
Register<MachinesPageModel, MachinesPage>();
Register<SettingsPageModel, SettingsPage>();
Register<DashboardPageModel, DashboardPage>(); Register<DashboardPageModel, DashboardPage>();
Register<LoginPageModel, LoginPage>(); Register<LoginPageModel, LoginPage>();
// Register Services (Services are registered by Singeltons default) // Register Services (Services are registered by Singeltons default)
_Container.Register<INavigationService, NavigationService>(); _Container.Register<INavigationService, NavigationService>();
} }

View File

@ -0,0 +1,37 @@
using System;
using Xamarin.Forms;
namespace Borepin.Behaviour.Base
{
public class BehaviorBase<T> : Behavior<T> where T : BindableObject
{
public T AssociatedObject { get; private set; }
protected override void OnAttachedTo(T bindable)
{
base.OnAttachedTo(bindable);
AssociatedObject = bindable;
if (bindable.BindingContext != null)
BindingContext = bindable.BindingContext;
bindable.BindingContextChanged += OnBindingContextChanged;
}
protected override void OnDetachingFrom(T bindable)
{
base.OnDetachingFrom(bindable);
bindable.BindingContextChanged -= OnBindingContextChanged;
AssociatedObject = null;
}
void OnBindingContextChanged(object sender, EventArgs e)
{
OnBindingContextChanged();
}
protected override void OnBindingContextChanged()
{
base.OnBindingContextChanged();
BindingContext = AssociatedObject.BindingContext;
}
}
}

View File

@ -0,0 +1,91 @@
using Borepin.Behaviour.Base;
using System;
using System.Reflection;
using System.Windows.Input;
using Xamarin.Forms;
namespace Borepin.Behaviour
{
public class EventToCommandBehavior : BehaviorBase<VisualElement>
{
Delegate eventHandler;
public static readonly BindableProperty EventNameProperty = BindableProperty.Create("EventName", typeof(string), typeof(EventToCommandBehavior), null, propertyChanged: OnEventNameChanged);
public static readonly BindableProperty CommandProperty = BindableProperty.Create("Command", typeof(ICommand), typeof(EventToCommandBehavior), null);
public string EventName
{
get { return (string)GetValue(EventNameProperty); }
set { SetValue(EventNameProperty, value); }
}
public ICommand Command
{
get { return (ICommand)GetValue(CommandProperty); }
set { SetValue(CommandProperty, value); }
}
protected override void OnAttachedTo(VisualElement bindable)
{
base.OnAttachedTo(bindable);
RegisterEvent(EventName);
}
protected override void OnDetachingFrom(VisualElement bindable)
{
DeregisterEvent(EventName);
base.OnDetachingFrom(bindable);
}
static void OnEventNameChanged(BindableObject bindable, object oldValue, object newValue)
{
var behavior = (EventToCommandBehavior)bindable;
if (behavior.AssociatedObject == null) return;
string oldEventName = (string)oldValue;
string newEventName = (string)newValue;
behavior.DeregisterEvent(oldEventName);
behavior.RegisterEvent(newEventName);
}
void RegisterEvent(string name)
{
if (string.IsNullOrWhiteSpace(name)) return;
EventInfo eventInfo = AssociatedObject.GetType().GetRuntimeEvent(name);
if (eventInfo == null)
throw new ArgumentException(string.Format("EventToCommandBehavior: Can't register the '{0}' event.", EventName));
MethodInfo methodInfo = typeof(EventToCommandBehavior).GetTypeInfo().GetDeclaredMethod("OnEvent");
eventHandler = methodInfo.CreateDelegate(eventInfo.EventHandlerType, this);
eventInfo.AddEventHandler(AssociatedObject, eventHandler);
}
void DeregisterEvent(string name)
{
if (string.IsNullOrWhiteSpace(name) || eventHandler == null)
return;
EventInfo eventInfo = AssociatedObject.GetType().GetRuntimeEvent(name);
if (eventInfo == null)
throw new ArgumentException(string.Format("EventToCommandBehavior: Can't de-register the '{0}' event.", EventName));
eventInfo.RemoveEventHandler(AssociatedObject, eventHandler);
eventHandler = null;
}
void OnEvent(object sender, object eventArgs)
{
if (Command == null) return;
object resolvedParameter;
resolvedParameter = eventArgs;
if (Command.CanExecute(resolvedParameter))
Command.Execute(resolvedParameter);
}
}
}

View File

@ -24,6 +24,15 @@
<PackageReference Include="Xamarin.Essentials" Version="1.5.3.2" /> <PackageReference Include="Xamarin.Essentials" Version="1.5.3.2" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Update="Page\BorpinMasterDetailPage.xaml.cs">
<DependentUpon>BorpinMasterDetailPage.xaml</DependentUpon>
</Compile>
<Compile Update="Page\BorpinMasterPage.xaml.cs">
<DependentUpon>BorpinMasterPage.xaml</DependentUpon>
</Compile>
<Compile Update="Page\MachinesPage.xaml.cs">
<DependentUpon>MachinesPage.xaml</DependentUpon>
</Compile>
<Compile Update="Properties\Resources.Designer.cs"> <Compile Update="Properties\Resources.Designer.cs">
<DesignTime>True</DesignTime> <DesignTime>True</DesignTime>
<AutoGen>True</AutoGen> <AutoGen>True</AutoGen>
@ -39,12 +48,24 @@
</Compile> </Compile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<EmbeddedResource Update="Page\BorpinMasterDetailPage.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Update="Page\MachinesPage.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Update="Page\BorpinMasterPage.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Update="Page\DashboardPage.xaml"> <EmbeddedResource Update="Page\DashboardPage.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator> <Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource> </EmbeddedResource>
<EmbeddedResource Update="Page\LoginPage.xaml"> <EmbeddedResource Update="Page\LoginPage.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator> <Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource> </EmbeddedResource>
<EmbeddedResource Update="Page\SettingsPage.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Update="Properties\Resources.resx"> <EmbeddedResource Update="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator> <Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput> <LastGenOutput>Resources.Designer.cs</LastGenOutput>

View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8" ?>
<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Borepin.Page.BorpinMasterDetailPage"
xmlns:pages="clr-namespace:Borepin.Page">
<MasterDetailPage.Master>
<pages:BorpinMasterPage BindingContext="{Binding BorpinMasterPM}" />
</MasterDetailPage.Master>
<MasterDetailPage.Detail>
<NavigationPage>
<x:Arguments>
<pages:MachinesPage BindingContext="{Binding MachinePM}" />
</x:Arguments>
</NavigationPage>
</MasterDetailPage.Detail>
</MasterDetailPage>

View File

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace Borepin.Page
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class BorpinMasterDetailPage : MasterDetailPage
{
public BorpinMasterDetailPage()
{
InitializeComponent();
}
}
}

View File

@ -0,0 +1,49 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:behaviour="clr-namespace:Borepin.Behaviour"
x:Class="Borepin.Page.BorpinMasterPage"
Title="Master">
<StackLayout>
<ListView x:Name="MasterMenuListView"
SeparatorVisibility="None"
HasUnevenRows="true"
ItemsSource="{Binding MasterMenu}"
SelectedItem="{Binding MasterMenu_ItemSelected, Mode=TwoWay}">
<ListView.Behaviors>
<behaviour:EventToCommandBehavior EventName="ItemSelected" Command="{Binding ShowDetailPageAsyncCommand}" />
</ListView.Behaviors>
<ListView.Header>
<Grid BackgroundColor="#03A9F4">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="10"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="10"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="80"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="10"/>
</Grid.RowDefinitions>
<Label
Grid.Column="1"
Grid.Row="2"
Text="FabAccess"
Style="{DynamicResource SubtitleStyle}"/>
</Grid>
</ListView.Header>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Padding="15,10" HorizontalOptions="FillAndExpand">
<Label VerticalOptions="FillAndExpand"
VerticalTextAlignment="Center"
Text="{Binding Title}"
FontSize="24"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage>

View File

@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace Borepin.Page
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class BorpinMasterPage : ContentPage
{
public BorpinMasterPage()
{
InitializeComponent();
}
}
}

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Borepin.Page.MachinesPage"
Title="Machines">
<StackLayout Padding="10">
<Label Text="Site about the Machines"/>
</StackLayout>
</ContentPage>

View File

@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace Borepin.Page
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class MachinesPage : ContentPage
{
public MachinesPage()
{
InitializeComponent();
}
}
}

View File

@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Borepin.Page
{
public class MasterMenuItem
{
public MasterMenuItem()
{
TargetType = typeof(MasterMenuItem);
}
public int Id { get; set; }
public string Title { get; set; }
public Type TargetType { get; set; }
}
}

View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Borepin.Page.SettingsPage"
Title="Settings">
<ContentPage.Content>
<StackLayout>
<Label Text="Site about the Settings"/>
</StackLayout>
</ContentPage.Content>
</ContentPage>

View File

@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace Borepin.Page
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class SettingsPage : ContentPage
{
public SettingsPage()
{
InitializeComponent();
}
}
}

View File

@ -0,0 +1,48 @@
using Borepin.Base;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace Borepin.PageModel
{
public class BorpinMasterDetailPageModel : PageModelBase
{
public BorpinMasterDetailPageModel(BorpinMasterPageModel borpinMasterPM, MachinesPageModel machinesPM, SettingsPageModel settingsPM)
{
BorpinMasterPM = borpinMasterPM;
MachinesPM = machinesPM;
SettingsPM = settingsPM;
}
private BorpinMasterPageModel _BorpinMasterPM;
public BorpinMasterPageModel BorpinMasterPM
{
get => _BorpinMasterPM;
set => SetProperty(ref _BorpinMasterPM, value);
}
private MachinesPageModel _MachinesPM;
public MachinesPageModel MachinesPM
{
get => _MachinesPM;
set => SetProperty(ref _MachinesPM, value);
}
private SettingsPageModel _SettingsPM;
public SettingsPageModel SettingsPM
{
get => _SettingsPM;
set => SetProperty(ref _SettingsPM, value);
}
public override Task InitializeAsync(object navigationData)
{
return Task.WhenAny(
base.InitializeAsync(navigationData),
BorpinMasterPM.InitializeAsync(null),
MachinesPM.InitializeAsync(null),
SettingsPM.InitializeAsync(null));
}
}
}

View File

@ -0,0 +1,76 @@
using Borepin.Base;
using Borepin.Page;
using Borepin.Service.Navigation;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Xml.Schema;
using Xamarin.Forms;
namespace Borepin.PageModel
{
public class BorpinMasterPageModel : PageModelBase
{
private INavigationService _NavigationService;
public BorpinMasterPageModel(INavigationService navigationService)
{
_NavigationService = navigationService;
MasterMenu = new ObservableCollection<MasterMenuItem>()
{
new MasterMenuItem{ Id = 0, Title = "Machines" },
new MasterMenuItem{ Id = 1, Title = "Settings" }
};
_showDetailPageAsyncCommand = new Command(ShowDetailPageAsync);
}
private ObservableCollection<MasterMenuItem> _MasterMenu;
public ObservableCollection<MasterMenuItem> MasterMenu
{
get => _MasterMenu;
set => SetProperty(ref _MasterMenu, value);
}
private MasterMenuItem _MasterMenu_ItemSelected;
public MasterMenuItem MasterMenu_ItemSelected
{
get => _MasterMenu_ItemSelected;
set => SetProperty(ref _MasterMenu_ItemSelected, value);
}
private ICommand _showDetailPageAsyncCommand;
public ICommand ShowDetailPageAsyncCommand
{
get => _showDetailPageAsyncCommand;
set => SetProperty(ref _showDetailPageAsyncCommand, value);
}
private async void ShowDetailPageAsync()
{
switch (MasterMenu_ItemSelected.Title)
{
case "Machines":
await _NavigationService.NaviagteToAsync<MachinesPageModel>();
break;
case "Settings":
await _NavigationService.NaviagteToAsync<SettingsPageModel>();
break;
default:
break;
}
if (Application.Current.MainPage is MasterDetailPage masterDetailPage)
{
masterDetailPage.IsPresented = false;
}
else if (Application.Current.MainPage is NavigationPage navigationPage
&& navigationPage.CurrentPage is MasterDetailPage nestedMasterDetail)
{
nestedMasterDetail.IsPresented = false;
}
}
}
}

View File

@ -0,0 +1,20 @@
using Borepin.Base;
using Borepin.Service.Navigation;
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Input;
using Xamarin.Forms;
namespace Borepin.PageModel
{
public class MachinesPageModel : PageModelBase
{
private INavigationService _NaviagtionService;
public MachinesPageModel(INavigationService navigationService)
{
_NaviagtionService = navigationService;
}
}
}

View File

@ -0,0 +1,12 @@
using Borepin.Base;
using System;
using System.Collections.Generic;
using System.Text;
namespace Borepin.PageModel
{
public class SettingsPageModel : PageModelBase
{
}
}

View File

@ -20,11 +20,27 @@ namespace Borepin.Service.Navigation
Xamarin.Forms.Page page = PageModelLocator.CreatePage(typeof(TPageModelBase)); Xamarin.Forms.Page page = PageModelLocator.CreatePage(typeof(TPageModelBase));
if(setRoot) if(setRoot)
{ {
App.Current.MainPage = new NavigationPage(page); if (page is TabbedPage tabbedPage)
{
App.Current.MainPage = tabbedPage;
}
else if(page is MasterDetailPage masterDetailPage)
{
App.Current.MainPage = masterDetailPage;
}
else
{
App.Current.MainPage = new NavigationPage(page);
}
} }
else else
{ {
if(App.Current.MainPage is NavigationPage navPage) if (App.Current.MainPage is MasterDetailPage masterDetailPage)
{
masterDetailPage.Detail = new NavigationPage(page);
}
if (App.Current.MainPage is NavigationPage navPage)
{ {
await navPage.PushAsync(page); await navPage.PushAsync(page);
} }