从ViewModel关闭注册窗口 [英] Closing Registration Window from a ViewModel

查看:57
本文介绍了从ViewModel关闭注册窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的MainWindow.我有一个让用户在系统上注册的窗口.
注册过程(目前)如下所示:

I have my MainWindow. I have a Window that lets users register on the system.
The register process (at the moment) looks like this:

    #region RegisterCommand
    
    private DelegateCommand _registerCommand;
    public ICommand RegisterCommand
    {
        get
        {
            _registerCommand = new DelegateCommand(param => Register());
            return _registerCommand;
        }
    }
    
    private bool CanRegister()
    {
        return true;
    }
    
    private void Register()
    {
        var newUser = new User
        {
            FirstName = _firstname,
            LastName = _lastname,
            Username = _username,
            Password = "", // TODO: Hashing and storing of passwords
        };
        using (var context = new WorkstreamContext())
        {
            var users = context.Set<User>();
            users.Add(newUser);
            context.SaveChanges();
        }
    }
    
    #endregion

在注册过程结束时,我希望能够关闭注册窗口,而不是主窗口.我看到过类似问题的其他答案,但发现它们令人困惑,例如,我并没有真正得到他们的建议 实现接口.

在注册过程结束时,是否有一种清晰,简单的方法可以从ViewModel关闭视图(窗口)?


At the end of the Register process I want to be able to close the register window, not the main window. I have seen other answers posted to similar questions but found them confusing and I didn't really get anyway with the suggestions they made, for example implementing an interface.

Is there a clear, simple way to close a View (Window) from a ViewModel at the end of my Registering process?


推荐答案

如果要关闭命令中的窗口,则需要将此窗口引用作为命令参数传递.

If you want to close the a window in command, you need pass this window reference as a command parameter.

public partial class MainWindow : Window { public MainWindow () { InitializeComponent(); MainWindowViewModel vm = new MainWindowViewModel(); this.DataContext = vm; } } public class MainWindowViewModel { private ICommand _openRegisterWindowCommand; public ICommand OpenRegisterWindowCommand { get { if (_openRegisterWindowCommand == null) { _openRegisterWindowCommand = new RelayCommand( OpenRegisterWindow, obj => true); } return _openRegisterWindowCommand; } } public void OpenRegisterWindow(object src) { RegisterWindow registerWindow = new RegisterWindow(); registerWindow.DataContext = this; registerWindow.Show(); } private ICommand _closeRegisterWindowCommand; public ICommand CloseRegisterWindowCommand { get { if (_closeRegisterWindowCommand == null) { _closeRegisterWindowCommand = new RelayCommand( CloseRegisterWindow, obj => true); } return _closeRegisterWindowCommand; } } public void CloseRegisterWindow(object src) { if(src!=null) {

//添加到数据库

//......

//关闭注册"窗口 (src为RegisterWindow).Close(); } } } 公共类RelayCommand:ICommand { 私有谓词< object> _canExecute; 私有动作<对象> _执行; 公共RelayCommand(Action< object>执行,谓词< object> canExecute) { this._canExecute = canExecute; this._execute =执行; } 公共事件EventHandler CanExecuteChanged { 添加{CommandManager.RequerySuggested + =值; } 删除{CommandManager.RequerySuggested-=值; } } 公共布尔CanExecute(对象参数) { 返回_canExecute(参数); } 公共无效执行(对象参数) { _execute(参数); } }

//close Register window (src as RegisterWindow).Close(); } } } public class RelayCommand : ICommand { private Predicate<object> _canExecute; private Action<object> _execute; public RelayCommand(Action<object> execute, Predicate<object> canExecute) { this._canExecute = canExecute; this._execute = execute; } public event EventHandler CanExecuteChanged { add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } } public bool CanExecute(object parameter) { return _canExecute(parameter); } public void Execute(object parameter) { _execute(parameter); } }

MainWindow XAML:

MainWindow XAML:

    <Grid>
        <Button Content="Register" Height="29" Width="100" Command="{Binding OpenRegisterWindowCommand}"/>
    </Grid>

注册窗口XAML:

<StackPanel>
        <BulletDecorator Margin="10" Height="29" FlowDirection="LeftToRight" VerticalAlignment="Center" HorizontalAlignment="Center">
            <BulletDecorator.Bullet>
                <Label FontSize="12"  FlowDirection="LeftToRight">Account Name:</Label>
            </BulletDecorator.Bullet>
            <BulletDecorator.Child>
                <TextBox FlowDirection="LeftToRight" Width="126.378" />
            </BulletDecorator.Child>
        </BulletDecorator>
        <BulletDecorator Margin="38,10,10,10" Height="29" FlowDirection="LeftToRight" VerticalAlignment="Center" HorizontalAlignment="Center">
            <BulletDecorator.Bullet>
                <Label FontSize="12"  FlowDirection="LeftToRight">Password:</Label>
            </BulletDecorator.Bullet>
            <BulletDecorator.Child>
                <TextBox FlowDirection="LeftToRight" Width="126.378" />
            </BulletDecorator.Child>
        </BulletDecorator>
        <Button Margin="10" Height="29" Width="100" Content="Submit" Command="{Binding CloseRegisterWindowCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}"/>
    </StackPanel>

希望这对您有帮助.

最好的问候,

鲍勃


这篇关于从ViewModel关闭注册窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆