I am trying to implement Ooui in order to make my viewmodels supports ASP.NET Core and to target the web. I have a class library who is share across WPF, Xamarin.Forms using the MvvmLight framework. So i try to get the better approach for ooui. An advise will be apprecieted. For now i am not able to make the DisplayAlert work on every pages.
public class BaseController : Controller, IDialogService, INavigationService
{
private static Stack<Page> _stack = null;
protected Page CurrentPage => page.Navigation.NavigationStack.LastOrDefault();
protected Page RootPage => page.Navigation.NavigationStack.FirstOrDefault();
static BaseController()
{
_stack = new Stack<Page>();
}
private IHttpContextAccessor _service;
private IApplicationBuilder app;
protected NavigationPage page;
private HttpContext Context
{
get { return _service.HttpContext; }
}
public string CurrentPageKey => throw new NotImplementedException();
public BaseController()
{
if (!SimpleIoc.Default.IsRegistered<IDialogService>())
SimpleIoc.Default.Register<IDialogService>(() => this);
if (!SimpleIoc.Default.IsRegistered<INavigationService>())
SimpleIoc.Default.Register<INavigationService>(() => this);
_service = SimpleIoc.Default.GetInstance<IHttpContextAccessor>();
app = SimpleIoc.Default.GetInstance<IApplicationBuilder>();
}
public async Task ShowError(string message, string title, string buttonText, Action afterHideCallback)
{
await CurrentPage.DisplayAlert(title, message, buttonText);
afterHideCallback();
}
public async Task ShowError(Exception error, string title, string buttonText, Action afterHideCallback)
{
await CurrentPage.DisplayAlert(title, error.Message, buttonText);
afterHideCallback();
}
public async Task ShowMessage(string message, string title)
{
await CurrentPage.DisplayAlert(title, message, "OK");
}
public async Task ShowMessage(string message, string title, string buttonText, Action afterHideCallback)
{
await CurrentPage.DisplayAlert(title, message, buttonText);
afterHideCallback();
}
public async Task<bool> ShowMessage(string message, string title, string buttonConfirmText, string buttonCancelText, Action<bool> afterHideCallback)
{
var result = await page.DisplayAlert(title, message, buttonConfirmText, buttonCancelText);
afterHideCallback(result);
return result;
}
public async Task ShowMessageBox(string message, string title)
{
await CurrentPage.DisplayAlert(title, message, "OK");
}
public void GoBack()
{
page.PopAsync();
page.PushAsync(_stack.Pop());
}
public void NavigateTo(string pageKey)
{
_stack.Push(CurrentPage);
page.PopAsync();
page.PushAsync(SimpleIoc.Default.GetInstance<Page>(pageKey));
}
public void NavigateTo(string pageKey, object parameter)
{
throw new NotImplementedException();
}
}
public class HomeController : BaseController
{
public HomeController()
: base()
{
SimpleIoc.Default.Register<Page>(() => new Next(), "Next");
SimpleIoc.Default.Register<Page>(() => new MainPage(), "MainPage");
}
public IActionResult Index()
{
page = new NavigationPage(new MainPage());
//NavigateTo("MainPage");
return new ElementResult(page.GetOouiElement(), "Hello from XAML!");
}
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}