joshmond
Topic Author
Posts: 19
Joined: 02 Jun 2018, 00:10

Re: MonoGame: switching between views

09 Jun 2018, 05:14

Hey, just an update. I have changed added a LoginView.xaml.cs to my project and that current extends UserControl, I still get the same error message as before but what's interesting is if I extend from Noesis.UserControl, the class comes up with an error "Partial view must not extend a different base class" but for some reason it still compiles but does nothing at all. If I use the WPG Controls namespace, I get the same error as before. This is a very bizarre issue. Does the main window also need a view.xaml.cs file?



-Joshmond
 
mingingmingler
Posts: 23
Joined: 15 Jan 2018, 00:03

Re: MonoGame: switching between views

09 Jun 2018, 12:41

You get that error if you have a XAML file that defines a UserControl, but the code behind inherits from something other than UserControl. In this case it looks like it can't see the code behind at all. Here's some pointers from looking at your source:

1. Your NotifyProperty class shouldn't inherit from UserControl, i.e. it should only implement INotifyPropertyChanged
2. Create LoginView.xaml.cs to implement the code behind for LoginView.xaml, it should be a partial class that inherits from UserControl
3. LoginView.xaml should use the x:Class property to point to that partial class

With those changes it should work.

MainWindow.xaml doesn't need a code behind because it is not a type of UserControl.

Noesis wrote a good tutorial on UserControls for further reading: http://www.noesisengine.com/docs/Gui.Co ... orial.html

Finally, something to consider is whether your LoginView even needs to be a UserControl at all. UserControls are quite heavy handed, and in typical WPF it's usually the last option to consider. The benefit of using them in Noesis is you can have custom behaviors when the controls inside are interacted with, and you do this by subscribing to their events. For example if you need some sort of drag and drop with mouse events. If you don't need that, then take the entire Grid you've got in LoginView.xaml and put it in your DataTemplate in MainWindow.xaml, then you can remove LoginView entirely. This keeps it as a pure control structure without custom control behaviors.
 
joshmond
Topic Author
Posts: 19
Joined: 02 Jun 2018, 00:10

Re: MonoGame: switching between views

09 Jun 2018, 22:33

I just want to say thank you so much for your help and patience, after following your advice I was able to get my LoginView to switch without the UserControl. I do have two additional questions though.

1) If I choose not to use the UserControl for my views, can I still create complex and interactive UI elements with animations and various events such as drag/drop and hover?

2) Since I am developing a game, I will be having a lot of menus and various other UI systems such as dialogue boxes, quest logs, spell book, inventories and so on. Taking this into account, will my MainViewModel and my MainWindow.xaml have to know about all the potential views/view models I will be creating for my game, if so, is there a better way to approach switching views?

Thank you again for all you help and patience, I really do appreiciate it.

-Joshmond
 
mingingmingler
Posts: 23
Joined: 15 Jan 2018, 00:03

Re: MonoGame: switching between views

10 Jun 2018, 15:22

Good to hear, happy to be of service!

1) Animation can be achieved easily without UserControls. It's done with VisualStates, and most controls have their own states applicable to the control type - buttons have various click states, mouse over states, and such. You can even create transitions between these states. You can also trigger animation when data in your view model changes. The ease at which you can achieve complex animation is quite unprecedented for a UI framework, and it's one of the many reasons why XAML is quite simply the most powerful framework in the world. Animation does have its quirks that you may or may not need to eventually work around -- particularly regarding freezables -- but cross that bridge if you come to it.

Drag and drop can't be achieved with simple visual states, and that does require you to handle events in the code behind in UserControls. So if you want to be able to to drag and drop items in your inventory from one slot to another, you'll need to use them for that. But it's still a relatively straight forward task to implement, nonetheless. Noesis has a sample you can look at that achieves a similar thing https://github.com/Noesis/Tutorials/tre ... /ActionBar.

In WPF it is possible to achieve this with CustomControls rather than UserControls, which makes the whole code behind process a bit simpler. It becomes a case of using template parts, but Noesis doesn't seem to support this.

2) That depends really on how your UI is structured. If you're going for a console style full screen UI for things like the inventory, you'll probably want these menus to be contained within their own independent scenes. If they're more like windows, then these views will be contained inside your hud scene so they will naturally be visible to one another, but this is quite normal. You can make that easier to deal with by having a single main view model which contains all the necessary data structures for everything you need on your hud, including your windows. Bind it to the DataContext of your hud scene's root element, and then since data contexts get inherited down the visual tree, you can simply bind the relevant property from the view model to the controls that need them.
 
User avatar
sfernandez
Site Admin
Posts: 1911
Joined: 22 Dec 2011, 19:20

Re: MonoGame: switching between views

11 Jun 2018, 11:41

Thanks @mingingmingler a lot for your help here.

@joshmond Hi and welcome to Noesis.

You should see UserControls as a way to encapsulate a behavior inside a black box. A UserControl is typically composed of varios other controls (as you can see in our NumericUpDown user control tutorial). In this case the UserControl defines the appearance (that is responsible of the usercontrol creator) and the logic.

A custom control, on the other hand, is an extension of an existing control, or a totally new control derived from a more basic one, like a ContentControl. In that case you won't specify the look of the control directly, only the logic that provides the functionality to the control. The appearance is defined using a ControlTemplate, and different templates can be defined depending on how you want the control to look in every scenario (as you can see in our DateTime custom control tutorial).

I hope this helps clarify this topic a bit.
 
mingingmingler
Posts: 23
Joined: 15 Jan 2018, 00:03

Re: MonoGame: switching between views

11 Jun 2018, 16:47

Thanks for chiming in sfernandez! Quick question regarding CustomControls - are there any plans to support OnApplyTemplate and GetTemplateChild in Noesis? Reason for this being the FindName methods and their ilk can be a bit finicky because of timings and the order that the visual tree gets loaded, and the template methods streamline that process a bit better in WPF.
 
User avatar
sfernandez
Site Admin
Posts: 1911
Joined: 22 Dec 2011, 19:20

Re: MonoGame: switching between views

11 Jun 2018, 17:50

For now we don't have plans to provide the OnApplyTemplate virtual function, but we can investigate it. Users are using Loaded event to ask about template elements because at that point template is already applied, so we didn't find necessary to add that function.

The GetTemplateChild method is already available in FrameworkElement and will work once the template is being applied.
 
mingingmingler
Posts: 23
Joined: 15 Jan 2018, 00:03

Re: MonoGame: switching between views

11 Jun 2018, 20:08

I had a case where using the Loaded event in my CustomControl wasn't late enough to use FindName because the template children hadn't yet been loaded for some reason. I assumed this was something to do with the difference between the logical and visual trees. Sorry I can't be more specific on that, the decision to work around it has long passed. Didn't realise GetTemplateChild was already in - thanks!
 
joshmond
Topic Author
Posts: 19
Joined: 02 Jun 2018, 00:10

Re: MonoGame: switching between views

17 Jun 2018, 21:34

Hey guys, I just want to make sure that I understand what's going on as far as NoesisGUI is concerned because I'm still having trouble getting my views to switch with the NoesisGUI framework with MonoGame. I have created a separate WPF project using the same code I have with my project and everything works but I'm still having trouble switching views with my project. Below I have attached the necessary code as well as the error message, I have a working xaml file and a not working xaml file. They are the same file but one injects the xaml directly into the data-template which works but the other file tries to reference the view and this is what I can't seem to get working. I would also appreciate some pointers on how to switch between views, I'm not a huge fan of using the service-locator pattern or creating "managers" but the approach that I've taken seems to be criticised a lot, so any help there would be greatly appreciated.


-Joshmond
Attachments
Exception.png
xaml code.zip
(3.48 KiB) Downloaded 54 times
 
mingingmingler
Posts: 23
Joined: 15 Jan 2018, 00:03

Re: MonoGame: switching between views

18 Jun 2018, 14:13

In the code behind for LoginView you need to call InitializeComponent in the constructor. This call ensures the corresponding xaml for your class gets loaded, so without it, it won't recognize that your class is a view. It's a built in function in WPF, but in Noesis, you need to implement it yourself by making a call to Noesis.GUI.LoadComponent, and passing in the path to your corresponding xaml file. It'll look something like this:
private void InitializeComponent()
{
    Noesis.GUI.LoadComponent(this, "Menus/Views/LoginView.xaml");
}
In LoginView.xaml you don't need to set the DataContext, because this will get automatically passed in from the DataTemplate in MainWindow anyway.

Who is online

Users browsing this forum: No registered users and 1 guest