mingingmingler
Posts: 28
Joined: 15 Jan 2018, 00:03

Re: MonoGame: switching between views

21 Jun 2018, 00:25

Them being outside the game project doesn't change anything. All the paths are relative to the xaml file's location, e.g. you can step up a directory by leading the path with ../, so this is unaffected as long as your xaml files are always relative to each other. And the namespaces you use in the xaml need to reflect the namespaces defined in your cs files - that doesn't change. If they work in Blend, it will work in Noesis.
 
joshmond
Topic Author
Posts: 38
Joined: 02 Jun 2018, 00:10

Re: MonoGame: switching between views

21 Jun 2018, 13:44

I just want to thanks you again for all your help mate, your advice was great and I have everything working now, I can now switch between UserControls. The only thing I'm a little unsure on is two things, one is the way that I'm choosing to switch views, I'm wondering if that's ok or if there is a more optimal way to switch between views. My other question is how do I hide the other views, right now each ciew is being added to the main window rather than switching entirely. I'll link a screenshot below, I have two UserControl which are LoginView and CreateCharacterView, for now they just have simple buttons. You will see that the buttons are just added to the main window whereas ideally, I want everything to hide when I switch. Another thing as well is when I come to doing HUD views such as inventories and quest logs/speelsbook, would I create a HUD Window that has the data-templates for all my HUD views?

Thanks again.

-Joshmond
Attachments
Game.png
 
mingingmingler
Posts: 28
Joined: 15 Jan 2018, 00:03

Re: MonoGame: switching between views

21 Jun 2018, 14:36

Generally for a menu, 1 depth level is 1 view. So your root buttons will all reside in a single view, and then as you drill down, you switch to more relevant views. There's all sorts of approaches to this, and the choice comes down to how you feel it should be tackled for the project, rather than which one is objectively the best.

The single view approach is to just have several panels for each inner menu that you animate between, all within a single view. To do that, your view model needs a bindable data structure for each panel with properties determining the panel's current state, i.e. is it currently visible. And your buttons will use commands to change their states. If you have draggable windows for the inventory and quest log, this is the approach you need for that.

Another is the multi view approach. With this, you'd have a single ContentControl bound to the view model's CurrentContext, or somesuch. With a DataTemplate defined for each view type, it'll automatically switch when CurrentContext changes. So that means the commands in each view will need to bubble up to the root view model, where it will decide how to change CurrentContext. This is a general approach which has many applications, including menu drilling, tabs, selecting a quest in a log to see its detail, etc.

Going further, instead of using a root xaml file where you switch your views inside it, you could switch the root xaml file itself, and it uses its own separate view model. This is mostly a self containment preference, but I've seen cases where this has provided considerable performance, particularly where both the ui scenes and the game's simulation are quite intensive, and keeping their functionality tightly encapsulated was necessary. It can make sense to do this when switching from one full screen view to another, like from the hud to a loading screen.
 
User avatar
sfernandez
Site Admin
Posts: 2984
Joined: 22 Dec 2011, 19:20

Re: MonoGame: switching between views

21 Jun 2018, 17:51

If my xaml is outside of my project and my xaml.cs file is within my project, how would the paths look in the xaml because I would still need to let my xaml files know about my views and viewmodels, will they have the same namespace still, if not is is possible to reference a class in a different namespace/project?
If you take a look at our C# SDK you will see how we organize our xamls+code to work for both Blend and Noesis project.
We have the following folder layout:
+ ProjectFolder
  + Projects
    + windows_x86_64
      - ProjectNoesis.csproj
      - ProjectNoesis.sln
  + Src
    - App.xaml
    - App.xaml.cs
    - MainWindow.xaml
    - MainWindow.xaml.cs
    - ProjectBlend.csproj
    - ProjectBlend.sln
In Noesis project we use the "Add as Link" option to add code (and xaml), so we work with the same files that the Blend project.

@mingingmingler Thanks a lot for your detailed responses, they will be very useful for other people as well, for sure.
 
joshmond
Topic Author
Posts: 38
Joined: 02 Jun 2018, 00:10

Re: MonoGame: switching between views

25 Jun 2018, 00:35

Hey guys, I have one small problem when switching my views that's quite bizarre I am getting the intended behaviour (sort of) in that I can switch from my main window to my LoginView just fine however, when I try to switch from my LoginView to my CreateCharacterView, the button doesn't do anything. I have tried to reference the MainViewModel and use that SelectedViewModel property and pass in the main view as a reference but that didn't work out too well, I don't really know what could be causing this problem. Below I have attached all my Views and ViewModel code as well as two screenshots that show it's somewhat working. Any help on this would be greatly appreciated.

By the way, is it possible to use MVVM frameworks such as MVVMLight and Prism with NoesisGUI? I was looking into using a framework as a last resort if I cannot get this to work properly.

-Joshmond
Attachments
xaml code.zip
(4.75 KiB) Downloaded 124 times
Switching View Two.png
Switching View One.png
 
User avatar
sfernandez
Site Admin
Posts: 2984
Joined: 22 Dec 2011, 19:20

Re: MonoGame: switching between views

25 Jun 2018, 14:17

From the files you attached I see that you have in the MainWindow.xaml a ContentControl bound to the property SelectedViewModel. That property is resolved against the MainViewModel instance (because is the inherited DataContext of the ContentControl).

When you switch to the LoginView, the LoginCommand is changing the the SelectedViewModel property of the LoginViewModel instance, not on the parent MainViewModel instance. That is why nothing happens when you execute that button.
 
joshmond
Topic Author
Posts: 38
Joined: 02 Jun 2018, 00:10

Re: MonoGame: switching between views

25 Jun 2018, 22:53

I have tried using a reference to my MainViewModel from my LoginView, something similar to this:
mainViewModel.SelectedViewModel = new CreateCharacterViewModel(mainViewModel);
This didn't work and kept throwing a null reference exception, in order for this to work, would I have to take SelectedViewModel out of "BaseViewModel" and put into into "MainViewModel" and then make MainViewModel a singleton?

Thanks

-Joshmond
 
User avatar
sfernandez
Site Admin
Posts: 2984
Joined: 22 Dec 2011, 19:20

Re: MonoGame: switching between views

27 Jun 2018, 16:56

Is your mainViewModel variable initialized to a valid value?
Or what variable is null that is throwing that error?

Using MainViewModel as a singleton could be an alternative, so you can access it from anywhere something like this: MainViewModel.Instance.SelectedViewModel = ...

Who is online

Users browsing this forum: No registered users and 82 guests