我已经为应用程序开发了一个调度程序和一个图书馆管理程序。库管理需要调度程序中的一些相同功能,因此在 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}}
这是我在两个应用程序中的文化课
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>
希望有人可以帮助我解决这个问题。
谢谢安德斯·米克尔森