2

是否可以在 Silvelright 中本地化 DataPager 的页脚(Y 页 X)?

这些字符串似乎位于 DataPager 程序集中嵌入的资源中。那么我应该如何本地化呢?

不幸的是,DataPager 类中几乎没有任何东西是虚拟的,而且它使用了许多内部类,因此不可能(至少很容易)继承 DataPager 并覆盖该行为。

4

5 回答 5

4

这很简单。看看我如何将 DataPager 本地化为葡萄牙语:

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace MarceloOliveira.Controls
{
/// <summary>
/// Customização feita sobre o Data Pager padrão do Silverlight, para traduzir para o português
/// </summary>
public class CustomDataPager : DataPager
{
    TextBlock currentPagePrefixTextBlock;
    TextBlock currentPageSuffixTextBlock;
    TextBox currentPageTextBox;

    public CustomDataPager() : base()
    {
        this.PageIndexChanged += new EventHandler<EventArgs>(CustomDataPager_PageIndexChanged);
        this.MouseLeftButtonDown += new MouseButtonEventHandler(CustomDataPager_MouseLeftButtonDown);
    }

    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
        currentPagePrefixTextBlock = GetTemplateChild("CurrentPagePrefixTextBlock") as TextBlock;
        currentPageSuffixTextBlock = GetTemplateChild("CurrentPageSuffixTextBlock") as TextBlock;
        currentPageTextBox = GetTemplateChild("CurrentPageTextBox") as TextBox;
        currentPageTextBox.TextChanged += new TextChangedEventHandler(currentPageTextBox_TextChanged);
        currentPageSuffixTextBlock.SizeChanged += new SizeChangedEventHandler(currentPageSuffixTextBlock_SizeChanged);
    }

    void currentPageTextBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        TranslateLabels();
    }

    void CustomDataPager_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        TranslateLabels();
    }

    void CustomDataPager_PageIndexChanged(object sender, EventArgs e)
    {
        TranslateLabels();
    }

    void currentPageSuffixTextBlock_SizeChanged(object sender, SizeChangedEventArgs e)
    {
        TranslateLabels();
    }

    private void TranslateLabels()
    {
        if (currentPagePrefixTextBlock != null)
        {
            currentPagePrefixTextBlock.Text = "Pág.";
            currentPageSuffixTextBlock.Text = string.Format("de {0}", this.PageCount);
        }
    }
}

}

于 2010-04-23T13:42:29.667 回答
0

只需将您需要的文化添加到项目文件中的 SupportedCultures 元素中,例如

<SupportedCultures>en,de</SupportedCultures>

现在 DataPager 在德国计算机上使用德国资源。

于 2010-07-09T00:54:02.187 回答
0

The only solution I've found out so far is to edit the template of DataPager, remove the two textboxes responsible for displaying "Page" and "of X" and create new ones. Then, inherit DataPager, override OnApplyTemplate to attach to your new TextBoxes.

The last part is the trickiest - you have to handle proper events of your datasource (it depends on the datasource) and update text of your new textboxes.

Although this solution should work, it is not very nice...

于 2010-02-17T16:34:46.137 回答
0

另一种选择是删除“后缀”文本块,并通过绑定到 DataPager 的 PageCount 属性添加您自己的文本块:

<TextBlock Text="{Binding PageCount, RelativeSource={RelativeSource TemplatedParent}, StringFormat='/ \{0\}'}" VerticalAlignment="Center" Foreground="{TemplateBinding Foreground}" />
于 2011-12-20T00:00:39.847 回答
0

因此,还有另一种解决方案——更改 DLL 中的资源。

解决方案基于这篇文章

由于 System.Controls.Data.dll 由 MS 签名,我需要删除签名(强名称)。我使用AdmiralDebilitate将其删除。

  1. 将 System.Controls.Data.dll 复制到临时文件夹。
  2. 使用 AdmiralDebilitate 打开 dll,单击 Mark All 然后应用更改。这应该删除会阻止带有自定义资源的修补 dll 工作的强名称。
  3. 在 temp 文件夹中打开 Visual Studio 命令提示符。
  4. 通过命令反汇编dll

    ildasm /out=System.Controls.Data.il System.Controls.Data.dll

  5. 使用任何资源编辑器(我使用Resource.net)打开 System.Windows.Controls.DataPager.PagerResources.resources。

  6. 编辑所需的资源字符串。保存资源文件并关闭编辑器。
  7. 通过命令重新组装程序集

    ilasm /resource=System.Controls.Data.res /dll /output=System.Controls.Data.dll System.Controls.Data.il

  8. 完毕。

有两个可能的问题:

  • 您必须确保 VS 使用此 DLL 而不是 GAC 的原始 DLL。这可以通过在记事本中打开 .csproj 文件并检查参考路径来确保。
  • 如果您使用任何其他依赖于修补程序的 MS 程序集,您也需要修补它们(AdmiralDebilitate 应该会有所帮助)。
于 2010-03-12T10:22:04.760 回答