保存并将其还原回xaml文件后,获取WPF用户控件事件. [英] Get WPF user controls events after saving and restore it back to xaml file.

查看:85
本文介绍了保存并将其还原回xaml文件后,获取WPF用户控件事件.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我是WPF的新手.我有这样的要求:

我用一些基本事件创建了一些用户控件,

然后在运行时在列表视图中加载这些用户控件

然后将这些用户控件拖放到容器(画布)上.

然后我尝试使用"XamlWriter.Save"功能存储它,并使用"XamlReader.Load"重新打开它.

直到这里我都没有任何问题.

但是问题是,当我重新打开存储的xaml时,我没有得到事件.
我在Microsoft博客中读到,无法使用"XamlWriter.Save"存储事件.

我的目标是在再次还原xaml时获取事件.

请建议我适当解决此问题.

预先感谢,
Sunil

Hello,

I am new bee to WPF. I am having a requirement something like this:

I have Created some user Controls with some basic events,

Then load these user controls at runtime in a listview

Then drag and drop these user controls on a container(Canvas).

Then i tried to store it using "XamlWriter.Save" function and reopen it with "XamlReader.Load" it.

Till here i dont have any problem in doing it.

But problem is that i am not getting the events when i reopened the stored xaml.
I have reads in microsoft blogs that storing events using "XamlWriter.Save" is not possible.

My goal is to get the events when i restore the xaml again.

Please suggest me appropiate solution to this problem.

Thanks in advance,
Sunil

推荐答案

如果仔细考虑一下,您将很容易意识到,您不能赋予任何意义以存储和加载事件.您加载或存储的始终是某些状态.将您的UI视为某些状态机.您可以保存它:所有元素的位置和属性,焦点的位置,输入的数据,所有选择等.

现在想想什么是事件?有事件实例作为每个类实例的成员.它们就像任何其他实例成员(或静态成员)一样存在.它们具有一些编译时和运行时类型.您还具有关联的委托人类型(有趣的是,与事件实例的类型几乎没有关系).最后,在运行时,将事件作为应用程序行为的元素,最终导致事件实例的调用,进而导致对添加到应用程序的调用列表中的所有事件处理程序的调用.事件处理程序.现在,考虑其中的哪一部分可以成为状态机的一部分?没事,实际上.事件只是状态机状态之间转换的机制之一.

如果您阅读过以下内容,则可以更好地理解它:
http://en.wikipedia.org/wiki/Finite-state_machine [ http://msdn.microsoft.com/en-us/library/ms733127.aspx [ ^ ].

这样,您将在UI及其存储之间实现松散耦合,在开发过程中,尤其是在支持过程中,您将不胜感激.另请参见:
http://en.wikipedia.org/wiki/Loose_coupling [如何在我的表单应用程序? [ ^ ],
创建属性文件... [
If you think about it a bit, you will easily realize that you cannot attribute any meaning the the idea of storing and loading events. What you load or store is always some state. Think of your UI as of some state machine. You can save it: positions and properties of all elements, position of the focus, entered data, all selections, etc.

Now think what is an event? There are event instances as members of each class instance. They just exist, like any other instance members (or static members, the that matter). They have some compile-time and run-time types. You also have the associated delegate type (interestingly, having almost nothing to do with the type of an event instance). And finally, during run-time, there are events as elements of the behavior of the application, something that eventually leads to the invocation of an event instance, which in turns leads to the invocation of all event handlers added to the invocation list of the event handler. Now, think what part of it could be a part of the state machine? Nothing, in fact. Event is just one of the mechanism of transition between the states of a state machine.

You can understand it a little better if you read about it:
http://en.wikipedia.org/wiki/Finite-state_machine[^].



Create a data model for that, separate from UI. Develop UI as state machine <--> data model.
The data model can be loaded/stored using Data Contract.
Please see:
http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].

This way, you will achieve loose coupling between UI and its storage which you will appreciate during development and especially support. See also:
http://en.wikipedia.org/wiki/Loose_coupling[^].

Please see my past answers advocating Data Contract approach:
How can I utilize XML File streamwriter and reader in my form application?[^],
Creating property files...[^].

—SA


这篇关于保存并将其还原回xaml文件后,获取WPF用户控件事件.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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