2021-08-03 16:43:16 +00:00
|
|
|
|
using System;
|
|
|
|
|
using Xamarin.Forms;
|
2021-07-13 13:57:44 +00:00
|
|
|
|
|
|
|
|
|
namespace MobileApp1.Views
|
|
|
|
|
{
|
|
|
|
|
public class ContentPageHelper
|
|
|
|
|
{
|
|
|
|
|
|
2021-08-03 16:43:16 +00:00
|
|
|
|
public static App GetApp(ContentPage view) => GetParentElement<App>(view);
|
|
|
|
|
|
|
|
|
|
public static Application GetApplication(ContentPage view) => GetParentElement<Application>(view);
|
|
|
|
|
|
|
|
|
|
public static MainPage GetMainPage(ContentPage view) => GetParentElement<MainPage>(view);
|
|
|
|
|
|
|
|
|
|
public static T GetParentElement<T>(ContentPage view)
|
2021-07-13 13:57:44 +00:00
|
|
|
|
{
|
2021-08-03 16:43:16 +00:00
|
|
|
|
if (view == null) return default(T);
|
2021-07-13 13:57:44 +00:00
|
|
|
|
|
|
|
|
|
Element item = view.Parent;
|
|
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
if (item == null) break;
|
2021-08-03 16:43:16 +00:00
|
|
|
|
if (item is T) break;
|
2021-07-13 13:57:44 +00:00
|
|
|
|
|
|
|
|
|
item = item.Parent;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-03 16:43:16 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return (T)Convert.ChangeType(item, typeof(T));
|
|
|
|
|
}
|
|
|
|
|
catch (InvalidCastException)
|
|
|
|
|
{
|
|
|
|
|
return default(T);
|
|
|
|
|
}
|
2021-07-13 13:57:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-03 16:43:16 +00:00
|
|
|
|
public static void SetDetailPage(ContentPage view, Type pageType, bool useNavigationPage = true) => SetDetailPage(view, (Page)Activator.CreateInstance(pageType), useNavigationPage);
|
2021-07-13 13:57:44 +00:00
|
|
|
|
|
2021-08-03 16:43:16 +00:00
|
|
|
|
public static void SetDetailPage(ContentPage view, Page page, bool useNavigationPage = true)
|
|
|
|
|
{
|
|
|
|
|
if (view == null) return;
|
|
|
|
|
if (view.Parent == null) return;
|
|
|
|
|
if (page == null) return;
|
2021-07-13 13:57:44 +00:00
|
|
|
|
|
2021-08-03 16:43:16 +00:00
|
|
|
|
MainPage rs = GetMainPage(view);
|
|
|
|
|
if (rs == null)
|
2021-07-13 13:57:44 +00:00
|
|
|
|
{
|
2021-08-03 16:43:16 +00:00
|
|
|
|
return;
|
2021-07-13 13:57:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-03 16:43:16 +00:00
|
|
|
|
Device.BeginInvokeOnMainThread(() =>
|
|
|
|
|
{
|
|
|
|
|
if (useNavigationPage)
|
|
|
|
|
{
|
|
|
|
|
rs.Detail = new NavigationPage(page);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
rs.Detail = page;
|
|
|
|
|
}
|
|
|
|
|
});
|
2021-07-13 13:57:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-03 16:43:16 +00:00
|
|
|
|
public static void SetMainPage(ContentPage view, Page page, bool useNavigationPage = false)
|
2021-07-13 13:57:44 +00:00
|
|
|
|
{
|
|
|
|
|
if (view == null) return;
|
|
|
|
|
if (view.Parent == null) return;
|
|
|
|
|
if (page == null) return;
|
|
|
|
|
|
|
|
|
|
Application rs = GetApplication(view);
|
|
|
|
|
if (rs == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-03 16:43:16 +00:00
|
|
|
|
Device.BeginInvokeOnMainThread(() =>
|
|
|
|
|
{
|
|
|
|
|
if (useNavigationPage)
|
|
|
|
|
{
|
|
|
|
|
rs.MainPage = new NavigationPage(page);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
rs.MainPage = page;
|
|
|
|
|
}
|
|
|
|
|
});
|
2021-07-13 13:57:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static async void PushMainPageNavigation(ContentPage view, Page page)
|
|
|
|
|
{
|
|
|
|
|
if (view == null) return;
|
|
|
|
|
if (view.Parent == null) return;
|
|
|
|
|
if (page == null) return;
|
|
|
|
|
|
|
|
|
|
Application rs = GetApplication(view);
|
|
|
|
|
if (rs == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await rs?.MainPage.Navigation.PushAsync(page);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|