在我的程序的開始屏幕中,我顯示了一個(非交互式)UserControl DocumentView
的縮略圖,以在Tile-like UserControl LoadTileView
內顯示不同的文檔,用戶可以點擊將文檔加載到主視圖中。這個DocumentView
也用於主視圖中以圖形方式表示和編輯所選文檔。由於這個圖形很重,加載需要一些時間,這會導致啓動時間很長,因爲我在開始屏幕中顯示了DocumentView
的多個實例(例如,用戶可以從中選擇哪些Tiles,要編輯哪個文檔)。使用MVVM-Light中的RelayCommand將UIElement傳遞給ViewModel
因此,我在工作的方式來保存屬於每個LoadingTileView
,這樣可以證明,而不是實際DocumentView
,當我的程序啓動下一次DocumentView
實例的緩存圖片。
我目前正在研究如何保存緩存圖像。我的想法是讓每個LoadTileView
在加載後通過MVVM-Light RelayCommand
調用其LoadingTileViewModel
,並將其實例DocumentView
傳遞給該命令。我把DocumentView
放在一個DataTemplate中,這樣我就可以用它的緩存圖像(和相應的View)替換它,一旦它存在,在LoadingTileView
中相應地設置CurrentDocumentViewModel
。
我已經找到了如何爲UIElement
傳遞給視圖模型(here)以及如何與參數(here)使用RelayCommand
的解釋。結合攪拌機交互觸發器來觸發ViewModel中的ViewLoadedEventHandlerCommand
,得出如下所示的簡化代碼。
此代碼編譯並運行,但parameter
在ExecuteViewLoadedEventHandlerCommand(object parameter)
是Null
。我也直接嘗試使用DocumentView
,而不是ContentControl
,但parameter
仍然是Null
。
我不知道我在做什麼錯在這裏,因爲我已經在其他情況下,他們在那裏工作正常使用RelayCommand
和Interaction.Triggers
這一點。也許有人可以發現我的錯誤?
代碼LoadingTileView:
<UserControl.Resources>
<DataTemplate DataType="{x:Type ViewModels:DocumentViewModel}">
<View:DocumentView/>
</DataTemplate>
</UserControl.Resources>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<i:InvokeCommandAction Command="{Binding ViewLoadedEventHandlerCommand}"
CommandParameter="{Binding ElementName=DocumentViewInstance}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<Controls:Tile Command="{Binding LoadProgramCommand}">
<Viewbox>
<ContentControl x:Name="DocumentViewInstance" Content="{Binding CurrentDocumentViewModel}"/>
</Viewbox>
</Controls:Tile>
代碼LoadingTileViewModel:
public LoadingTileViewModel()
{
...
ViewLoadedEventHandlerCommand = new RelayCommand<object>((obj)=>ExecuteViewLoadedEventHandlerCommand(obj));
...
}
public RelayCommand<object> ViewLoadedEventHandlerCommand { get; set; }
private void ExecuteViewLoadedEventHandlerCommand(object parameter) // object is NULL
{
UIElement toSave = (UIElement)parameter;
//OnViewLoaded();
}
更新:
堆棧跟蹤,在線UIElement toSave = (UIElement)parameter;
突破時:
Software.UI.ViewModel.dll!ProgramEditor.ViewModel.LoadingTileViewModel.ExecuteViewLoadedEventHandlerCommand(object parameter) Line 168 C#
Software.UI.ViewModel.dll!ProgramEditor.ViewModel.LoadingTileViewModel.get_ViewLoadedEventHandlerCommand.AnonymousMethod__50_0(object i) Line 184 C#
[Native to Managed Transition]
[Managed to Native Transition]
GalaSoft.MvvmLight.dll!GalaSoft.MvvmLight.Helpers.WeakAction<System.__Canon>.Execute(System.__Canon parameter) Unknown
GalaSoft.MvvmLight.dll!GalaSoft.MvvmLight.Command.RelayCommand<object>.Execute(object parameter) Unknown
System.Windows.Interactivity.dll!System.Windows.Interactivity.InvokeCommandAction.Invoke(object parameter) Unknown
System.Windows.Interactivity.dll!System.Windows.Interactivity.TriggerBase.InvokeActions(object parameter) Unknown
System.Windows.Interactivity.dll!System.Windows.Interactivity.EventTriggerBase.OnEvent(System.EventArgs eventArgs) Unknown
System.Windows.Interactivity.dll!System.Windows.Interactivity.EventTriggerBase.OnEventImpl(object sender, System.EventArgs eventArgs) Unknown
PresentationCore.dll!System.Windows.RoutedEventHandlerInfo.InvokeHandler(object target, System.Windows.RoutedEventArgs routedEventArgs) Unknown
PresentationCore.dll!System.Windows.EventRoute.InvokeHandlersImpl(object source, System.Windows.RoutedEventArgs args, bool reRaised) Unknown
PresentationCore.dll!System.Windows.UIElement.RaiseEventImpl(System.Windows.DependencyObject sender, System.Windows.RoutedEventArgs args) Unknown
PresentationCore.dll!System.Windows.UIElement.RaiseEvent(System.Windows.RoutedEventArgs e) Unknown
PresentationFramework.dll!System.Windows.BroadcastEventHelper.BroadcastEvent(System.Windows.DependencyObject root, System.Windows.RoutedEvent routedEvent) Unknown
PresentationFramework.dll!System.Windows.BroadcastEventHelper.BroadcastLoadedEvent(object root) Unknown
PresentationCore.dll!MS.Internal.LoadedOrUnloadedOperation.DoWork() Unknown
PresentationCore.dll!System.Windows.Media.MediaContext.FireLoadedPendingCallbacks() Unknown
PresentationCore.dll!System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks() Unknown
PresentationCore.dll!System.Windows.Media.MediaContext.RenderMessageHandlerCore(object resizedCompositionTarget) Unknown
PresentationCore.dll!System.Windows.Media.MediaContext.RenderMessageHandler(object resizedCompositionTarget) Unknown
PresentationCore.dll!System.Windows.Media.MediaContext.Resize(System.Windows.Media.ICompositionTarget resizedCompositionTarget) Unknown
PresentationCore.dll!System.Windows.Interop.HwndTarget.OnResize() Unknown
PresentationCore.dll!System.Windows.Interop.HwndTarget.HandleMessage(MS.Internal.Interop.WindowMessage msg, System.IntPtr wparam, System.IntPtr lparam) Unknown
PresentationCore.dll!System.Windows.Interop.HwndSource.HwndTargetFilterMessage(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam, ref bool handled) Unknown
WindowsBase.dll!MS.Win32.HwndWrapper.WndProc(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam, ref bool handled) Unknown
WindowsBase.dll!MS.Win32.HwndSubclass.DispatcherCallbackOperation(object o) Unknown
WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate callback, object args, int numArgs) Unknown
WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.TryCatchWhen(object source, System.Delegate callback, object args, int numArgs, System.Delegate catchHandler) Unknown
WindowsBase.dll!System.Windows.Threading.Dispatcher.LegacyInvokeImpl(System.Windows.Threading.DispatcherPriority priority, System.TimeSpan timeout, System.Delegate method, object args, int numArgs) Unknown
WindowsBase.dll!MS.Win32.HwndSubclass.SubclassWndProc(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam) Unknown
[Native to Managed Transition]
user32.dll!74d862fa() Unknown
[Frames below may be incorrect and/or missing, no symbols loaded for user32.dll]
user32.dll!74d86d3a() Unknown
user32.dll!74d86ded() Unknown
user32.dll!74d86e4c() Unknown
ntdll.dll!773a011a() Unknown
user32.dll!74d872c1() Unknown
user32.dll!74dad4ff() Unknown
user32.dll!74d862fa() Unknown
user32.dll!74d86d3a() Unknown
user32.dll!74d90d37() Unknown
user32.dll!74d90d5d() Unknown
WindowsBase.ni.dll!6b4a6e70() Unknown
[Managed to Native Transition]
WindowsBase.dll!MS.Win32.HwndSubclass.DefWndProcWrapper(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam) Unknown
[Native to Managed Transition]
user32.dll!74d862fa() Unknown
user32.dll!74d86d3a() Unknown
user32.dll!74d90d37() Unknown
user32.dll!74d90d5d() Unknown
WindowsBase.ni.dll!6b4ced1a() Unknown
[Managed to Native Transition]
WindowsBase.dll!MS.Win32.HwndSubclass.SubclassWndProc(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam) Unknown
[Native to Managed Transition]
user32.dll!74d862fa() Unknown
user32.dll!74d86d3a() Unknown
user32.dll!74d86ded() Unknown
user32.dll!74d86ded() Unknown
user32.dll!74d88fb7() Unknown
ntdll.dll!773a011a() Unknown
user32.dll!74d92832() Unknown
user32.dll!74d92802() Unknown
user32.dll!74d9289a() Unknown
[Managed to Native Transition]
MahApps.Metro.dll!Standard.NativeMethods.SetWindowRgn(System.IntPtr hWnd, System.IntPtr hRgn, bool bRedraw) Unknown
MahApps.Metro.dll!Microsoft.Windows.Shell.WindowChromeWorker._SetRoundingRegion(Standard.WINDOWPOS? wp) Unknown
MahApps.Metro.dll!Microsoft.Windows.Shell.WindowChromeWorker._UpdateFrameState(bool force) Unknown
MahApps.Metro.dll!Microsoft.Windows.Shell.WindowChromeWorker._ApplyNewCustomChrome() Unknown
MahApps.Metro.dll!Microsoft.Windows.Shell.WindowChromeWorker._WindowSourceInitialized(object sender, System.EventArgs e) Unknown
PresentationFramework.dll!System.Windows.Window.OnSourceInitialized(System.EventArgs e) Unknown
PresentationFramework.dll!System.Windows.Window.CreateSourceWindow(bool duringShow) Unknown
PresentationFramework.dll!System.Windows.Window.CreateSourceWindowDuringShow() Unknown
PresentationFramework.dll!System.Windows.Window.SafeCreateWindowDuringShow() Unknown
PresentationFramework.dll!System.Windows.Window.ShowHelper(object booleanBox) Unknown
WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate callback, object args, int numArgs) Unknown
WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.TryCatchWhen(object source, System.Delegate callback, object args, int numArgs, System.Delegate catchHandler) Unknown
WindowsBase.dll!System.Windows.Threading.DispatcherOperation.InvokeImpl() Unknown
WindowsBase.dll!System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(object state) Unknown
WindowsBase.dll!MS.Internal.CulturePreservingExecutionContext.CallbackWrapper(object obj) Unknown
mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) Unknown
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) Unknown
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) Unknown
WindowsBase.dll!MS.Internal.CulturePreservingExecutionContext.Run(MS.Internal.CulturePreservingExecutionContext executionContext, System.Threading.ContextCallback callback, object state) Unknown
WindowsBase.dll!System.Windows.Threading.DispatcherOperation.Invoke() Unknown
WindowsBase.dll!System.Windows.Threading.Dispatcher.ProcessQueue() Unknown
WindowsBase.dll!System.Windows.Threading.Dispatcher.WndProcHook(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam, ref bool handled) Unknown
WindowsBase.dll!MS.Win32.HwndWrapper.WndProc(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam, ref bool handled) Unknown
WindowsBase.dll!MS.Win32.HwndSubclass.DispatcherCallbackOperation(object o) Unknown
WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate callback, object args, int numArgs) Unknown
WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.TryCatchWhen(object source, System.Delegate callback, object args, int numArgs, System.Delegate catchHandler) Unknown
WindowsBase.dll!System.Windows.Threading.Dispatcher.LegacyInvokeImpl(System.Windows.Threading.DispatcherPriority priority, System.TimeSpan timeout, System.Delegate method, object args, int numArgs) Unknown
WindowsBase.dll!MS.Win32.HwndSubclass.SubclassWndProc(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam) Unknown
[Native to Managed Transition]
user32.dll!74d862fa() Unknown
user32.dll!74d86d3a() Unknown
user32.dll!74d877d3() Unknown
user32.dll!74d8789a() Unknown
WindowsBase.ni.dll!6b4ceff4() Unknown
[Managed to Native Transition]
WindowsBase.dll!System.Windows.Threading.Dispatcher.PushFrameImpl(System.Windows.Threading.DispatcherFrame frame) Unknown
WindowsBase.dll!System.Windows.Threading.Dispatcher.PushFrame(System.Windows.Threading.DispatcherFrame frame) Unknown
PresentationFramework.dll!System.Windows.Application.RunDispatcher(object ignore) Unknown
PresentationFramework.dll!System.Windows.Application.RunInternal(System.Windows.Window window) Unknown
PresentationFramework.dll!System.Windows.Application.Run(System.Windows.Window window) Unknown
PresentationFramework.dll!System.Windows.Application.Run() Unknown
ProgramEditor.exe!ProgramEditor.App.Main() C#
[Native to Managed Transition]
mscoreei.dll!7143cc0b() Unknown
mscoree.dll!714b7f16() Unknown
mscoree.dll!714b4de3() Unknown
kernel32.dll!76d6338a() Unknown
ntdll.dll!773c9902() Unknown
ntdll.dll!773c98d5() Unknown
如果您在構造函數中設置LoadingTileView的DataContext的,然後確保你做這個* *前的InitializeComponent調用() 。 –
我會說,如果綁定到命令的作品DataContext在那裏,那麼不是這樣。我實際上認爲代碼看起來不錯...當你點擊ExecuteViewLoadedEventHandlerCommand時,stacktrace如何看起來可能你會發現它出現錯誤的提示... –
@MarkusHütter感謝您的評論。不幸的是,這個callstack並沒有給我太多啓發。見下文。也許你有另一個建議? 'LoadingTileViewModel.ExecuteViewLoadedEventHandlerCommand(對象參數) LoadingTileViewModel..ctor.AnonymousMethod__24_0(obj對象) [外部代碼] [下面的幀可能是不正確的和/或缺失,沒有加載user32.dll中的符號]' – packoman