我將視圖的構造函數中View的DataContext設置爲ViewModel的實例,只是標準的東西。此後不久,UPDATE_RECENT_DOCUMENTS_LIST事件從我的ViewModel正確捕獲的Event Aggregator觸發。屬性發生了變化,onPropertyChanged方法被調用,但是由於PropertyChanged事件爲null,它失敗了。設置DataContext後PropertyChanged事件爲空
我做的非常接下來的事情就是它提出了一個CREATE_PROJECT事件和相同的視圖模型被接收事件,除了現在的UI操作,PropertyChanged事件不再爲空,一切都按預期工作。
在註冊到PropertyChanged事件之前設置DataContext之後是否需要經過特定的時間?是否有可以等待的事件確保PropertyChanged事件不爲null?
另外,在集成Prism和使用非常方便的EventAggregator之後,我沒有遇到使用標準.NET事件的這個問題。
爲了簡潔起見,我在View和ViewModel後面顯示了我的代碼,省略了View XAML。
ToolBarView.xaml.cs:
namespace ToolBarModule
{
public partial class ToolBarView : UserControl
{
public ToolBarView(ToolBarViewModel toolBarViewModel)
{
InitializeComponent();
this.DataContext = toolBarViewModel;
}
}
}
ToolBarViewModel.cs
namespace ToolBarModule
{
public class ToolBarViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private ToolBarCommands baseCommands;
private IEventAggregator eventAggregator;
private KickStartEvent kickStartEvent;
private SubscriptionToken subscriptionToken;
private ObservableCollection<IDocumentReference> recentDocuments = new ObservableCollection<IDocumentReference>();
private ActionCommand newTest;
private ActionCommand openTest;
private ActionCommand saveTest;
private ActionCommand exitApplication;
public ToolBarViewModel(){}
public ToolBarViewModel(IEventAggregator eventAggregator)
{
this.eventAggregator = eventAggregator;
baseCommands = new ToolBarCommands(eventAggregator);
kickStartEvent = eventAggregator.GetEvent<KickStartEvent>();
subscriptionToken = kickStartEvent.Subscribe(kickStartEventHandler, ThreadOption.UIThread, true, toolBarEventHandlerFilter);
}
public ICommand NewTest
{
get
{
if (newTest == null)
{
newTest = new ActionCommand(baseCommands.NewTestAction);
}
return newTest;
}
}
public ICommand OpenTest
{
get
{
if (openTest == null)
{
openTest = new ActionCommand(baseCommands.OpenTestAction);
}
return openTest;
}
}
public ICommand SaveTest
{
get
{
if (saveTest == null)
{
saveTest = new ActionCommand(baseCommands.SaveTestAction);
}
return saveTest;
}
}
public ICommand ExitApplication
{
get
{
if (exitApplication == null)
{
exitApplication = new ActionCommand(baseCommands.ExitApplicationAction);
}
return exitApplication;
}
}
public ObservableCollection<IDocumentReference> RecentDocuments
{
get
{
return recentDocuments;
}
set
{
recentDocuments = value;
onPropertyChanged("RecentDocuments");
}
}
private void onPropertyChanged(string propertyChanged)
{
if (PropertyChanged != null)
{
PropertyChanged(this,new PropertyChangedEventArgs(propertyChanged));
}
}
private void kickStartEventHandler(KickStartEventsArgs e)
{
switch (e.EventType)
{
case KickStartEventsArgs.KickStartEventType.CREATE_PROJECT:
onPropertyChanged("RecentDocuments");
break;
case KickStartEventsArgs.KickStartEventType.UPDATE_RECENT_DOCUMENTS_LIST:
RecentDocuments.Clear();
foreach (IDocumentReference recentDocs in e.KickStartTestList)
{
RecentDocuments.Add(recentDocs);
}
onPropertyChanged("RecentDocuments");
break;
}
}
}
}
您是否在XAML中命名了您的UserControl? – saber 2012-08-09 00:19:44
提供您的UserControl的XAML。 – saber 2012-08-09 00:23:43
您是自己創建ToolBarView還是正在使用Resolve或RegionManager?....也許您需要等待控件加載到可視化樹中...也就是說,等到Loaded事件後....你可以在你的Loaded事件中設置DataContext,如果你喜歡 – 2012-08-09 00:29:23