【问题标题】:Visual Studio Designer unhandled exceptionVisual Studio 设计器未处理的异常
【发布时间】:2016-06-07 10:17:35
【问题描述】:

我有一个 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”。 在 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& contentType) 在 System.Windows.ResourceDictionary.set_Source(Uri 值) 在 System.Windows.Baml2006.WpfSharedBamlSchemaContext.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 property, Object value)

Microsoft.Expression.DesignHost.Isolation.Remoting.RemoteException '设置属性'System.Windows.ResourceDictionary.Source' 引发异常。行号“17”和行位置“18”。 在 System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlReader templateReader,XamlObjectWriter currentWriter) 在 System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlObjectWriter objectWriter) 在 System.Windows.FrameworkTemplate.LoadOptimizedTemplateContent(DependencyObject 容器,IComponentConnector 组件连接器,IStyleConnector 样式连接器,列表`1 受影响的孩子,UncommonField `1 模板非FeChildrenField) 在 System.Windows.FrameworkTemplate.LoadContent(DependencyObject 容器,列表`1 受影响的孩子) 在 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(可用大小) 在 System.Windows.UIElement.Measure(可用大小) 在 System.Windows.Controls.Border.MeasureOverride(大小约束) 在 System.Windows.FrameworkElement.MeasureCore(可用大小) 在 System.Windows.UIElement.Measure(可用大小) 在 System.Windows.Controls.Grid.MeasureOverride(大小约束) 在 System.Windows.FrameworkElement.MeasureCore(可用大小) 在 System.Windows.UIElement.Measure(可用大小) 在 MS.Internal.Helper.MeasureElementWithSingleChild(UIElement 元素,大小约束) 在 System.Windows.Controls.ContentPresenter.MeasureOverride(大小约束) 在 System.Windows.FrameworkElement.MeasureCore(可用大小) 在 System.Windows.UIElement.Measure(可用大小) 在 System.Windows.Controls.Control.MeasureOverride(大小约束) 在 System.Windows.FrameworkElement.MeasureCore(可用大小) 在 System.Windows.UIElement.Measure(可用大小) 在 System.Windows.Controls.Border.MeasureOverride(大小约束) 在 System.Windows.FrameworkElement.MeasureCore(可用大小) 在 System.Windows.UIElement.Measure(可用大小) 在 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.c__DisplayClass12.b__11() 在 Microsoft.Expression.DesignHost.Isolation.Remoting.ThreadMarshaler.c__DisplayClass16`1.b__15() 在 Microsoft.Expression.DesignHost.Isolation.Remoting.STAMarshaler.Call.InvokeWorker() 在 Microsoft.Expression.DesignHost.Isolation.Remoting.STAMarshaler.Call.Invoke(布尔型 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,布尔提升查询,布尔处理) 在 Microsoft.Expression.DesignHost.Isolation.Remoting.STAMarshaler.OnWindowMessage(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam,Boolean& 处理) 在 Microsoft.Expression.DesignHost.Isolation.Remoting.MessageOnlyHwndWrapper.WndProc(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam) 在 MS.Win32.UnsafeNativeMethods.DispatchMessage(味精和味精) 在 System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame 框架) 在 System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame 框架) 在 System.Windows.Application.RunDispatcher(对象忽略) 在 System.Windows.Application.RunInternal(窗口窗口) 在 System.Windows.Application.Run(窗口窗口) 在 Microsoft.Expression.DesignHost.Isolation.IsolationProcess.RunApplication() 在 Microsoft.Expression.DesignHost.Isolation.IsolationProcess.c__DisplayClass2.b__0() 在 System.Threading.ThreadHelper.ThreadStart_Context(对象状态) 在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext,ContextCallback 回调,对象状态,布尔值 preserveSyncCtx) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback 回调,对象状态,布尔值 preserveSyncCtx) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback 回调,对象状态) 在 System.Threading.ThreadHelper.ThreadStart()

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

【问题讨论】:

  • 您可能想尝试更改 /ResourceDictionaries/MyStyles.xaml 的属性,右键单击并告诉我们您在属性窗口中看到的内容...。底线是 BAML 的加载程序不能在运行时找到这个文件。
  • 在运行时一切运行正常,错误只发生在设计时。无论如何,xaml 文件的属性如下: Build Action: Page;复制到输出目录:不要复制;自定义工具:MSBuild:Compile;自定义工具命名空间:[Empty];跳过文件名和完整路径,因为它们可能不会影响任何内容。

标签: c# wpf visual-studio exception designer


【解决方案1】:

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

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

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

【讨论】:

  • 我不敢相信设计师真的需要这个才不会出错。无论如何感谢您解决了我的问题。现在我可以在不调试的情况下进行视觉调整:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-11-04
  • 1970-01-01
  • 2015-05-20
  • 2015-06-22
  • 2018-06-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多