NavigateToViewModel with Caliburn Micro 2 and Windows Phone 8.1 - caliburn.micro

I'm trying to figure out how to successfully get Caliburn Micro to navigate from one page to another in a Windows Phone 8.1 app.
My first page loads just fine, as specified in my App class:
protected override void OnLaunched(LaunchActivatedEventArgs args)
{
this.DisplayRootViewFor<HomeViewModel>();
}
This launches the HomeView without issue. On that view I have a button that calls the following method:
public void GoToPage2()
{
this.navigationService.NavigateToViewModel<Page2ViewModel>();
}
This method is called when the button is pressed and the constructor for Page2ViewModel is called as well. The page just never displays and I can't figure out why. I feel like I'm missing a core concept, but I can't find any examples of how this should work.
Thanks for any help.

The solution is odd, and perhaps a bug in Caliburn Micro. In the OnLaunched method I used to have:
protected override void OnLaunched(LaunchActivatedEventArgs args)
{
this.DisplayRootViewFor<HomeViewModel>();
}
This worked and launched the home view, but subsequent navigation never worked. After comparing to a sample application I found, I changed the code to:
protected override void OnLaunched(LaunchActivatedEventArgs args)
{
this.DisplayRootView<HomeView>();
}
This also displays the home view, but now subsequent navigation works! I'm not sure why this would be the case, but at least I have an answer.

Related

Xamarin Forms AppShell How return to previous page

I see a lot of threads on this sort of subject but none seem to apply to my question. I don't have a navigation page, I have a hamburger menu -- so Push/PopAsync() would not appear to be the answer. And I don't want to go to a specific page, I want to go back to the previous page (whatever it was) so GoToAsync() would not appear to be the answer.
Xamarin app for Android and UWP with iOS somewhere in the future. The description of the problem below is specific to Android; it works a little differently on UWP.
I want to put a Help entry in the hamburger menu that will take the user to a help page in the default browser. Hamburger menu seems to only go to an app page, so I defined a "dummy" View page that displays "Loading ..." and issues Browser.OpenAsync() in its OnAppearing() method, and that pretty much works. The problem is that the user would expect that the Back button would take him or her to the page they were on before clicking Help. I tried a couple of things. I have gotten close with the following but it does not quite work correctly:
In each of my other Views' OnAppearing() I call a method that saves the value of Current.MainPage.CurrentItem in a static. Then in the Help page after the OpenAsync() I set Current.MainPage.CurrentItem to its setting from the last page before the Help page.
Console.WriteLine("#### HelpPage loading Web Help");
_ = State.DisplayHelpPage(this, "MainHelp"); // _ = await Browser.OpenAsync(uri, blo);
Console.WriteLine("#### HelpPage returning to previous page");
State.ReloadPreviousPage(); // Current.MainPage).CurrentItem = lastFlyoutItem;
It almost works. The first time I click Help in the hamburger menu I get
#### HelpPage loading Web Help
#### HelpPage returning to previous page
#### HelpPage loading Web Help
#### HelpPage returning to previous page
The Web page loads perfectly. But when I click the Back button it displays again. Obviously my OnAppearing() method has been driven twice, which I do not understand.
If I click the Back button again I come back to the previous page in the application just as I wanted. The next time I click Help in the Hamburger menu it takes me to my dummy View page with no Web page. Obviously, my OnAppearing() is not being driven at all. But after that it works perfectly. I can go to any app page, and click Help in the menu. I get the Web page, and the Back button takes me back to the app and the page. In UWP of course the browser does not load on top of the app Views, and I seem to see it being loaded twice every time.
So ... what should I be doing differently? And why is my OnAppearing() being driven twice and then not at all ... and thereafter as I would expect?
There are several parts to this answer:
Get the previous page on to the Navigation stack. This is done by intercepting the Route "//HelpPage", and replacing it with a route that ISN'T a child of Shell.
Remember "FakePageVisible", so we know to do "PopAsync" in OnResume, when app returns from browser.
(Optional) "Entering" flag prevents going to browser twice.
App.xaml.cs:
public partial class App : Application
{
public App()
{
InitializeComponent();
MainPage = new AppShell();
}
protected override void OnResume()
{
if (HelpPage.FakePageVisible) {
HelpPage.FakePageVisible = false;
var shell = MainPage as AppShell;
if (shell != null) {
shell.Navigation.PopAsync();
}
}
}
}
AppShell.xaml.cs:
public partial class AppShell : Xamarin.Forms.Shell
{
public AppShell()
{
InitializeComponent();
// Define a route that isn't a child of Shell.
Routing.RegisterRoute("Help2", typeof(HelpPage));
}
protected override void OnNavigating(ShellNavigatingEventArgs args)
{
base.OnNavigating(args);
if (args.Current != null) {
if (args.Source == ShellNavigationSource.ShellItemChanged) {
if (args.Target.Location.OriginalString == "//HelpPage") {
// Cancel the original route.
args.Cancel();
Device.BeginInvokeOnMainThread(() => {
// Used by the next OnAppearing.
HelpPage.Entering = true;
// Go there by a route that isn't a child of Shell.
// Doing so, pushes our previous location on to Navigation stack.
Shell.Current.GoToAsync("Help2");
});
}
}
}
}
}
HelpPage.xaml.cs:
public partial class HelpPage : ContentPage
{
public static bool Entering;
public static bool FakePageVisible;
protected override void OnAppearing
{
// Make sure this only happens once (just in case).
if (Entering) {
Entering = false;
FakePageVisible = true;
Xamarin.Essentials.Browser.OpenAsync("https://aka.ms/xamarin-quickstart");
}
}
}
For a simple demo, this code communicates via static variables in HelpPage. Re-factor as appropriate for your situation.

Navigation issue in Xamarin Forms with same page type multiple time

Relatively new to Xamarin, hitting an issue with PushAsync and navigation I can't figure out.
I have a main navigation page, and then a "MyContentPage" that is responsible for rendering a dynamic list based on a supplied id. When the user clicks on a list item they go to a next (newed up) "MyContentPage" (same class) with a different id. Basically a recursive page hierarchy based on a local db.
Problem is that navigation seems to quickly get messed up in some way I can't work out. The pages get swapped around, or get lost. Navigating back to root, if I click back down again, it skips to a page that is further down etc.
So basically the one page apart from the main page (which has multiple navigationpages in tabs - though I only use one tab at this point) binds its controls to this function:
public async Task NavigateToContent(int contentId)
{
await ((Application.Current.MainPage) as TabbedPage)?.CurrentPage.Navigation.PushAsync(new MyContentPage(contentId));
}
The above is then used recursively. Ie. Similar controls bind to the same function until there are no further pages to click down to.
The MyContentPage constructor loads the model:
public MyContentPage(int id)
{
InitializeComponent();
_id = id;
BindingContext = viewModel = new ContentPageViewModel(id);
}
What is the issue here?
From what you mentioned in comments, the issue is caused by the navigation code called in the 'service' class. When you call the service method multiple times, it actually changes the current navigation stack in xamarin forms. Move the page navigation code from service class to viewmodel class.
Or try to put the page navigation source code into something like 'NavigationService' (one example is the one in https://learn.microsoft.com/en-us/xamarin/xamarin-forms/enterprise-application-patterns/ ) and inject this service into your view model class.
OK so this all turned out to be an issue with concurrency.
The original button click was like this:
private void Button_Clicked(object sender, EventArgs e)
{
Task.Run(async () => await (BindingContext as ContentPageViewModel).ExecuteNavCommand(sender));
}
But this resulted in a UI operation happening on a different task
The event handler can be declared as async
The correction is
private async void Button_Clicked(object sender, EventArgs e)
{
await viewModel.ExecuteNavCommand(sender);
}

JavaFx WebView - how to scroll it to the bottom

I have made a conversation stage in JavaFx with a WebView that is displaying whole conversation. I want to show it from the bottom where the newest messages are so I used a function "execute script":
public void initialize() throws SQLException
{
buildText();
conversationText.getEngine().loadContent(text1);
conversationText.getEngine().executeScript("window.scrollTo(0, document.body.scrollHeight);");
conversationText.getEngine().setUserStyleSheetLocation(getClass().getResource("/html/style.css").toExternalForm());
}
I wrote this function in an initialize() void but it doesn't work, when I open the conversation it shows me all from the top where the eldest messages are :
But what is interesting it works properly when I use this metod form a void that is connected with a button named "script"
public void script()
{
conversationText.getEngine().executeScript("window.scrollTo(0, document.body.scrollHeight);");
}
when I press this button it scrolls to the bottom :
Is there any way to do this correct in an initialize void ? I have tried many ways like inserting the button method script() into initialize but i noticed that if I use function webengine.loadcontent() it always displays me all from the top.
I want to show the user the newest messages at the bottom from the beginning , not after pushing a button.

Make JavaFx application Fullscreen OnClick

Hello i am making a web browser in javafx and i want to make it fullscreen when user clicks on Fullscreen button. I tried the below given code but unfortunately it is giving error.
Fullscreen.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
primaryStage.setFullScreen(true); // here it gives error "cannot find variable primaryStage"
}
});
**I know that this doesn't follow the basics of java but i didn't find another way of implementing it.
Kindly help :)
Assuming Fullscreen is a button, just get the stage it's in with
((Stage)Fullscreen.getScene().getWindow()).setFullScreen(true);

WPF equivalent of RootVisual in Caliburn.Micro docs

I am trying to work through the Caliburn.Micro Soup to Nuts tutorials using WPF rather than Silverlight.
In the "All About Actions" section of the Caliburn.Micro tutorial the following Silverlight code is shown in the overridden MefBootstrapper:
public class MefBootstrapper : BootstrapperBase
{
//same as before
protected override void OnStartup(object sender, StartupEventArgs e)
{
Application.RootVisual = new ShellView();
}
//same as before
}
With the comment "In this scenario, we simply override OnStartup, instantiate the view ourselves and set it as the RootVisual (or call Show in the case of WPF)."
Does anyone know what is the format of the Show command that is used? (I have tried a number of internet researched items but none worked). I'm guessing it must be very simple because of the casual mention in the tutorial...

Resources