加载资源到导入的程序集

时间:2015-09-03 09:29:55

标签: wpf binding resources .net-assembly mergeddictionaries

我已经开发了应用程序调度程序和库管理程序。 库调配需要调度程序中的一些相同功能,因此在NuGet上发布调度程序并将其安装在库管理程序中,并使用调度程序软件中的用户控件。 这工作正常,但是当我在库程序中使用它时,调度程序的翻译不会被加载。当我单独使用调度程序时,它工作正常。

在这两个程序中,我创建了一个像这样的ResourceDictionary

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cultures="clr-namespace:LibraryAdministration.Cultures">

<!-- Resources ODP contains the current instance of the WPFLocalize.Properties.Resources class.
   Used in bindings to get localized strings and automatic updates when the culture is updated -->
<ObjectDataProvider x:Key="Resources" ObjectType="{x:Type cultures:CultureResources}" MethodName="GetResourceInstance"/>

<!-- CultureResources ODP provides access to list of currently available cultures -->
<ObjectDataProvider x:Key="CultureResourcesDS" ObjectType="{x:Type cultures:CultureResources}"/>
<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary 
          Source="pack://application:,,,/Administration;component/resourcedictionary.xaml" />
</ResourceDictionary.MergedDictionaries>

我在这两个程序中加载资源

{Binding Path=MainWindow_Title, Source={StaticResource Resources}}

这是我在两个应用程序中的Culture类

public class CultureResources
{
    //only fetch installed cultures once
    private static bool bFoundInstalledCultures = false;

    private static List<CultureInfo> pSupportedCultures = new List<CultureInfo>();
    /// <summary>
    /// List of available cultures, enumerated at startup
    /// </summary>
    public static List<CultureInfo> SupportedCultures
    {
        get { return pSupportedCultures; }
    }

    public CultureResources()
    {
        if (!bFoundInstalledCultures)
        {
            //determine which cultures are available to this application
            Debug.WriteLine("Get Installed cultures:");
            CultureInfo tCulture = new CultureInfo("");
            foreach (string dir in Directory.GetDirectories(Application.StartupPath))
            {
                try
                {
                    //see if this directory corresponds to a valid culture name
                    DirectoryInfo dirinfo = new DirectoryInfo(dir);
                    tCulture = CultureInfo.GetCultureInfo(dirinfo.Name);

                    //determine if a resources dll exists in this directory that matches the executable name
                    if (dirinfo.GetFiles(Path.GetFileNameWithoutExtension(Application.ExecutablePath) + ".resources.dll").Length > 0)
                    {
                        pSupportedCultures.Add(tCulture);
                        Debug.WriteLine(string.Format(" Found Culture: {0} [{1}]", tCulture.DisplayName, tCulture.Name));
                    }
                }
                catch(ArgumentException) //ignore exceptions generated for any unrelated directories in the bin folder
                {
                }
            }
            bFoundInstalledCultures = true;
        }
    }

    /// <summary>
    /// The Resources ObjectDataProvider uses this method to get an instance of the WPFLocalize.Properties.Resources class
    /// </summary>
    /// <returns></returns>
    public LibraryAdministration.Cultures.Resources GetResourceInstance()
    {

        return new Resources();
    }

    private static ObjectDataProvider m_provider;
    public static ObjectDataProvider ResourceProvider
    {
        get
        {
            if (m_provider == null)
                m_provider = (ObjectDataProvider)App.Current.FindResource("Resources");
            return m_provider;
        }
    }

    /// <summary>
    /// Change the current culture used in the application.
    /// If the desired culture is available all localized elements are updated.
    /// </summary>
    /// <param name="culture">Culture to change to</param>
    public static void ChangeCulture(CultureInfo culture)
    {
        //remain on the current culture if the desired culture cannot be found
        // - otherwise it would revert to the default resources set, which may or may not be desired.
        if (pSupportedCultures.Contains(culture))
        {
            Resources.Culture = culture;
            ResourceProvider.Refresh();
        }
        else
            Debug.WriteLine(string.Format("Culture [{0}] not available", culture));
    }
}

}

我知道问题是调度程序软件中的资源没有初始化,我试图在我的库应用程序中创建一个MergedDictionaries,如此没有运气

    <ResourceDictionary.MergedDictionaries>
    <ResourceDictionary 
          Source="pack://application:,,,/Administration;component/resourcedictionary.xaml" />
</ResourceDictionary.MergedDictionaries>

希望有一些可以帮助我解决这个问题。

由于 安德斯米克尔森

1 个答案:

答案 0 :(得分:0)

我通过在我的图书馆管理中向我的resourceDirectory.xaml添加一个ObjectDataProvider来解决它

<ObjectDataProvider x:Key="CSResources" ObjectType="{x:Type CScultures:CultureResources}" MethodName="GetResourceInstance"/>

来自我的学员管理

"xmlns:CScultures="clr-namespace:Administration.Cultures;assembly=Administration"

我将密钥命名为与调度程序管理中的密钥相同。

因为不能有2个具有相同值的键,所以我将资源名称更改为CSResource。