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

MonoGame: switching between views

06 Jun 2018, 22:08

Hey guys, I was wondering if anyone could perhaps point me in the right direction with this, I want to use NoesisGUI for my project but I'm unsure on how to switch between the XAML views. I have an idea but I'm not sure if you can use the following XAML attrbutes with NoesisGUI: "x:Class=", "xmlns:local=" and "<DataTemplate DataType=""></DataTemplate". Could anyone help me with this issue?


Thanks


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

Re: MonoGame: switching between views

07 Jun 2018, 03:02

You can use all of the above in Noesis. In MVVM you need to Create a DataTemplate in your resources, and for the DataType property you specify a class that implements INotifyPropertyChanged. Inside the DataTemplate put whatever control markup you want. Then in your root element, you add a ContentControl and bind your view model to the Content property. As long as you have a DataTemplate defined for the type of data that you bind, the view will automatically switch to it.
 
joshmond
Topic Author
Posts: 19
Joined: 02 Jun 2018, 00:10

Re: MonoGame: switching between views

07 Jun 2018, 11:15

Thanks for that, I'll definitely give it a try. By the way I'm using the MonoGame wrapper for NoesisGUI and that automatically points to the "Data" folder inside the SharpSDK and I was wondering if it's possible to create a WPF project inside my current game project and use that to store my views instead?

This is the code that creates the view and that method is taking whatever is inside the "Data" folder of the SDK and copying that too the main "Bin" directory of my MonoGame project:
 private void CreateNoesisGUI()
        {
            var rootPath = Path.Combine(Environment.CurrentDirectory, "Data");
            var providerManager = new NoesisProviderManager(
                new FolderXamlProvider(rootPath),
                new FolderFontProvider(rootPath),
                new FolderTextureProvider(rootPath, game.Graphics.GraphicsDevice));

            var config = new NoesisConfig(
                game.GetWindow,
                game.Graphics,
                providerManager,
                rootXamlFilePath: "Menus/MainMenu.xaml",
                themeXamlFilePath: "Themes/WindowsStyle.xaml",
                currentTotalGameTime: lastUpdateTotalGameTime);

            config.SetupInputFromWindows();

            noesisGUIWrapper = new NoesisWrapper(config);
        }
Is it possible to have this method reference a folder inside a WPF project (or a class library) that contains all the views?
 
mingingmingler
Posts: 23
Joined: 15 Jan 2018, 00:03

Re: MonoGame: switching between views

07 Jun 2018, 14:21

It's good practice to have a separate WPF project so that you can do your design and prototyping in Blend. But I wouldn't recommend including it in your MonoGame solution since it will break cross platform support, and because Noesis doesn't need it. You want to set your source up in such a way that your view models and views are accessible independently from both your WPF project and your game project. Whether that's by being selective of the files you include in each of them, or by having a new generic project host your view models and views. But be aware that if you take the latter approach, XAML can be a little finicky about namespaces. Only speaking generally though, I don't have a lot of experience with MonoGame so I can't be specific on that.
 
joshmond
Topic Author
Posts: 19
Joined: 02 Jun 2018, 00:10

Re: MonoGame: switching between views

07 Jun 2018, 17:24

That's what I thought initially, it would be a lot better to keep my views/viewmodels separate from my game logic. I'm just wondering how to do that. Would I need to change that's method's path to point to a folder insaide my WPF project or could I remove that method completely if not, would the NoesisWrapper.cs file also be included inside the WPF project. Sorry about all the questions but MVVM is something I am quite new to NoesisGUI and I'm trying to understand how it references files/folders. The example uses the Path.Combine() method along with Environment.GetCurrentdirectory() but from what I've read it's not considered good practice to do that, so I'm wondering how I would change that method to where it points to a different project (WPF) and also creates the view without being tied to the game logic? In the example, the CreateNoesisGUI() method in inside the Game1.cs which is the main class for a MonoGame project.


Thanks for the continued help and support with this, you really are helping me to understand the bigger picture. :)

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

Re: MonoGame: switching between views

08 Jun 2018, 02:40

Hey, just an update I have managed to structure my project in the way that I want and everything is working fine however, when it comes to switching views via the data-context I keep getting a noesis exception: "NoesisException: Menus\MainWindow.xaml(19): Can't set 'project.UI.Menus.Views.LoginView' on property 'FrameworkTemplate.VisualTree' of type 'Visual'.". Below is my main window and I've set the resources and the data-template:
<Grid
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
	xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
	mc:Ignorable="d"
	xmlns:local="clr-namespace:Project"
	xmlns:viewmodels="clr-namespace:Project.UI.Menus.ViewModels"
	xmlns:views="clr-namespace:Project.UI.Menus.Views">

	<Grid>
	
	<Grid.DataContext>
        <viewmodels:MainViewModel />
    </Grid.DataContext>
	
        <Grid.Resources>
		<DataTemplate x:Key="LoginView" DataType="{x:Type viewmodels:LoginViewModel}">
		<views:LoginView/>
        </DataTemplate>
		</Grid.Resources>
		
		<ViewBox>
		<Button Content="Click Me!"/>
		</ViewBox>
		
		
		</Grid>
   </Grid>
I haven't set any bindings yet but do you have any idea as to why I'm getting this exception?

Thanks

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

Re: MonoGame: switching between views

08 Jun 2018, 09:43

What is LoginView? It looks like a data type rather than a control. I reproduced the same error by doing just that. The content of a DataTemplate needs to be a control structure to get placed in the visual tree. If you run it in Blend it will tell you the problem at design time.
 
joshmond
Topic Author
Posts: 19
Joined: 02 Jun 2018, 00:10

Re: MonoGame: switching between views

08 Jun 2018, 14:47

The LoginView is a xaml file that is located inside my views folder of the project. How I have thing set up currently is the CreateNoesisGUI method creates the mainwindow.xaml and that file knows about my LoginView.xaml which is a UserControl. I don't think I understand when you mention a data template needing a control structure. I did put it inot a new WPF project and was getting a few errors but they didn't really help me that greatly. I'm not new to programming but I am farily new to XAML, so I don't think I quite understand how it's working with the data templates and loading it other views as resources. Do you have any examples that you could show to give what you're saying context? If it helps I have attached both my LoginView and my MainWindow xaml files below.


Thanks

-Joshmond
Attachments
xaml files.zip
(1.12 KiB) Downloaded 57 times
 
mingingmingler
Posts: 23
Joined: 15 Jan 2018, 00:03

Re: MonoGame: switching between views

08 Jun 2018, 16:31

The error you're getting means it's failing to put LoginView into the DataTemplate's visual tree because it's not interpreting it as a visual, i.e. it doesn't see it as a class that inherits from another control. What does the code behind for LoginView look like, is it a partial class that inherits from UserControl? If so, what errors was Blend throwing at you?
 
joshmond
Topic Author
Posts: 19
Joined: 02 Jun 2018, 00:10

Re: MonoGame: switching between views

08 Jun 2018, 22:34

If I change my Datacontext to point to a ViewModel instead of a View, my window loads up but I can't seem to load up a view despite my base class extending UserControl. Below I have attached both of my views and view models as well as my base view model (NotifyProperty.cs)





-Joshmond
Attachments
xaml viewmodels.zip
(3.03 KiB) Downloaded 58 times

Who is online

Users browsing this forum: No registered users and 0 guests