2016-07-07 75 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

相关问题