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-04 23:50:48 +00:00
|
|
|
|
public static App GetApp(ContentPage view) => (App)GetParentElement<App>(view);
|
|
|
|
|
//{
|
|
|
|
|
// if (view == null) return null;
|
2021-08-03 16:43:16 +00:00
|
|
|
|
|
2021-08-04 23:50:48 +00:00
|
|
|
|
// Element item = view.Parent;
|
2021-08-03 16:43:16 +00:00
|
|
|
|
|
2021-08-04 23:50:48 +00:00
|
|
|
|
// while (true)
|
|
|
|
|
// {
|
|
|
|
|
// if (item == null) break;
|
|
|
|
|
// if (item is App) break;
|
2021-08-03 16:43:16 +00:00
|
|
|
|
|
2021-08-04 23:50:48 +00:00
|
|
|
|
// item = item.Parent;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// return (item as App);
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
public static Application GetApplication(ContentPage view) => (Application)GetParentElement<Application>(view);
|
|
|
|
|
//{
|
|
|
|
|
// if (view == null) return null;
|
|
|
|
|
|
|
|
|
|
// Element item = view.Parent;
|
|
|
|
|
|
|
|
|
|
// while (true)
|
|
|
|
|
// {
|
|
|
|
|
// if (item == null) break;
|
|
|
|
|
// if (item is Application) break;
|
|
|
|
|
|
|
|
|
|
// item = item.Parent;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// return (item as Application);
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
public static MainPage GetMainPage(ContentPage view) => (MainPage)GetParentElement<MainPage>(view);
|
|
|
|
|
|
|
|
|
|
public static object 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-04 23:50:48 +00:00
|
|
|
|
//try
|
|
|
|
|
//{
|
|
|
|
|
// return (T)Convert.ChangeType(item, typeof(T));
|
|
|
|
|
//}
|
|
|
|
|
//catch (InvalidCastException)
|
|
|
|
|
//{
|
|
|
|
|
// return default(T);
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
return item;
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|