C# wpf在Resources.resx中添加/编辑/使用字符串值
我有一个WPF应用程序,Resources.resx中有多个字符串值,Resources.resx的访问修饰符设置为public 我的问题是,我想从Resources.resx文件中直接检索到xml标记中的值,还想编辑Resources.resx中键的值 为了编辑Resources.resx中的一些键,我使用了C# wpf在Resources.resx中添加/编辑/使用字符串值,c#,wpf,animation,storyboard,C#,Wpf,Animation,Storyboard,我有一个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
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>
到
如何在XML标记中做到这一点?比如我应该打什么?我尝试使用App.Resources,但找不到引用
我也是WPF的新手
非常感谢。更改资源的做法不是很好,因为默认情况下,它们应该被冻结。冻结用于避免内存泄漏的资源。更多信息请参见 在某些情况下,它可能会导致异常,例如:
InvalidOperationException
。但是,可以在表单的代码行中更改资源:
Application.Current.Resources["MyResource"] = MyNewValue;
示例
在资源中创建持续时间
:
<Window.Resources>
<Duration x:Key="MyDuration">0:0:0.75</Duration>
</Window.Resources>
您也可以这样使用它:
<DoubleAnimation Storyboard.TargetProperty="MyControl" From="-100" To="0" Duration="{StaticResource MyDuration}" DecelerationRatio=".9" />
对于要更改的资源,最好使用
DynamicResource
。有关详细信息,请参阅。非常感谢您的回答,我找不到Windows。参考资料,顺便说一下,我使用的是WPF,而您使用的是Windows窗体代码,我想是吗?不,这是WPF代码。你想在哪里查找Window.Resources
?我试图在app.config中添加它,它说名称空间前缀x没有定义,我的代码看起来是:0:0:0.999我想你走错方向了。。。我下载了示例,可以从下载。每当我尝试访问MyDuration值来读取它时,它都会抛出一个null异常,itried:MessageBox.Show(Application.Current.Resources[“MyDuration”].ToString());
<Window.Resources>
<Duration x:Key="MyDuration">0:0:0.75</Duration>
</Window.Resources>
<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" />
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();
}