将参数从视图模型传递到另一个

Pass parameter from viewmodels to another

当我搜索时,我发现如何将视图模型中的值绑定到视图而不是视图模型到 viewmodel.can 谁能帮助我做到这一点。我需要的是将身份验证传递给其他 viewmodel.I 是 MVVM 世界的新手,所以请给我更多详细信息。 我的 ViewModel 看起来像这样

public class ModelView_Authentication : INotifyPropertyChanged
{
    //Binding authentication
    private Authentication _authentication;

    public Authentication authentication
    {
        get { return _authentication; }

        set
        {
            _authentication = value;
            NotifayPropertyChanged("_authentication");
        }
    }
    
    //Command Button
    public ModelView_Authentication()
    {
        authentication = new Authentication();
        ButtonCommand = new ViewModdelCommand(exeMethode, canexeMethode);
    }

    public ICommand ButtonCommand { get; set; }

    public event PropertyChangedEventHandler PropertyChanged;

    private bool canexeMethode(Object param)
    {
        return true;
    }

    //run this Command Onclick Button
    private void exeMethode(Object param)
    {
       
    }

    protected void NotifayPropertyChanged(string s)
    {
        PropertyChangedEventHandler pc = PropertyChanged;
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(s));
        }
    }

    //Run Assync Login 
    public static async Task<string> main(Authentication authentication)
    {
        var tocken = await Login.GetConnection(authentication);
        return tocken.ToString();
    }
}

need is to pass Authentication to other viewmodel

您的主 ViewModel 遵循 INotifyPropertyChanged,您可以让其他 VM 订阅主 VM 的通知进程,并根据需要获取对特定属性的更改。


只需引用主 VM,就这么简单。 VM 在何处获得引用,该过程由您决定。

App class 上有个好地方。由于 App class 在每个命名空间中都是已知的,因此在其上设置静态 属性,在创建主 VM 后设置它,然后根据需要访问它。

public static ModelView_Authentication AuthVM { get; set; }

等访问
var mainVM = App.AuthVM;