ControlTemplate异常:“XamlParseException:无法找到具有名称/键的资源”

时间:2010-05-24 23:13:32

标签: c# wpf silverlight silverlight-3.0 f#

如果我将应用程序资源移动到UserControl资源,则everythgin会运行groovy。我仍然不明白为什么。


我注意到我的应用程序对象MyApp做的不仅仅是从Application继承,它为主模板加载了一个XAML并连接了所有的管道。所以我决定创建一个用户控件来从applciation中删除模板(认为可能有一个偶数订单问题,不允许找到我的资源)。

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;组件/ 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;组件/ 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>

不幸的是,这不起作用。如果我从navigationFrame元素中删除template属性,则应用程序会加载内容区域并将其指向pages数组中的第一页。引用该资源会继续抛出资源,找不到错误。


原帖

我有以下app.xaml(使用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>

和内容模板:

<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>

调试器说通过添加一些最小代码正确加载了contentTemplate:

输入MyApp()as this =     继承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"]

在main.xaml中使用此行

<navigation:Frame Name="contentFrame" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"  Template="{StaticResource TransitioningFrame}"/>

产生此异常

  

网页错误详情

     

用户代理:Mozilla / 4.0(兼容;   MSIE 8.0; Windows NT 6.1;三叉戟/ 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)时间戳:2010年5月24日星期一   23:10:15 UTC

     

消息:未处理的错误   Silverlight应用代码:4004
  类别:ManagedRuntimeError
  信息:   System.Windows.Markup.XamlParseException:   找不到资源了   名称/键TransitioningFrame [行:86   位置:115]在   MS.Internal.XcpImports.CreateFromXaml(字符串   xamlString,布尔createNamescope,   Boolean requireDefaultNamespace,   Boolean allowEventHandlers,Boolean   expandTemplatesDuringParse)at   MS.Internal.XcpImports.CreateFromXaml(字符串   xamlString,布尔createNamescope,   Boolean requireDefaultNamespace,   布尔值allowEventHandlers)at   System.Windows.Markup.XamlReader.Load(字符串   xaml)at Globals.loadXaml [T](String   xamlPath)在Module1.MyApp..ctor()

     

行:54字符:13代码:0 URI:   文件:/// C:/fsharp/FSSilverlightDemo/FSSilverlightApp/bin/Debug/SilverlightApplication2TestPage.html

1 个答案:

答案 0 :(得分:0)

嘿,我以这种方式测试控件模板,但我尝试从控件本身加载它。我在github中有这个F#WPF自定义控件repo,

https://github.com/fahadsuhaib/FWpfControls

随意玩耍并了解它的加载方式。我甚至让它与BLEND合作:)。

WHOOPS,刚刚发现你正在使用Silverlight,我确信应该以相同的方式工作,尝试并让我知道。

-Fahad