0
votes

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:

Profile View with empty viewmodel:

<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">
<Grid>
    <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"/>
    <TextBlock x:Name="Username" HorizontalAlignment="Left" Height="26" Margin="103,96,0,0" TextWrapping="Wrap" Text="{Binding username}" VerticalAlignment="Top" Width="130"/>
    <TextBlock x:Name="LoginTime" HorizontalAlignment="Left" Margin="109,152,0,0" TextWrapping="Wrap" Text="{Binding LoginTime}" VerticalAlignment="Top" Width="124" Height="33"/>
    <Label x:Name="label" Content="Username :" HorizontalAlignment="Left" Margin="10,96,0,0" VerticalAlignment="Top" Width="71" FontWeight="Bold"/>
    <Label x:Name="label1" Content="Login-Time:" HorizontalAlignment="Left" Height="26" Margin="12,159,0,0" VerticalAlignment="Top" Width="86" FontWeight="Bold"/>

</Grid>

but it has an empty viewmodel and it loads fine.

Here is another view with a simple form that is data-bound to its viewmodel

<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">
<Grid>
    <Label x:Name="usernameLabel" Content="Username :" HorizontalAlignment="Left" Margin="10,93,0,0" VerticalAlignment="Top" Width="113" Height="35" FontSize="18.667" FontWeight="Bold"/>
    <Label x:Name="label" Content="Password :&#xD;&#xA;" HorizontalAlignment="Left" Margin="10,146,0,0" VerticalAlignment="Top" Width="100" Height="38" FontSize="18.667" FontWeight="Bold"/>
    <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}"/>
    <PasswordBox x:Name="passwordBox" HorizontalAlignment="Left" Margin="132,146,0,0" VerticalAlignment="Top" Width="178" Height="35"/>
    <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}" />
    <Button x:Name="Logoutbtn" Content="Logout" HorizontalAlignment="Left" Margin="230,208,0,0" VerticalAlignment="Top" Width="80" Height="42" FontWeight="Bold" FontSize="18.667"/>
</Grid>

Here is the viewmodel its bound to: left it very simple just to understand the concept:

 public class LoginUserViewModel : BindableBase
{
    private AuthenticationService _auth;
    public DelegateCommand<object> LoginCommand { get; set; }

    public LoginUserViewModel(AuthenticationService auth)
    {
        _auth = auth;
        LoginCommand = new DelegateCommand<object>(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); }
    }

}

And here is the MainWindowViewmodel that handles the navigation using the button at the top of my mainwindow view.

    public class MainWindowViewModel : BindableBase
{
    IRegionManager _regions;
    public DelegateCommand<string> NavigationCommand { get; set; }

    public MainWindowViewModel(IRegionManager regions)
    {
        _regions = regions;
        NavigationCommand = new DelegateCommand<string>(Navigate);
    }

    private void Navigate(string uri)
    {
        _regions.RequestNavigate("ContentRegion", uri);
    }

}

And lastly, here is the MainWindowViewModel that binds to the viewmodel:

<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">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <StackPanel Orientation="Horizontal">
        <Button Content="Login" Margin="5" Command="{Binding NavigationCommand, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" CommandParameter="LoginUser" />
        <Button Content="Home" Margin="5" Command="{Binding NavigationCommand, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" CommandParameter="Home"/>
        <Button Content="Profile" Margin="5" Command="{Binding NavigationCommand}" CommandParameter="Profile"/>
    </StackPanel>

    <ContentControl Grid.Row="1" prism:RegionManager.RegionName="ContentRegion" />
</Grid>

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.

Edit:

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?

1
You are missing vital informations. To which view are you navigating that has a ViewModel? Profile? Home? If it's profile, what's the ViewModel of your profile - Tseng
The StackOverflowException usually comes from to high or endless recursion. On the first look I don't see anything that may call recursion in your posted code, so it may be somewhere else. Minor side note, your LoginUser method is violating MVVM ;) - Tseng
Yeah I know that...sending in the password box. I chose this over databinding the password and storing in memory. And I want to click a login button at the top of the MainWindow View so it takes me to the login form and if I click the other button it should take me to the other view (profile). The profile view works without any thing in the viewmodel but the loginview just doesn't work unless I comment everthing in its ViewModel - Joseph Izang
Oh as a side note, I am using Unity Container to handle dependency injection and I am using unity to register my views for navigation... - Joseph Izang

1 Answers

0
votes

After doing some more searching and question asking, I got the answer to this problem. I spoke to the awesome @brianLagunas and shared my code with him and he made understand the problem I had. Its in a domain model which I didn't include in my post.

public class User : IUser
{
    public User()
    {

    }
    public User(User user){} //this is the problem line
    [Required]
    [Key]
    public int UserId { get; set; }
    [Required(ErrorMessage ="You must supply a Username")]
    [DataType(DataType.Text)]
    public string Username { get; private set; }
    [Required(ErrorMessage = "You must enter a valid password to Login!")]
    public SecureString Password { get; private set; }
    [DataType(DataType.Time)]
    [Required]
    public TimeSpan TimeLoggedIn { get; private set; }
    [DataType(DataType.Date)]
    public DateTime LoginHistory { get; private set; }

    public void SetUserDetails(string username, TimeSpan date, DateTime History)
    {
        if(string.IsNullOrWhiteSpace(username))
        {
            throw new ArgumentNullException("You cannot have any of the Login Fields empty..!");
        }
        Username = username;
        TimeLoggedIn = date;
        LoginHistory = History;
    }

}

The second constructor was casing an infinite loop this the stack overflow error I was getting. Removing that constructor fixed the issue and all is well.