从代码更新Xaml中的资源 [英] Update resource in Xaml from code

查看:124
本文介绍了从代码更新Xaml中的资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在XAML中创建了这样的资源:

 <   window.resources  > 
    <!-   Opretter代理人 ->  
    <   local:agents     x:key   ="     xmlns:x   ="     xmlns:local   ="    >/ > 
<  /window.resources  > 



现在,我希望更新"该资源.
我希望从XML文件加载,然后使资源使用代理".

代理= FindResource(" ) <   local:agents     x:key   ="   xmlns:x   ="     xmlns:本地  ="    / >  


使用刚刚反序列化的代理

 agents = AgentsXmlSerializer.DeSerialize();


解决方案

您将必须删除现有资源,然后将新对象添加到资源中.

 Resources.Remove(" );
Resources.Add(" ,代理); 



我不明白您为什么要这么做,但是无论您要达到什么目的,我都相信您可以用更好的方式做到这一点.
还要记住,如果您用StaticResource引用资源,那么使用DynamicResource所做的操作可能没有效果,但不一定会达到您要实现的效果.


I have created a resource in XAML like this:

<window.resources>
    <!--Opretter Agents objekt-->
    <local:agents x:key="AgentsList" xmlns:x="#unknown" xmlns:local="#unknown" />
</window.resources>



Now I wish to "update" that resource.
I wish to load from a XML file, and then make the resource use "agents".

agents = FindResource("AgentsList") as Agents;
agents = AgentsXmlSerializer.DeSerialize();



so I want the window resource

<local:agents x:key="AgentsList" xmlns:x="#unknown" xmlns:local="#unknown" />


to use the agents I just deserialized

agents = AgentsXmlSerializer.DeSerialize();


how do I do this?

解决方案

You''ll have to remove the existing resource and then add the new object to the resources

Resources.Remove("AgentsList");
Resources.Add("AgentsList", agents);



I don''t understand why you would want to do this, but no matter what you''re trying to achieve I''m sure you could do it in a much better way.
Also remember if you refer to the resource with StaticResource then what you do will have no effect, using DynamicResource might but not necessarily have the effect you''re trying to achieve.


这篇关于从代码更新Xaml中的资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆