Visual Studio Designer未处理的异常

时间:2016-06-07 10:17:35

标签: c# wpf visual-studio exception designer

我有一个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,然后通过DataTemplate查看) )。

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   找不到资源&resourcedictionaries / mystyles.xaml&#39;。     在MS.Internal.AppModel.ResourcePart.GetStreamCore(FileMode模式,FileAccess访问)     在System.IO.Packaging.PackagePart.GetStream(FileMode模式,FileAccess访问)     在System.IO.Packaging.PackWebResponse.CachedResponse.GetResponseStream()     在System.IO.Packaging.PackWebResponse.GetResponseStream()     在System.IO.Packaging.PackWebResponse.get_ContentType()     在MS.Internal.WpfWebRequestHelper.GetContentType(WebResponse响应)     在MS.Internal.WpfWebRequestHelper.GetResponseStream(WebRequest请求,ContentType&amp; contentType)     在System.Windows.ResourceDictionary.set_Source(Uri值)     在System.Windows.Baml2006.WpfSharedBamlSchemaContext。&lt;&gt; c.b__342_0(对象目标,对象值)     在System.Windows.Baml2006.WpfKnownMemberInvoker.SetValue(对象实例,对象值)     在MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(XamlMember成员,对象obj,对象值)     在MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(Object inst,XamlMember属性,Object value)

     

Microsoft.Expression.DesignHost.Isolation.Remoting.RemoteException   &#39;设置属性&#39; System.Windows.ResourceDictionary.Source&#39;抛出异常。&#39;行号&#39; 17&#39;和行位置&#39; 18&#39;     在System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlReader templateReader,XamlObjectWriter currentWriter)     在System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlObjectWriter objectWriter)     在System.Windows.FrameworkTemplate.LoadOptimizedTemplateContent(DependencyObject容器,IComponentConnector componentConnector,IStyleConnector styleConnector,List`1 affectedChildren,UncommonField`1 templatedNonFeChildrenField)     在System.Windows.FrameworkTemplate.LoadContent(DependencyObject容器,List`1 affectedChildren)     在System.Windows.StyleHelper.ApplyTemplateContent(UncommonField`1 dataField,DependencyObject容器,FrameworkElementFactory templateRoot,Int32 lastChildIndex,HybridDictionary childIndexFromChildID,FrameworkTemplate frameworkTemplate)     在System.Windows.FrameworkTemplate.ApplyTemplateContent(UncommonField`1 templateDataField,FrameworkElement容器)     在System.Windows.FrameworkElement.ApplyTemplate()     在System.Windows.FrameworkElement.MeasureCore(Size availableSize)     在System.Windows.UIElement.Measure(Size availableSize)     在System.Windows.Controls.Border.MeasureOverride(大小约束)     在System.Windows.FrameworkElement.MeasureCore(Size availableSize)     在System.Windows.UIElement.Measure(Size availableSize)     在System.Windows.Controls.Grid.MeasureOverride(大小约束)     在System.Windows.FrameworkElement.MeasureCore(Size availableSize)     在System.Windows.UIElement.Measure(Size availableSize)     在MS.Internal.Helper.MeasureElementWithSingleChild(UIElement元素,大小约束)     在System.Windows.Controls.ContentPresenter.MeasureOverride(大小约束)     在System.Windows.FrameworkElement.MeasureCore(Size availableSize)     在System.Windows.UIElement.Measure(Size availableSize)     在System.Windows.Controls.Control.MeasureOverride(大小约束)     在System.Windows.FrameworkElement.MeasureCore(Size availableSize)     在System.Windows.UIElement.Measure(Size availableSize)     在System.Windows.Controls.Border.MeasureOverride(大小约束)     在System.Windows.FrameworkElement.MeasureCore(Size availableSize)     在System.Windows.UIElement.Measure(Size availableSize)     在System.Windows.ContextLayoutManager.UpdateLayout()     在System.Windows.UIElement.UpdateLayout()     在System.Windows.Interop.HwndSource.SetLayoutSize()     在System.Windows.Interop.HwndSource.set_RootVisualInternal(视觉值)     在System.Windows.Interop.HwndSource.set_RootVisual(视觉值)     在Microsoft.Expression.DesignHost.Isolation.Remoting.RemoteUIElement。&lt;&gt; c__DisplayClass12.b__11()     在Microsoft.Expression.DesignHost.Isolation.Remoting.ThreadMarshaler。&lt;&gt; c__DisplayClass16`1.b__15()     在Microsoft.Expression.DesignHost.Isolation.Remoting.STAMarshaler.Call.InvokeWorker()     在Microsoft.Expression.DesignHost.Isolation.Remoting.STAMarshaler.Call.Invoke(Boolean waitingInExternalCall)     在Microsoft.Expression.DesignHost.Isolation.Remoting.STAMarshaler.InvokeCall(来电通话)     在Microsoft.Expression.DesignHost.Isolation.Remoting.STAMarshaler.ProcessQueue(CallQueue队列)     在Microsoft.Expression.DesignHost.Isolation.Remoting.STAMarshaler.ProcessInboundAsyncQueue(Int32身份)     在Microsoft.Expression.DesignHost.Isolation.Remoting.STAMarshaler.ProcessMessage(Int32 msg,IntPtr wParam,Boolean elevatedQuery,Boolean&amp; processed)     在Microsoft.Expression.DesignHost.Isolation.Remoting.STAMarshaler.OnWindowMessage(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam,Boolean&amp; handling)     在Microsoft.Expression.DesignHost.Isolation.Remoting.MessageOnlyHwndWrapper.WndProc(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam)     在MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG&amp; msg)     在System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame框架)     在System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame框架)     在System.Windows.Application.RunDispatcher(Object ignore)     在System.Windows.Application.RunInternal(窗口窗口)     在System.Windows.Application.Run(窗口窗口)     在Microsoft.Expression.DesignHost.Isolation.IsolationProcess.RunApplication()     在Microsoft.Expression.DesignHost.Isolation.IsolationProcess。&lt;&gt; c__DisplayClass2.b__0()     在System.Threading.ThreadHelper.ThreadStart_Context(对象状态)     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)     在System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态)     在System.Threading.ThreadHelper.ThreadStart()

奇怪的是MainView本身不会添加MyStyles.xaml资源字典。 MyStyles.xaml仅在子视图ViewAViewB中添加。我想再次强调,设计师也可以无错误地渲染两个子视图。虽然这不会在运行时对我的实际应用程序造成损害,但是为了看看我的视图是什么样子,必须开始运行整个应用程序仍然很烦人。这里有人知道这里出了什么问题吗?顺便说一句,我使用的是Visual Studio Professional 2012。

1 个答案:

答案 0 :(得分:1)

你的包装看起来比我以前看到的要小......

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

所以,虽然在运行时它可能有用,但设计可能需要额外的东西,如[yourApplication]参​​考?或者声明实际样式的任何[path / subpath]。 (但没有方括号)