[英]Binding a View to a ViewModel, without an “empty” constructor
繼續上一個問題“ 更新/使用另一個ViewModel中的變量 ”,我決定開始使用Caliburn Micro作為框架。
我根據此指南http://www.mindscapehq.com/blog/index.php/2012/02/01/caliburn-micro-part-4-the-event-aggregator/設置了事件匯總器。
事實是,根據本指南,不應有帶有0個參數的“空”構造函數。
好吧
現在的問題是,我現在不知道如何將ViewModel綁定到View。 在切換到此框架之前,我使用App.xaml和Static資源作為datacontext的資源,但由於沒有空的構造函數,因此無法再執行此操作。
我該如何解決這個問題? 我已經嘗試解決了大約一個小時,而我卻一無所獲。
一些代碼:
[Export(typeof(ViewModelBase))]
public class ViewModelBase : INotifyPropertyChanged, IHandle<updateEvent>
{
private Class _studclass;
public AddStudentViewModel NewModel { get; private set; }
public Class StudentClass
{
get { return _studclass; }
set
{
_studclass = value;
NotifyPropertyChanged("StudentClass");
}
}
[ImportingConstructor]
public ViewModelBase(AddStudentViewModel newModel, IEventAggregator events)
{
StudentClass = new Class();
NewModel = newModel;
Student asaf = new Student();
asaf.Name = "Asaf";
StudentClass.StudentList.Add(asaf);
events.Subscribe(this);
}
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(string PropertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
}
}
public void Handle(updateEvent msg)
{
StudentClass.StudentList.Add(msg.Student);
}
}
這就是“主要”視圖模型。 但是,我無法將其綁定到視圖,因此數據不會顯示...我什至試圖設置一個偽造的數據...效果不佳,您可能已經猜到了。
Caliburn.Micro使用約定。 因此,如果ViewModels文件夾中有ShellViewModel
,它將在Views文件夾中尋找ShellView
。
我建議您看一下樣本和文檔 。 一個不錯的起點是安裝Caliburn.Micro.Start NuGet軟件包(將其導入Caliburn.Micro),然后按照文檔中的說明編輯App.xaml文件。
從本質上講,引導程序是導致其實例化的應用程序資源,該資源隨后通過配置的IoC容器解析您的外殼視圖模型,找到相應的外殼視圖,進行綁定並使用Caliburn.Micro窗口管理器顯示它。
您也不需要在視圖模型基類上實現INotifyPropertyChanged
。 Caliburn.Micro包括PropertyChangedBase
類型, Screen
類型和Conductor
類型。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.