代码之家  ›  专栏  ›  技术社区  ›  akaphenom

如何确定是否正确加载了ResourceDictionary

  •  0
  • akaphenom  · 技术社区  · 16 年前

    我如何判断(通过调试器判断我的应用程序资源是否正确加载)。我试过了(在F)

    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")
        let siteTemplate : Grid = mainGrid 
    
        let txt : TextBlock = siteTemplate ? txt
    
        do
            this.Startup.Add(this.startup)
            let mutable s = "Items: "
            s <- s + this.Resources.Count.ToString()
    

    它返回一个零的计数。尽管我非常确定应用程序正在加载资源,因为如果我更改app.xaml中的路径,我会在运行时得到异常。其他Re、Lavent片段包括:

    我有以下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>
    

    内容模板:

    <

    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>
    
    1 回复  |  直到 16 年前
        1
  •  1
  •   Jim McCurdy    16 年前

    要查看加载了哪些合并词典,请使用调试器监视窗口或代码查看:

    Application.Current.Resources.MergedDictionaries.Count
    Application.Current.Resources.MergedDictionaries[0].Count
    etc...
    

    如果您的资源字典似乎没有加载,则可能是您传递到的源路径有问题…

    <ResourceDictionary Source="/FSSilverlightApp;component/TransitioningFrame.xaml" />
    

    对于名为 FSSilverlight应用程序 为了一个 转换框架.xaml 文件位于项目/程序集根目录下,因此请确保XAML文件位于该位置。

    如果要从同一程序集中加载资源字典,只需使用相对路径而不使用“ 组件;组件/ “语法。我总是把我的资源字典放在一个资产文件夹(Silverlight模板约定)中,引用没有前导斜杠的文件,如…

    <ResourceDictionary Source="Assets/Styles.xaml" />
    

    祝你好运,
    Jim McCurdy、YinyangMoney和面对面软件

    推荐文章