【问题标题】:wpf adding/editing/using string values in Resources.resxwpf 在 Resources.resx 中添加/编辑/使用字符串值
【发布时间】:2013-06-26 23:47:33
【问题描述】:

我有一个 WPF 应用程序,我在 Resources.resx 中有多个字符串值,并且 Resources.resx 的访问修饰符设置为 public。

我的问题是我想将 Resources.resx 文件中的值直接检索到 xml 标记中,并且还想编辑 Resources.resx 中键的值。

为了编辑我使用的 Resources.resx 中的一些键

this.Resources["Duration_value"] = "0:0:15"; // that's a key in Resources.resx that i created

但它根本不起作用,价值保持不变。

关于第二个问题,假设我有一个带有双重动画的情节提要的 xml 代码,例如:

    <Storyboard x:Key="FlipIn" >
        <DoubleAnimation Storyboard.TargetProperty="(RenderTransform).(TransformGroup.Children)[1].(SkewTransform.AngleX)" From="-100" To="0" Duration="0:0:.75" DecelerationRatio=".9" />
        <DoubleAnimation Storyboard.TargetProperty="(RenderTransform).(TransformGroup.Children)[1].(SkewTransform.AngleY)" From="-100" To="0" Duration="0:0:.75" DecelerationRatio=".9" />
    </Storyboard>

假设我想更改持续时间值

Duration="0:0:.75"

Duration="{ Duration_value from regex file }"

我如何在 XML 标记中做到这一点?比如我应该输入什么?我尝试使用 App.Resources 但找不到参考。

我也是 WPF 的新手。

非常感谢。

【问题讨论】:

    标签: c# wpf animation storyboard


    【解决方案1】:

    更改资源的做法不是很好,因为默认情况下,它们应该被冻结。冻结用于避免内存泄漏的资源。更多信息请参阅MSDN link

    在某些情况下,可能会导致异常,例如:InvalidOperationException。但是,可以在表单的代码行中更改资源:

    Application.Current.Resources["MyResource"] = MyNewValue;
    

    示例

    在资源中创建Duration

    <Window.Resources>
        <Duration x:Key="MyDuration">0:0:0.75</Duration>
    </Window.Resources>
    

    显示在TextBlock值:

    <Grid Name="MyGrid">
        <TextBlock Name="MyTextBlock" Text="{Binding Source={StaticResource MyDuration}}" />
    
        <Button Name="ChangeResource" Content="ChangeResource" Width="100" Height="30" Click="ChangeResource_Click" />
    </Grid>
    

    你也可以这样使用:

    <DoubleAnimation Storyboard.TargetProperty="MyControl" From="-100" To="0" Duration="{StaticResource MyDuration}" DecelerationRatio=".9" />
    

    ChangeResource_Click列表:

        private void ChangeResource_Click(object sender, RoutedEventArgs e)
        {
            TimeSpan MyTimespan = new TimeSpan(2, 2, 3);
            Duration DurationInCode = MyTimespan;
    
            // Set the new value
            Application.Current.Resources["MyDuration"] = MyTimespan;
    
            // Show value
            MyTextBlock.Text = Application.Current.Resources["MyDuration"].ToString();
        }
    

    对于您要更改的资源,最好使用DynamicResource。请参阅MSDN link,了解更多信息。

    【讨论】:

    • 非常感谢您的回答,我找不到 Windows.Resources,顺便说一下,我使用的是 WPF,而您使用的是 Windows 窗体代码,我猜?
    • 不,这是 WPF 代码。你在哪里寻找Window.Resources
    • 我尝试在 app.config 中添加它,它说命名空间前缀 x 未定义,我的代码如下所示:0:0:0.999
    • 我觉得你走错方向了...我下载了例子,可以从link下载。
    • 每当我尝试访问 MyDuration 值来读取它时,它都会抛出一个空异常,尝试:MessageBox.Show(Application.Current.Resources["MyDuration"].ToString());跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-29
    • 2013-07-30
    • 1970-01-01
    • 2012-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多