How to fetch string from resource to assign in WPF Resource section in xaml

寵の児 提交于 2019-12-17 12:17:30

问题


I have a XBAP application with the following user control:

  <UserControl x:Class="XXX.UsersGrid"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Height="Auto" Width="Auto">

        <UserControl.Resources>
            <DataTemplate x:Key="UpArrowUsers">
                <DockPanel>
                    <TextBlock Text="xxUser" x:Name="upArrowUsersHeader" HorizontalAlignment="Center"></TextBlock>
                    <Path x:Name="arrow" StrokeThickness = "1" Fill= "gray" Data= "M 5,10 L 15,10 L 10,5 L 5,10"/>
                </DockPanel>
            </DataTemplate>
    </UserControl>
    ...

Now I want to fetch the string "xxUser" from a resx file which is embed as resource in the application How do I achieve this?


回答1:


None of those answers are close to what you want. I'd start by reading about Localization in WPF. You'll find that if you are doing localization with WPF you'll want x:Uid defined on every node in your app.

http://msdn.microsoft.com/en-us/library/ms788718.aspx




回答2:


I was able to do it in a program with:

<TextBlock VerticalAlignment="Center" Margin="3"
           Text="{x:Static prop:Resources.OpenButton}"
           Visibility="{Binding Source={x:Static prop:Settings.Default}, Path=ShowButtonText, Converter={StaticResource BoolToVis}}"></TextBlock>

I also had to include the .Properties namespace in my xaml, like so:

xmlns:prop="clr-namespace:MyProjectNamespace.Properties"

This allowed me to not only use the string resources I had defined for my project for globalization, but I was also able to bind (two way) to my application's Settings. This let me very easily remember the window's position, size, etc. As you can see, use Settings. for settings, and Resources. for resources.

As Steven mentioned, I think the "official" way or the "best" way is to stick x:Uid on everything that you want to globalize, but I didn't and it worked with no problems. I think the x:Uid thing is mostly required if you are using automated tools or breaking the translation task up as you would in a large project. I just did all my own stuff manually in VS, so maybe it was ok.

Ben




回答3:


Two more additional points that I forgot to mention above in "I was able to do it...":

  1. You don't have to wrap the Properties.Resources object in your own. You can access it directly, as I have done in my example above. I think wrapping the object is a way to get the same result as I discuss in my 2nd point.
  2. By default, the resource files are built with "ResXFileCodeGenerator". This makes them internal when it generates the code file, so xaml can't access it. You have to change this to "PublicResXFileCodeGenerator", which generates public classes. You can change this by clicking the resource file in the solution explorer and editing the "Custom Tool" property.

(sorry, I couldn't edit my above post because I was a temporary member at that time.)




回答4:


Create a static class that makes the resources available as properties:

public static class Resources
{
   public string Resource
   {
      return Properties.Resources.ResourceManager.GetString("Resource");
   }
}

Then you can bind your TextBox to this:

<TextBlock Text="{Binding Source={x:Static local:Resources}, Path=Resource}" x:Name="upArrowUsersHeader" HorizontalAlignment="Center"
   xmlns:local="clr-namespace:MY_NAMESPACE;assembly=MY_ASSEMBLY">



回答5:


As Ben said, and I found an another tutorial. The access modifier of Resources.resx should be changed from Internal to Public. I failed many times and after changing the access modifier to Public, it does work.




回答6:


I don't know if this can be done directly in XAML but if you write your own wrapper class around ResourceManager and use it instead. Notice that the class inherits from TextBlock:

public class ResourceContentTextBlock : TextBlock
{
    public string ResourceName 
    {
        set
        {
            this.Text = Properties.Resources.ResourceManager.GetString(value);
        }
    }
}

You can then use ResourceContentTextBlock in your XAML anywhere you would otherwise use a TextBlock:

<Window x:Class="WpfApplication3.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:client="clr-namespace:WpfApplication3" 
    >
        <client:ResourceContentTextBlock ResourceName="String1" />
</Window>


来源:https://stackoverflow.com/questions/665764/how-to-fetch-string-from-resource-to-assign-in-wpf-resource-section-in-xaml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!