0

我的应用程序的 App.xaml 文件中有此资源。

//App.xaml
<Application.Resources>
    <sys:String x:Key="ApplicationTitle">FUEL CONSUMPTION</sys:String>
</Application.Resources>

这非常有效!但我正在尝试通过代码隐藏文件设置资源,如下所示:

// MainPage.xaml.cs#PhoneApplicationPage_Loaded event
// To get the assembly version number
var nameHelper = 
           new System.Reflection.AssemblyName
              (System.Reflection.Assembly.GetExecutingAssembly().FullName);
// To change the application title based on the assembly version 
Application.Current.Resources["ApplicationTitle"] = "FUEL CONSUMPTION - v" +
           nameHelper.Version.Major + "." + nameHelper.Version.Minor; 

但是每次我尝试分配它时,我都会NotImplentedException被抛出。这是可以预料的,因为显然资源的设置器是以这种方式实现的,但是有没有办法解决这个问题?

我想利用这些资源使绑定变得容易。

4

2 回答 2

2

尝试:App.Current.Resources.Add("Key",Value);

于 2012-08-20T09:20:11.153 回答
1

我找到了解决问题的方法。非常感谢Milan Aggarwal为我提供了解决方案。

解决方案是将以下代码放入Application_Launching事件处理程序中。

var nameHelper = new System.Reflection.AssemblyName
    (System.Reflection.Assembly.GetExecutingAssembly().FullName);

if (Application.Current.Resources.Contains("ApplicationTitle")) 
    Application.Current.Resources.Remove("ApplicationTitle");

Application.Current.Resources.Add("ApplicationTitle", 
    "FUEL CONSUMPTION - v" + nameHelper.Version.Major + "." +
    nameHelper.Version.Minor);

事实证明,如果绑定对象更新,应用程序标题不会更新。资源的更改必须在PhoneApplicationPage对象创建之前发生。

于 2012-08-23T10:31:41.590 回答