C# 将参数从viewmodels传递到另一个viewmodels
当我搜索时,我发现了如何将值从viewmodel绑定到视图,而不是从viewmodel绑定到viewmodel。有人能帮我吗。我需要的是将身份验证传递给其他viewmodel。我是MVVM领域的新手,请提供更多详细信息。 我的ViewModel看起来像这样C# 将参数从viewmodels传递到另一个viewmodels,c#,mvvm,C#,Mvvm,当我搜索时,我发现了如何将值从viewmodel绑定到视图,而不是从viewmodel绑定到viewmodel。有人能帮我吗。我需要的是将身份验证传递给其他viewmodel。我是MVVM领域的新手,请提供更多详细信息。 我的ViewModel看起来像这样 public class ModelView_Authentication : INotifyPropertyChanged { //Binding authentication private Authentication
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();
}
}
公共类模型视图\u身份验证:INotifyPropertyChanged
{
//绑定身份验证
私人认证(private Authentication);;
公共身份验证
{
获取{return\u authentication;}
设置
{
_认证=价值;
不更改属性(“U认证”);
}
}
//命令按钮
公共ModelView_身份验证()
{
身份验证=新身份验证();
ButtonCommand=newviewmoddelcommand(exeMethode、canexeMethode);
}
公共ICommand按钮命令{get;set;}
公共事件属性更改事件处理程序属性更改;
私有布尔canexeMethode(对象参数)
{
返回true;
}
//在单击按钮时运行此命令
私有void方法(对象参数)
{
}
受保护的void notifayProperty已更改(字符串s)
{
PropertyChangedEventHandler pc=PropertyChanged;
if(PropertyChanged!=null)
{
PropertyChanged(即新PropertyChangedEventArgs);
}
}
//运行同步登录
公共静态异步任务主(身份验证)
{
var tocken=await Login.GetConnection(身份验证);
返回tocken.ToString();
}
}
需要将身份验证传递给其他viewmodel
您的主视图模型符合INotifyPropertyChanged,您可以让其他虚拟机订阅主虚拟机的通知过程,并根据需要获取对特定属性的更改
public static ModelView_Authentication AuthVM { get; set; }
只要参考一下主VM,就很容易了。VM从何处获得引用,这一过程取决于您 一个好地方是在
App
class上。由于App
类在每个名称空间中都是已知的,因此在其上设置一个静态属性,在创建主VM后设置它,然后根据需要访问它
public static ModelView_Authentication AuthVM { get; set; }
访问权限,例如
var mainVM = App.AuthVM;
如果存在引用,Viewmodels可以自由地相互访问(或将结果存储在共享模型中)。只要使引用可用,至少有100种方法可以传递引用。@Sinatr你能告诉我一种方法吗?我创建一个模型来处理用户身份验证、用户切换等。然后两个视图模型都需要访问它,一个用来设置状态,最好是通过模型验证方法,另一个用来读取用户权限。最简单的方法是将模型实例作为
App
property提供。@Sinatr我们可以私下谈谈吗?