107 lines
2.6 KiB
C#
107 lines
2.6 KiB
C#
using System;
|
|
using Xamarin.Forms;
|
|
|
|
namespace MobileApp1.Views
|
|
{
|
|
public class ContentViewHelper
|
|
{
|
|
|
|
public static App GetApp(ContentView view) => (App)GetParentElement<App>(view);
|
|
//{
|
|
// if (view == null) return null;
|
|
|
|
// Element item = view.Parent;
|
|
|
|
// while (true)
|
|
// {
|
|
// if (item == null) break;
|
|
// if (item is App) break;
|
|
|
|
// item = item.Parent;
|
|
// }
|
|
|
|
// return (item as App);
|
|
//}
|
|
|
|
public static Application GetApplication(ContentView 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 ContentPage GetContentPage(ContentView view) => (ContentPage)GetParentElement<ContentPage>(view);
|
|
|
|
public static object GetParentElement<T>(ContentView view)
|
|
{
|
|
if (view == null) return default(T);
|
|
|
|
Element item = view.Parent;
|
|
|
|
while (true)
|
|
{
|
|
if (item == null) break;
|
|
if (item is T) break;
|
|
|
|
item = item.Parent;
|
|
}
|
|
|
|
//try
|
|
//{
|
|
// return (T)Convert.ChangeType(item, typeof(T));
|
|
//}
|
|
//catch (InvalidCastException)
|
|
//{
|
|
// return default(T);
|
|
//}
|
|
|
|
return item;
|
|
}
|
|
|
|
public static void SetMainPage(ContentView view, Page page)
|
|
{
|
|
if (view == null) return;
|
|
if (view.Parent == null) return;
|
|
if (page == null) return;
|
|
|
|
Application rs = GetApplication(view);
|
|
if (rs == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
Device.BeginInvokeOnMainThread(() =>
|
|
{
|
|
//rs.MainPage = new NavigationPage(new PasswordResetPage());
|
|
rs.MainPage = page;
|
|
});
|
|
}
|
|
|
|
public static async void PushMainPageNavigation(ContentView 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);
|
|
}
|
|
|
|
}
|
|
}
|