Hello everyone,
I'm new to WPF and MVVM (i use mvvm light) and I'm trying to make an application that has 2 pages. On page 1 is a button that needs to switch the current page to the second page.
When I run my application and i press the button nothing happens. No error no output. So I started debugging and everything works the property changed event gets executed and the right data is passed on.
CODE:
This is the view model for my main window. it has a frame and that frame holds the pages.
using CommonServiceLocator;
using GalaSoft.MvvmLight.Views;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
namespace WorldCraft.ViewModel
{
public class MainWindowViewModel : MainViewModel, INotifyPropertyChanged
{
public MainWindowViewModel()
{
_page = new Pages.Home();
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private Page page;
public Page _page
{
get
{
return page;
}
set
{
page = value;
this.OnPropertyChanged("page");
}
}
}
}
this is the xaml for the main page.
<Window x:Class="WorldCraft.MainWindow"
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"
xmlns:local="clr-namespace:WorldCraft"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800"
DataContext="{Binding MainWindow, Source={StaticResource Locator}}">
<Grid>
<Frame Content="{Binding Path=_page, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</Grid>
</Window>
This is the code for the first page called Home. It needs to switch to another page called WorldEditor.
using CommonServiceLocator;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Ioc;
using GalaSoft.MvvmLight.Views;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using WorldCraft.ViewModel.Commands;
namespace WorldCraft.ViewModel
{
public class HomeViewModel : MainViewModel
{
public NavigateCommand navigateCommand { get; private set; }
public HomeViewModel()
{
navigateCommand = new NavigateCommand(SwitchTest);
}
public void SwitchTest()
{
ServiceLocator.Current.GetInstance<MainWindowViewModel>()._page = new Pages.WorldEditor();
}
}
}
I don't know what I'm doing wrong. Also, I don't know or this is the right way to navigate between pages. If there is a better way pls let me know.
What I have tried:
searching on the internet but it's hard to describe my problem to google if there is no output of any kind.