Visual Studio 设计器未处理的异常

Visual Studio Designer unhandled exception

我有一个 UserControl 视图 (MainView),其中包含多个 UserControl 子视图(ViewAViewB 等)。在ViewA中,唯一的内容集是一个TabControl,也是MVVM方式实现的。

<-- ViewA.xaml -->
<TabControl ItemsSource="{Binding MyTabList}" ...
<TabControl.Resources>
    <DataTemplate DataType="{x:Type vm:ViewModelA}">
        <view:ViewA />
    </DataTemplate>
    <DataTemplate DataType="{x:Type vm:ViewModelB}">
        <view:ViewB />
    </DataTemplate>
</TabControl.Resources>

// ViewModelA.cs
private ObservableCollection<ViewModelBase> myTabList;
public ObservableCollection<ViewModelBase> MyTabList
{
    get
    {
        if (this.myTabList == null)
            this.myTabList = new ObservableCollection<ViewModelBase>();
        return this.myTabList;
    }
}

所以在这种情况下,我的 TabControl 使用 View-First 方法实现 MVVM(通过将 View 的 DataContext 设置为 ViewModel),而每个选项卡使用 ViewModel-Fist 方法实现 MVVM(首先创建 ViewModel,然后创建 View通过数据模板)。

ViewAViewB 中,我有这个:

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/ResourceDictionaries/MyStyles.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>

一切运行在调试模式和发布模式下都完美无缺,除了我的设计器出现故障。当我打开 ViewA.xaml 时,设计器渲染正常。同样对于ViewB.xaml,设计者渲染它没有任何问题。但是,当我打开 MainView.xaml 时,我的设计器崩溃并显示以下内容:

Microsoft.Expression.DesignHost.Isolation.Remoting.RemoteException Cannot locate resource 'resourcedictionaries/mystyles.xaml'. at MS.Internal.AppModel.ResourcePart.GetStreamCore(FileMode mode, FileAccess access) at System.IO.Packaging.PackagePart.GetStream(FileMode mode, FileAccess access) at System.IO.Packaging.PackWebResponse.CachedResponse.GetResponseStream() at System.IO.Packaging.PackWebResponse.GetResponseStream() at System.IO.Packaging.PackWebResponse.get_ContentType() at MS.Internal.WpfWebRequestHelper.GetContentType(WebResponse response) at MS.Internal.WpfWebRequestHelper.GetResponseStream(WebRequest request, ContentType& contentType) at System.Windows.ResourceDictionary.set_Source(Uri value) at System.Windows.Baml2006.WpfSharedBamlSchemaContext.<>c.b__342_0(Object target, Object value) at System.Windows.Baml2006.WpfKnownMemberInvoker.SetValue(Object instance, Object value) at MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(XamlMember member, Object obj, Object value) at MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(Object inst, XamlMember property, Object value)

Microsoft.Expression.DesignHost.Isolation.Remoting.RemoteException 'Set property 'System.Windows.ResourceDictionary.Source' threw an exception.' Line number '17' and line position '18'. at System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlReader templateReader, XamlObjectWriter currentWriter) at System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlObjectWriter objectWriter) at System.Windows.FrameworkTemplate.LoadOptimizedTemplateContent(DependencyObject container, IComponentConnector componentConnector, IStyleConnector styleConnector, List `1 affectedChildren, UncommonField `1 templatedNonFeChildrenField) at System.Windows.FrameworkTemplate.LoadContent(DependencyObject container, List`1 affectedChildren) at System.Windows.StyleHelper.ApplyTemplateContent(UncommonField`1 dataField, DependencyObject container, FrameworkElementFactory templateRoot, Int32 lastChildIndex, HybridDictionary childIndexFromChildID, FrameworkTemplate frameworkTemplate) at System.Windows.FrameworkTemplate.ApplyTemplateContent(UncommonField`1 templateDataField, FrameworkElement container) at System.Windows.FrameworkElement.ApplyTemplate() at System.Windows.FrameworkElement.MeasureCore(Size availableSize) at System.Windows.UIElement.Measure(Size availableSize) at System.Windows.Controls.Border.MeasureOverride(Size constraint) at System.Windows.FrameworkElement.MeasureCore(Size availableSize) at System.Windows.UIElement.Measure(Size availableSize) at System.Windows.Controls.Grid.MeasureOverride(Size constraint) at System.Windows.FrameworkElement.MeasureCore(Size availableSize) at System.Windows.UIElement.Measure(Size availableSize) at MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint) at System.Windows.Controls.ContentPresenter.MeasureOverride(Size constraint) at System.Windows.FrameworkElement.MeasureCore(Size availableSize) at System.Windows.UIElement.Measure(Size availableSize) at System.Windows.Controls.Control.MeasureOverride(Size constraint) at System.Windows.FrameworkElement.MeasureCore(Size availableSize) at System.Windows.UIElement.Measure(Size availableSize) at System.Windows.Controls.Border.MeasureOverride(Size constraint) at System.Windows.FrameworkElement.MeasureCore(Size availableSize) at System.Windows.UIElement.Measure(Size availableSize) at System.Windows.ContextLayoutManager.UpdateLayout() at System.Windows.UIElement.UpdateLayout() at System.Windows.Interop.HwndSource.SetLayoutSize() at System.Windows.Interop.HwndSource.set_RootVisualInternal(Visual value) at System.Windows.Interop.HwndSource.set_RootVisual(Visual value) at Microsoft.Expression.DesignHost.Isolation.Remoting.RemoteUIElement.<>c__DisplayClass12.b__11() at Microsoft.Expression.DesignHost.Isolation.Remoting.ThreadMarshaler.<>c__DisplayClass16`1.b__15() at Microsoft.Expression.DesignHost.Isolation.Remoting.STAMarshaler.Call.InvokeWorker() at Microsoft.Expression.DesignHost.Isolation.Remoting.STAMarshaler.Call.Invoke(Boolean waitingInExternalCall) at Microsoft.Expression.DesignHost.Isolation.Remoting.STAMarshaler.InvokeCall(Call call) at Microsoft.Expression.DesignHost.Isolation.Remoting.STAMarshaler.ProcessQueue(CallQueue queue) at Microsoft.Expression.DesignHost.Isolation.Remoting.STAMarshaler.ProcessInboundAsyncQueue(Int32 identity) at Microsoft.Expression.DesignHost.Isolation.Remoting.STAMarshaler.ProcessMessage(Int32 msg, IntPtr wParam, Boolean elevatedQuery, Boolean& handled) at Microsoft.Expression.DesignHost.Isolation.Remoting.STAMarshaler.OnWindowMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at Microsoft.Expression.DesignHost.Isolation.Remoting.MessageOnlyHwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam) at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg) at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame) at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame) at System.Windows.Application.RunDispatcher(Object ignore) at System.Windows.Application.RunInternal(Window window) at System.Windows.Application.Run(Window window) at Microsoft.Expression.DesignHost.Isolation.IsolationProcess.RunApplication() at Microsoft.Expression.DesignHost.Isolation.IsolationProcess.<>c__DisplayClass2.b__0() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart()

奇怪的是MainView本身并没有添加MyStyles.xaml资源字典。 MyStyles.xaml 仅在子视图 ViewAViewB 中添加。我想再次强调,设计者也可以无误地渲染两个子视图。虽然这不会在 运行 时影响我的实际应用程序,但不得不启动 运行 整个应用程序只是为了看看我的视图是什么样子,这仍然很烦人。这里的任何人都知道这里出了什么问题吗?顺便说一下,我使用的是 Visual Studio Professional 2012。

你的背包看起来和我以前看到的有点不一样...

pack://application:,,,/[yourApplication];component/[path within your project]/[any subpath]/MyStyles.xaml"

所以,虽然在 运行-time 它可能会工作,但设计可能需要一些额外的东西,例如 [yourApplication] 参考?或任何 [path/subpath] 到声明实际样式的位置。 (但没有方括号)