Silverlight DataPager 本地化

发布于 2024-08-22 06:31:41 字数 194 浏览 7 评论 0原文

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

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

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

Is it possible to localize DataPager's footer (Page X of Y) in Silvelright?

The strings seem to be located in resources embedded in the assembly of DataPager. So how should I localize it?

Unfortunately, almost nothing in the DataPager class is virtual and also many internal classes are used by it, so it is not possible (at least easily) to inherit DataPager and override the behavior.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

那片花海 2024-08-29 06:31:41

这很简单。了解我如何将 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);
        }
    }
}

}

That's pretty simple. See how I localized DataPager for Portuguese language:

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);
        }
    }
}

}

遮了一弯 2024-08-29 06:31:41

到目前为止我发现的唯一解决方案是编辑DataPager的模板,删除负责显示“Page”和“of X”的两个文本框并创建新的文本框。然后,继承 DataPager,重写 OnApplyTemplate 以附加到新的 TextBox。

最后一部分是最棘手的 - 您必须处理数据源的正确事件(这取决于数据源)并更新新文本框的文本。

虽然这个解决方案应该可行,但它不是很好......

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...

樱娆 2024-08-29 06:31:41

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

该解决方案基于本文

由于 System.Controls.Data.dll 是由 MS 签名的,因此我需要删除签名(强名称)。我使用 AdmiralDebilitate 删除它。

  1. 将 System.Controls.Data.dll 复制到临时文件夹。
  2. 使用 AdmiralDebilitate 打开 dll,单击“全部标记”,然后单击“应用更改”。这应该删除强名称,该名称会阻止带有自定义资源的修补后的 dll 正常工作。
  3. 在临时文件夹中打开 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 应该有所帮助)。

So, there is another solution - changing the resources in the DLL.

The solution is based on this article.

Since the System.Controls.Data.dll is signed by MS, I needed to remove the signature (strong name). I used AdmiralDebilitate to remove it.

  1. Copy System.Controls.Data.dll to a temp folder.
  2. Use AdmiralDebilitate to open the dll, click Mark All and then Apply changes. This should remove the strong name that would prevent the patched dll with custom resources to work.
  3. Open Visual Studio Command Prompt in the temp folder.
  4. Disassemble the dll by command

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

  5. Use any resource editor (I used Resource.net) to open System.Windows.Controls.DataPager.PagerResources.resources.

  6. Edit the resource strings you want. Save the resource file and close the editor.
  7. Reassemble the assembly by command

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

  8. Done.

There are two possible problems:

  • You must make sure that VS uses this DLL and not the original one from GAC. This can be ensured by opening the .csproj file in notepad and checking the reference path.
  • If you use any other MS assemblies that are dependent on the patched one, you will need to patch them too (AdmiralDebilitate should help).
又爬满兰若 2024-08-29 06:31:41

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

<SupportedCultures>en,de</SupportedCultures>

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

Just add the cultures you need to the SupportedCultures element in the project file, e.g.

<SupportedCultures>en,de</SupportedCultures>

Now the DataPager uses German resources on German computers.

假装爱人 2024-08-29 06:31:41

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

<TextBlock Text="{Binding PageCount, RelativeSource={RelativeSource TemplatedParent}, StringFormat='/ \{0\}'}" VerticalAlignment="Center" Foreground="{TemplateBinding Foreground}" />

Another option is to remove the "suffix" textblock and add in your own with a binding to the PageCount property of the DataPager:

<TextBlock Text="{Binding PageCount, RelativeSource={RelativeSource TemplatedParent}, StringFormat='/ \{0\}'}" VerticalAlignment="Center" Foreground="{TemplateBinding Foreground}" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文