ControlTemplate Exception: "XamlParseException: Cannot find a Resource with the Name/Key"
Posted
by akaphenom
on Stack Overflow
See other posts from Stack Overflow
or by akaphenom
Published on 2010-05-24T23:13:31Z
Indexed on
2010/05/26
12:41 UTC
Read the original article
Hit count: 1035
If I move the application resources to a UserControl resources everythgin runs groovy. I still don't understand why.
I noticed that my application object MyApp did more than inherit from Application it loaded an XAML for the main template and connected all the plumbing. So I decided to create a user control to remove the template from the applciation (thinking there may be an even order issue not allowing my resource to be found).
namespace Module1
type Template() as this =
//inherit UriUserControl("/FSSilverlightApp;component/template.xaml", "")
inherit UriUserControl("/FSSilverlightApp;component/templateSimple.xaml", "")
do
Application.LoadComponent(this, base.uri)
let siteTemplate : Grid = (this.Content :?> FrameworkElement) ? siteTemplate
let nav : Frame = siteTemplate ? contentFrame
let pages : UriUserControl array = [|
new Module1.Page1() :> UriUserControl ;
new Module1.Page2() :> UriUserControl ;
new Module1.Page3() :> UriUserControl ;
new Module1.Page4() :> UriUserControl ;
new Module1.Page5() :> UriUserControl ; |]
do
nav.Navigate((pages.[0] :> INamedUriProvider).Uri) |> ignore
type MyApp() as this =
inherit Application()
do Application.LoadComponent(this, new System.Uri("/FSSilverlightApp;component/App.xaml", System.UriKind.Relative))
do
System.Windows.Browser.HtmlPage.Plugin.Focus()
this.RootVisual <- new Template() ;
// test code to check for the existance of the ControlTemplate - it exists
let a = Application.Current.Resources.MergedDictionaries
let b = a.[0]
let c = b.Count
let d : ControlTemplate = downcast c.["TransitioningFrame"]
()
"/FSSilverlightApp;component/templateSimple.xaml"
<UserControl x:Class="Module1.Template"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
<Grid HorizontalAlignment="Center" Background="White"
Name="siteTemplate">
<StackPanel Grid.Row="3" Grid.Column="2" Name="mainPanel">
<!--Template="{StaticResource TransitioningFrame}"-->
<navigation:Frame Name="contentFrame" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Template="{StaticResource TransitioningFrame}"/>
</StackPanel>
</Grid>
</UserControl>
"/FSSilverlightApp;component/App.xaml"
<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="Module1.MyApp">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/FSSilverlightApp;component/TransitioningFrame.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
"/FSSilverlightApp;component/TransitioningFrame.xaml"
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ControlTemplate x:Key="TransitioningFrame" TargetType="navigation:Frame">
<Border Background="Olive"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="5"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
<ContentPresenter Cursor="{TemplateBinding Cursor}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Margin="{TemplateBinding Padding}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
Content="{TemplateBinding Content}"/>
</Border>
</ControlTemplate>
</ResourceDictionary>
Unfortunately that did not work. If I remove the template attribute from the navigationFrame element, the app loads and directs the content area to the first page in the pages array. Referencing that resource continues to throw a resource no found error.
Original Post
I have the following app.xaml (using Silverlight 3)
<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="Module1.MyApp">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/FSSilverlightApp;component/TransitioningFrame.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
and content template:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ControlTemplate x:Key="TransitioningFrame" TargetType="navigation:Frame">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
<ContentPresenter Cursor="{TemplateBinding Cursor}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Margin="{TemplateBinding Padding}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
Content="{TemplateBinding Content}"/>
</Border>
</ControlTemplate>
</ResourceDictionary>
The debugger says it the contentTemplate is loaded correctly by adding some minimal code:
type MyApp() as this = inherit Application() do Application.LoadComponent(this, new System.Uri("/FSSilverlightApp;component/App.xaml", System.UriKind.Relative))
let cc = new ContentControl()
let mainGrid : Grid = loadXaml("MainWindow.xaml")
do
this.Startup.Add(this.startup)
let t = Application.Current.Resources.MergedDictionaries
let t1 = t.[0]
let t2 = t1.Count
let t3: ControlTemplate = t1.["TransitioningFrame"]
With this line in my main.xaml
<navigation:Frame Name="contentFrame" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Template="{StaticResource TransitioningFrame}"/>
Yields this exception
Webpage error details
User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2; .NET4.0C; .NET4.0E) Timestamp: Mon, 24 May 2010 23:10:15 UTC
Message: Unhandled Error in Silverlight Application Code: 4004
Category: ManagedRuntimeError
Message: System.Windows.Markup.XamlParseException: Cannot find a Resource with the Name/Key TransitioningFrame [Line: 86 Position: 115] at MS.Internal.XcpImports.CreateFromXaml(String xamlString, Boolean createNamescope, Boolean requireDefaultNamespace, Boolean allowEventHandlers, Boolean expandTemplatesDuringParse) at MS.Internal.XcpImports.CreateFromXaml(String xamlString, Boolean createNamescope, Boolean requireDefaultNamespace, Boolean allowEventHandlers) at System.Windows.Markup.XamlReader.Load(String xaml) at Globals.loadXaml[T](String xamlPath) at Module1.MyApp..ctor()Line: 54 Char: 13 Code: 0 URI: file:///C:/fsharp/FSSilverlightDemo/FSSilverlightApp/bin/Debug/SilverlightApplication2TestPage.html
© Stack Overflow or respective owner