![](/img/trans.png)
[英]How to update list<> from another View model on same View in WPF using Caliburn.Micro MVVM?
[英]Get info from another window in Caliburn.Micro, WPF, MVVM
我在基於Caliburn.Micro mvvm框架的應用程序中制作了登錄窗口。 那么,如何返回一個屬性(例如, true
如果用戶通過良好的數據或false
,如果他傳球不好的憑證)從TryClose()
從我的登錄窗口,是Caliburn.Micro初始化方法? 如何從IWindowManager.ShowDialog()
打開的窗口獲取信息?
首先,我的MainWindowViewModel.cs :
using Caliburn.Micro;
namespace TaskManager.ViewModels
{
class MainWindowViewModel : Conductor<IScreen>.Collection.OneActive
{
protected override void OnViewLoaded(object view)
{
IWindowManager manager = new WindowManager();
//Login page, context is data with user's lists
LoginViewModel loginView = new LoginViewModel(context);
manager.ShowDialog(loginView, null, null);
//here i want to get info, if i get logged properly or not
}
public void LoadUserInfoPage() //here starts "main" program
{
ActivateItem(new UserInfoViewModel());
}
//and so on...
}
}
我的LoginViewModel.cs :
namespace TaskManager.ViewModels
{
class LoginViewModel : Screen
{
public string Login { get; set; }
public string Password { get; set; }
public LoginViewModel(FakeData context)
{
this.context = context;
}
public void LoginButton()
{
bool check = Services.Login.IsValid(Login, Password, context);
if(check) //if login is OK, check == true
TryClose();
}
private FakeData context { get; set; } //data is here
}
}
然后,我的IsValid()
方法:
namespace TaskManager.Services
{
static class Login
{
static public bool IsValid(string login, string password, FakeData context)
=> context.users.Any(i => i.Login == login);
//i know it is bad, but its only example
}
}
按鈕,打開和關閉窗口效果很好(也可以從文本框中讀取)。 如果用戶傳遞了良好的數據,我只希望獲取信息(也許是通過引用?)。
謝謝您的建議!
您可以為此目的使用EventAggregator。
“事件聚合器是一種服務,它能夠以基於松散的方式將對象從一個實體發布到另一個實體。”
第一步是在ViewModels中創建EventAggregator實例並進行訂閱。 您可以在兩個ViewModels的構造函數中通過DI進行操作。
對於LoginViewModel,
private IEventAggregator _eventAggregator;
public LoginViewModel(FakeData context,IEventAggregator eventAggregator)
{
_eventAggregator = eventAggregator;
}
還有MainWindowViewModel,
private IEventAggregator _eventAggregator;
public MainWindowViewModel (IEventAggregator eventAggregator)
{
_eventAggregator = eventAggregator;
_eventAggregator.Subscribe(this);
}
下一步是創建一個消息對象,該對象可以在ViewModel之間傳輸所需的信息。
public class OnLoginAttemptMessage
{
string UserName { get; set; }
bool IsLoginSuccessful { get; set; }
}
最后,是時候將所有內容放在一起了。 在LoginViewModel的youg LoginButton方法中,我們修改代碼以在成功登錄時引發事件。
public void LoginButton()
{
bool check = Services.Login.IsValid(Login, Password, context);
if(check) //if login is OK, check == true
{
_eventAggregator.PublishOnUIThread(new OnLoginAttemptMessage
{
UserName = Login,
IsLoginSuccessful = check;
});
TryClose();
}
}
最后一步是在MainWindowViewModel中,您需要在其中實現IHandle接口。
class MainWindowViewModel : Conductor<IScreen>.Collection.OneActive, IHandle<OnLoginSuccessMessage>
{
public void Handle(OnLoginSuccessMessage message)
{
if(message.IsLoginSuccessful)
{
// Login is successfull, do next steps.
}
}
}
您可以在此處( https://caliburnmicro.com/documentation/event-aggregator )閱讀更多有關EventAggregator的信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.