gpt4 book ai didi

c# - 如何从资源中获取字符串以在 xaml 的 WPF 资源部分中分配

转载 作者:可可西里 更新时间:2023-11-01 08:13:50 25 4
gpt4 key购买 nike

我有一个具有以下用户控件的 XBAP 应用程序:

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

现在我想从 resx 文件中获取字符串“xxUser”,该文件作为资源嵌入到应用程序中我如何实现这一点?

最佳答案

我能够在一个程序中做到这一点:

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

我还必须在我的 xaml 中包含 .Properties 命名空间,如下所示:

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

这让我不仅可以使用我为我的项目定义的字符串资源来实现全局化,而且我还能够绑定(bind)(两种方式)到我的应用程序的设置。这让我非常容易记住窗口的位置、大小等。如您所见,使用“设置”。用于设置和资源。获取资源。

正如 Steven 所提到的,我认为“官方”方式或“最佳”方式是将 x:Uid 粘贴到您想要全局化的所有内容上,但我没有这样做,而且它没有问题。如果您使用自动化工具或像在大型项目中那样分解翻译任务,我认为 x:Uid 是最需要的。我只是在 VS 中手动完成了我自己的所有工作,所以也许没问题。

关于c# - 如何从资源中获取字符串以在 xaml 的 WPF 资源部分中分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/665764/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com