简体   繁体   English

Wpf 更改应用程序变量

[英]Wpf change application variable

I want to declare some application level variables.我想声明一些应用程序级变量。 These variables changes with values on different pages.这些变量随不同页面上的值而变化。 I declare a variable like this我这样声明一个变量

<sys:String x:key="Item1">Test</sys:String>

Now in my code I want to change it so the code should be现在在我的代码中我想改变它所以代码应该是

Application.Current.Properties["Item1"] = "This is a test";
String t = (string)this.TryFindResource("Item1");
MessegeBox.Show(t);

but this code is not changing the value and it always gives "Test" Any Idea how to fix that.但这段代码并没有改变价值,它总是给出“测试”任何想法如何解决这个问题。

Application properties are unrelated to XAML resources.应用程序属性与 XAML 资源无关。 Do this:做这个:

    Resources["Item1"] = "This is a test";
    String t = (string)this.TryFindResource("Item1");
    MessageBox.Show(t);

But really what you should probably be doing instead here is the MVVM pattern (google it)实际上你应该做的是 MVVM 模式(google it)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM