title: Prism 6 Navigation Wierdness
body: <p>I am still learning prism and I want to understand navigation. Having read the manual and followed @brianlagunas MVVM made simple webinar online, I decided to try a small project and see. I get a stackoverflow exception when I try to navigate to a view that has a viewmodel data-bound to some controls on my view. Here is the code below:</p>
  
  <p>Profile View with empty viewmodel:</p>
  
  <pre><code>&lt;UserControl x:Class="SampleLogin.Views.Profile"
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
           xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
           xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
           xmlns:prism="http://prismlibrary.com/"
           prism:ViewModelLocator.AutoWireViewModel="True"
           xmlns:local="clr-namespace:SampleLogin.Views"
           mc:Ignorable="d" 
           d:DesignHeight="300" d:DesignWidth="300"&gt;
  &lt;Grid&gt;
      &lt;TextBlock x:Name="ProfileTextBlock" HorizontalAlignment="Center" Margin="74,29,78,0" TextWrapping="Wrap" Text="PROFILE PAGE" VerticalAlignment="Top" Height="45" Width="148" FontSize="21.333" FontWeight="Bold"/&gt;
      &lt;TextBlock x:Name="Username" HorizontalAlignment="Left" Height="26" Margin="103,96,0,0" TextWrapping="Wrap" Text="{Binding username}" VerticalAlignment="Top" Width="130"/&gt;
      &lt;TextBlock x:Name="LoginTime" HorizontalAlignment="Left" Margin="109,152,0,0" TextWrapping="Wrap" Text="{Binding LoginTime}" VerticalAlignment="Top" Width="124" Height="33"/&gt;
      &lt;Label x:Name="label" Content="Username :" HorizontalAlignment="Left" Margin="10,96,0,0" VerticalAlignment="Top" Width="71" FontWeight="Bold"/&gt;
      &lt;Label x:Name="label1" Content="Login-Time:" HorizontalAlignment="Left" Height="26" Margin="12,159,0,0" VerticalAlignment="Top" Width="86" FontWeight="Bold"/&gt;
  
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>
  
  <p>but it has an empty viewmodel and it loads fine.</p>
  
  <p>Here is another view with a simple form that is data-bound to its viewmodel</p>
  
  <pre><code>&lt;UserControl x:Class="SampleLogin.Views.LoginUser"
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
           xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
           xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
           xmlns:prism="http://prismlibrary.com/"
           prism:ViewModelLocator.AutoWireViewModel="True"
           xmlns:local="clr-namespace:SampleLogin.Views"
           mc:Ignorable="d" 
           d:DesignHeight="300" d:DesignWidth="325"&gt;
  &lt;Grid&gt;
      &lt;Label x:Name="usernameLabel" Content="Username :" HorizontalAlignment="Left" Margin="10,93,0,0" VerticalAlignment="Top" Width="113" Height="35" FontSize="18.667" FontWeight="Bold"/&gt;
      &lt;Label x:Name="label" Content="Password :&amp;#xD;&amp;#xA;" HorizontalAlignment="Left" Margin="10,146,0,0" VerticalAlignment="Top" Width="100" Height="38" FontSize="18.667" FontWeight="Bold"/&gt;
      &lt;TextBox x:Name="Username" HorizontalAlignment="Left" Height="35" Margin="132,93,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="178" Text="{Binding Username, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/&gt;
      &lt;PasswordBox x:Name="passwordBox" HorizontalAlignment="Left" Margin="132,146,0,0" VerticalAlignment="Top" Width="178" Height="35"/&gt;
      &lt;Button x:Name="Loginbtn" Content="Login" HorizontalAlignment="Left" Margin="85,208,0,0" VerticalAlignment="Top" Width="89" Height="42" FontSize="18.667" FontWeight="Bold"
              Command="{Binding LoginCommand}" CommandParameter="{Binding ElementName=passwordBox}" /&gt;
      &lt;Button x:Name="Logoutbtn" Content="Logout" HorizontalAlignment="Left" Margin="230,208,0,0" VerticalAlignment="Top" Width="80" Height="42" FontWeight="Bold" FontSize="18.667"/&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>
  
  <p>Here is the viewmodel its bound to: left it very simple just to understand the concept:</p>
  
  <pre><code> public class LoginUserViewModel : BindableBase
  {
      private AuthenticationService _auth;
      public DelegateCommand&lt;object&gt; LoginCommand { get; set; }
  
      public LoginUserViewModel(AuthenticationService auth)
      {
          _auth = auth;
          LoginCommand = new DelegateCommand&lt;object&gt;(LoginUser, CanLoginUser);
      }
  
      private void LoginUser(object obj)
      {
  
          var passwdbox = obj as PasswordBox;
          _auth.LoginUser(Username, passwdbox.SecurePassword);
  
      }
  
      private bool CanLoginUser(object obj)
      {
          var password = obj as PasswordBox;
          return string.IsNullOrWhiteSpace(Username);
      }
  
      private string _username = "Enter Username here";
      public string Username
      {
          get { return _username; }
          set { SetProperty(ref _username, value); }
      }
  
  }
  </code></pre>
  
  <p>And here is the MainWindowViewmodel that handles the navigation using the button at the top of my mainwindow view.</p>
  
  <pre><code>    public class MainWindowViewModel : BindableBase
  {
      IRegionManager _regions;
      public DelegateCommand&lt;string&gt; NavigationCommand { get; set; }
  
      public MainWindowViewModel(IRegionManager regions)
      {
          _regions = regions;
          NavigationCommand = new DelegateCommand&lt;string&gt;(Navigate);
      }
  
      private void Navigate(string uri)
      {
          _regions.RequestNavigate("ContentRegion", uri);
      }
  
  }
  </code></pre>
  
  <p>And lastly, here is the MainWindowViewModel that binds to the viewmodel:</p>
  
  <pre><code>&lt;Window x:Class="SampleLogin.Views.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:prism="http://prismlibrary.com/"
      prism:ViewModelLocator.AutoWireViewModel="True"
      xmlns:local="clr-namespace:SampleLogin.Views"
      mc:Ignorable="d"
      Title="MainWindow" Height="350" Width="525"&gt;
  &lt;Grid&gt;
      &lt;Grid.RowDefinitions&gt;
          &lt;RowDefinition Height="Auto" /&gt;
          &lt;RowDefinition Height="*" /&gt;
      &lt;/Grid.RowDefinitions&gt;
      &lt;StackPanel Orientation="Horizontal"&gt;
          &lt;Button Content="Login" Margin="5" Command="{Binding NavigationCommand, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" CommandParameter="LoginUser" /&gt;
          &lt;Button Content="Home" Margin="5" Command="{Binding NavigationCommand, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" CommandParameter="Home"/&gt;
          &lt;Button Content="Profile" Margin="5" Command="{Binding NavigationCommand}" CommandParameter="Profile"/&gt;
      &lt;/StackPanel&gt;
  
      &lt;ContentControl Grid.Row="1" prism:RegionManager.RegionName="ContentRegion" /&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>
  
  <p>I find this very strange. The first view works when I click the buttons at the top of the screen for navigation as long as I leave the ViewModel blank. Same with the view that has the form, if I comment out the ViewModel, it navigates with no issues, showing the form and all. But if the databinding is there in the viewmodel I get the exception. Any ideas?! I am stomped and not sure where my mistake is.</p>
  
  <p>Edit:</p>
  
  <p>Having thought about this problem and tried to move it forward, I discovered that the error comes from the fact that my LoginUserViewModel having constructor parameters is why I am having the errors, tried using unity container in the bootstrapper register LoginUserViewmodel's dependencies but I am still getting the errors, any ideas anyone?</p>