【问题标题】:Prism 6 Navigation WierdnessPrism 6 导航怪异度
【发布时间】:2015-12-26 23:29:20
【问题描述】:

我还在学习棱镜,我想了解导航。在阅读了手册并按照@brianlagunas MVVM 在线制作了简单的网络研讨会后,我决定尝试一个小项目来看看。当我尝试导航到视图模型数据绑定到视图上的某些控件的视图时,出现 stackoverflow 异常。下面是代码:

带有空视图模型的配置文件视图:

<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>

但它有一个空视图模型,并且加载正常。

这是另一个视图,它有一个简单的表单,数据绑定到它的视图模型

<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>

这里是它绑定的视图模型:让它很简单,只是为了理解这个概念:

 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); }
    }

}

这是使用主窗口视图顶部的按钮处理导航的 MainWindowViewmodel。

    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);
    }

}

最后,这里是绑定到视图模型的 MainWindowViewModel:

<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>

我觉得这很奇怪。只要我将 ViewModel 留空,当我单击屏幕顶部的按钮进行导航时,第一个视图就会起作用。与具有表单的视图相同,如果我注释掉 ViewModel,它会毫无问题地导航,显示表单和所有内容。但如果数据绑定存在于视图模型中,我会得到异常。有任何想法吗?!我被跺了跺脚,不知道我的错误在哪里。

编辑:

考虑过这个问题并尝试将其向前推进,我发现错误来自这样一个事实,即我的 LoginUserViewModel 具有构造函数参数是我遇到错误的原因,尝试在引导程序注册 LoginUserViewmodel 的依赖项中使用统一容器,但我仍然收到错误,有什么想法吗?

【问题讨论】:

  • 您缺少重要信息。您要导航到具有 ViewModel 的 哪个 视图?轮廓?家?如果是个人资料,您的个人资料的 ViewModel 是什么
  • StackOverflowException 通常来自高递归或无限递归。乍一看,我在您发布的代码中看不到任何可能调用递归的东西,所以它可能在其他地方。次要说明,您的 LoginUser 方法违反了 MVVM ;)
  • 是的,我知道...在密码框中发送。我选择了这个而不是数据绑定密码并存储在内存中。我想单击 MainWindow 视图顶部的登录按钮,以便将我带到登录表单,如果我单击另一个按钮,它应该将我带到另一个视图(配置文件)。配置文件视图在视图模型中没有任何内容,但登录视图不起作用,除非我在其视图模型中评论所有内容
  • 顺便说一句,我正在使用 Unity Container 来处理依赖注入,我正在使用 unity 注册我的导航视图...

标签: c# wpf mvvm prism


【解决方案1】:

在进行了更多搜索和提问之后,我得到了这个问题的答案。我与很棒的@brianLagunas 交谈并与他分享了我的代码,他理解了我遇到的问题。它在我的帖子中没有包含的域模型中。

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;
    }

}

第二个构造函数封装了一个无限循环,这是我得到的堆栈溢出错误。删除该构造函数解决了问题,一切都很好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-27
    • 1970-01-01
    • 2018-04-08
    相关资源
    最近更新 更多