Hi, I write in this thread because I am trying to implement a way to navigate back by the different views hierarchy and I think the title of this one suits perfect.
As I didn't find any Navigate Back (as we can find in Xamarin, for instance, but it is normal considering the wpf approach) I have desgined a draft like you can see in t he code below.
It seems to work ok except in a case that you go to a view and navigate back several times in a fast way, in this case it crashes. I've tried to debug it through noesis methods until this point:
Class: Collection.
Method: Add
ret = NoesisGUI_PINVOKE.Collection_Add__SWIG_0(swigCPtr, BaseComponent.getCPtr(item));
It seems that the delegates that is calling native intructions makes the app to crash, in a way I cannot even catch through an exception (debuging with adb in android says something like app pid="xxx" died!)
After I put the uc.Dispose() in the LoadView method in my MainView it seems to work a little better, but anyway if go too fast (going to a view, navigate back, go again, etc..) it crashes.
Also, when I use an internal button that calls the ViewActions.NavigateBack (acting slowly, with pauses of seconds) it seems to work in android (in editor mode it always work). But when I use the Android back button (as you can see in the MainView.Update) it automatically crash (no way of making it work).
If I use Esc in Unity Editor, after two or three times of going to the view and navigating back it makes the Unity3d editor to crash (which is something not very unusual as the Editor crashes constantly with any thing, even when blinking in pair seconds : p)
I will probably report it as a bug, but first I want to find a work around way.
public static class ViewActions
{
private static Stack<ViewModelBaseUnity> _navigationPath;
public static Stack<ViewModelBaseUnity> NavigationPath
{
get { return _navigationPath ?? (_navigationPath= new Stack<ViewModelBaseUnity>()); }
}
public static Action<ViewModelBaseUnity> LoadView { get; set; }
public static void Navigate(ViewModelBaseUnity viewModel)
{
NavigationPath.Push(viewModel);
LoadView(viewModel);
}
public static void NavigateBack()
{
if (NavigationPath.Count > 1)
{
// remove current
var current = NavigationPath.Pop();
// retrieves and remove parent
var prev = NavigationPath.Pop();
// navigate to parent
Navigate(prev);
}
}
}
and in the MainView.cs
public class MainView : MonoBehaviour, IXamlLocalizable
{
public string Xaml { get { return "MainView.xaml"; }}
private NoesisGUIPanel GuiPanel;
private Grid DivContent;
public void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
try
{
ViewActions.NavigateBack();
}
catch (Exception e1)
{
ClientDebugger.SendToDebug("NavigateBack error: "+e1.Message);
}
}
}
private void LoadView(ViewModelBaseUnity viewModel)
{
var viewType = ViewDictionary.RegisteredTypes[viewModel.GetType()];
Debug.Log("loading view:" + viewType.Name+"---------------------------------");
try
{
var viewCodeBehind = gameObject.AddComponent(viewType);
var xaml = ((IXamlLocalizable)viewCodeBehind).Xaml;
var uc = DivContent.GetChildren();
uc.Dispose();
DivContent.GetChildren().Clear();
var newView = NoesisGUISystem.LoadXaml<UserControl>(xaml);
DivContent.GetChildren().Add(newView);
newView.SetDataContext(viewModel);
}
catch (Exception e1)
{
Debug.Log(e1.Message);
}
}
}