2016-07-07 73 views
1

在我的程序的開始屏幕中,我顯示了一個(非交互式)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,得出如下所示的簡化代碼。

此代碼編譯並運行,但parameterExecuteViewLoadedEventHandlerCommand(object parameter)Null。我也直接嘗試使用DocumentView,而不是ContentControl,但parameter仍然是Null

我不知道我在做什麼錯在這裏,因爲我已經在其他情況下,他們在那裏工作正常使用RelayCommandInteraction.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 
+0

如果您在構造函數中設置LoadingTileView的DataContext的,然後確保你做這個* *前的InitializeComponent調用() 。 –

+0

我會說,如果綁定到命令的作品DataContext在那裏,那麼不是這樣。我實際上認爲代碼看起來不錯...當你點擊ExecuteViewLoadedEventHandlerCommand時,stacktrace如何看起來可能你會發現它出現錯誤的提示... –

+0

@MarkusHütter感謝您的評論。不幸的是,這個callstack並沒有給我太多啓發。見下文。也許你有另一個建議? 'LoadingTileViewModel.ExecuteViewLoadedEventHandlerCommand(對象參數) LoadingTileViewModel..ctor.AnonymousMethod__24_0(obj對象) [外部代碼] [下面的幀可能是不正確的和/或缺失,沒有加載user32.dll中的符號]' – packoman

回答

0

事實證明,問題是,該TileViewLoaded事件的DocumentView加載的DataTemplate內之前觸發。我通過將DocumentViewModel中的另一個命令附加到DocumentViewLoaded事件中發現此問題。之後我看到了馬庫斯的回答,這實際上就是他所建議的。

最後我通過將Loaded事件的命令移動到DataTemplate來獲得我的代碼。爲此,我使用一個StackPanel綁定到其Loaded事件,讓我結束了:

<DataTemplate DataType="{x:Type ViewModels:DocumentViewModel}"> 
    <StackPanel> 
     <View:DocumentView x:Name="DocumentViewInstance"/> 
     <i:Interaction.Triggers> 
      <i:EventTrigger EventName="Loaded"> 
       <i:InvokeCommandAction Command="{Binding Path=DataContext.ViewLoadedEventHandlerCommand, 
                 RelativeSource={RelativeSource FindAncestor, 
                         AncestorType={x:Type UserControl}}}" 
             CommandParameter="{Binding ElementName=DocumentViewInstance}"/> 
      </i:EventTrigger> 
     </i:Interaction.Triggers> 
    </StackPanel> 
</DataTemplate> 
0

在您說的someinput達到命令的評論中。這意味着元素名綁定要麼完全失敗,要麼失敗。

所以:爲了測試,添加一個Button,它具有相同的i:InvokeCommandAction,但在<i:EventTrigger EventName="Click">上,您可以測試後面的綁定是否可以工作。

同時關注輸出在調試時VS的窗口。如果綁定失敗,則應該有消息。

我的假設是問題出在您的Tile控件中。我猜你的Tile控件在Loaded事件發生時尚未初始化其子元素。

+0

是的,最終情況就是如此。正如我在答案中所描述的那樣,DataTemplate正在'Tile'控制之後初始化。我想知道,這是否是「ContentControl」的默認行爲? – packoman

相關問題