如何在Silverlight 5中添加应用程序范围的资源字典



我有一个Silverlight 5应用程序,它拒绝接受应用程序中的任何内容。资源标签:

<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
         x:Class="My.Awesome.App"
         >
<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Assets/Styles.xaml"/>
            <ResourceDictionary>
                <App:ApplicationResources x:Key="ApplicationResources" />
            </ResourceDictionary>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

Styles.xaml和ApplicationResources都不可用于我的控件。例如:

<controls:ChildWindow
x:Class="My.Awesome.ErrorWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
Title="{Binding Path=Errors.ErrorWindowTitle, Source={StaticResource ApplicationResources}}"
Style="{StaticResource ErrorWindowStyle}">

该控件在运行时会崩溃,但在VisualStudio编辑器中运行良好(我可以看到来自ApplicationResources的文本)。App.xaml中的App:ApplicationResources如下所示:

namespace My.Awesome
{
    using System;
    using System.ComponentModel;
    using System.Globalization;
    using System.Windows.Browser;
    /// <summary>
    /// Wraps access to the strongly-typed resource classes so that you can bind control properties to resource strings in XAML.
    /// </summary>
    public sealed class ApplicationResources
    {
        private static readonly ApplicationStrings applicationStrings = new ApplicationStrings();
        private static readonly ErrorResources errorResources = new ErrorResources();
        /// <summary>
        /// Gets the <see cref="ApplicationStrings"/>.
        /// </summary>
        public ApplicationStrings Strings
        {
            get { return applicationStrings; }
        }
        /// <summary>
        /// Gets the <see cref="ErrorResources"/>.
        /// </summary>
        public ErrorResources Errors
        {
            get { return errorResources; }
        }
    }
}

我有相应的ApplicationStrings.resx和ErrorResources.resx,它们具有指定的正确命名空间(My.Awesome)和PublicResXFileCodeGenerator处理器。

我把它缩小到Application.Resources字典不起作用,因为如果我在后面的代码中这样做:

this.Resources.Add("ApplicationResources", new ApplicationResources());

并删除对Styles的引用,然后应用程序运行。

作为参考,我从Silverlight业务应用程序模板中提取了ErrorWindow.xaml、Styles.xaml和两者.resx;它在那里运行得很好,我只是不明白为什么它在这个应用程序中不起作用。

Nevermind我最终将代码复制到了Silverlight Business应用程序上,并且成功了。我不知道为什么那样会搞砸;这并不是它遭受的唯一错误。

最后,启动a-fresh解决了我的所有问题:)

发生这种情况是因为不知何故,我的"App.xaml"构建操作不是"ApplicationDefinition",而是"Page"。切换到"ApplicationDefinition"为我修复了它。干杯!

相关内容

最新更新