Reputation: 8363
I have a UserControl
view (MainView
) that hosts several UserControl
sub-views (ViewA
, ViewB
etc). In ViewA
, the only content set is a TabControl
, which is also implemented MVVM way.
<-- 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;
}
}
So in this case, my TabControl implements MVVM with View-First approach (by setting DataContext of View to ViewModel), whereas the each tabs implement MVVM with ViewModel-Fist approach (where ViewModel is created first, and subsequently View through DataTemplate).
And in both ViewA
and ViewB
, I have this:
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/ResourceDictionaries/MyStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
Everything runs perfectly in debug mode and release mode, except that my designer breaks down. When I open ViewA.xaml
, the designer renders it alright. Similarly for ViewB.xaml
, the designer renders it without any problems. However, when I open MainView.xaml
, my designer crashes with the following:
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()
The weird thing is that MainView
itself does not add the MyStyles.xaml
resource dictionary. MyStyles.xaml
is only added in the subviews ViewA
and ViewB
. I want to emphasize again that the designer can render both subviews without errors too. Although this does not bug my actual application at runtime, but it is still irritating to have to start run the whole application just to see how my views look like. Anyone here have any idea what went wrong here? By the way I'm using Visual Studio Professional 2012.
Upvotes: 0
Views: 586
Reputation: 48139
Your pack looks a LITTLE bit off from what I am used to seeing...
pack://application:,,,/[yourApplication];component/[path within your project]/[any subpath]/MyStyles.xaml"
So, although at run-time it may work, but design may need something extra such as the [yourApplication] reference? or any [path/subpath] to where your actual styles is declared. (but without the square brackets)
Upvotes: 1