【问题标题】:How to determine if a ResourceDictionary is loaded correctly如何确定 ResourceDictionary 是否正确加载
【发布时间】:2010-05-24 21:20:29
【问题描述】:

我怎么知道(通过调试器我的应用资源是否正确加载)。我已经尝试过(在 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 sn-ps 是:

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

【问题讨论】:

    标签: wpf silverlight silverlight-3.0


    【解决方案1】:

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

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

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

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

    对于名为 FSSilverlightApp 的程序集以及项目/程序集根目录中的 TransitioningFrame.xaml 文件,您的语法看起来是正确的,因此请确保您的 XAML 文件位于那个位置。

    如果您从同一个程序集中加载资源字典,只需使用不带“程序集;组件/”语法的相对路径。我总是将我的资源字典放在 Assets 文件夹中(Silverlight 模板约定),并引用不带前导斜杠的文件,如...

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

    祝你好运,
    Jim McCurdy、YinYangMoney 和 Face to Face 软件

    【讨论】:

    • VS2010 rc1 中的 f# 应用程序中的文件夹支持不多。感谢您的指点,我将仔细检查该属性并查看它是否正在加载。
    猜你喜欢
    • 2023-03-25
    • 2012-02-16
    • 2013-11-13
    • 1970-01-01
    • 1970-01-01
    • 2014-11-02
    • 1970-01-01
    • 2012-12-18
    • 1970-01-01
    相关资源
    最近更新 更多