[英]How can I serialize wpf user control with xaml and code behind
I want to serialize wpf user control xaml and codebehind. 我想序列化WPF用户控件xaml和代码隐藏。
XamlWriter.Save()
only serialize's xaml so what can I do in this situation? XamlWriter.Save()
仅序列化xaml,因此在这种情况下我该怎么办? My situtation is I have a usercontrol which include custom methods, subscribed events (ex: button click) When I deserialize usercontrol (for create usercontrol at runtime) I want to run that events and methods. 我的情况是,我有一个用户控件,其中包括自定义方法,已订阅的事件(例如,单击按钮),当我反序列化用户控件时(要在运行时创建用户控件),我想运行该事件和方法。 Sorry my english isn't very good.
对不起,我的英语不是很好。
Just an idea, you can use MEF to built up a plugin structure for your user control. 只是一个想法,您可以使用MEF为您的用户控件建立插件结构。 You want to create a user control on the fly but the event handler still should be hardcoded somewhere else in your project;
您想即时创建用户控件,但事件处理程序仍应在项目中的其他位置进行硬编码。 with the plugin structure, you can just collect the plugin and reuse it;
通过插件结构,您可以收集插件并重复使用; the events can be handled by a command something.
事件可以通过命令来处理。 Maybe giving a scenario and we can figure out more detail.
也许给出一个方案,我们可以找出更多细节。
Plus, ISerializable provides a way for custom binary serialization for field, not for methods or events. 另外,ISerializable提供了一种用于字段(而不是方法或事件)的自定义二进制序列化的方法。 Here is a related question: What is the point of the ISerializable interface?
这是一个相关的问题: ISerializable接口的作用是什么? ;
; on the other hand, you can still try some pattern like how the web control save its view state;
另一方面,您仍然可以尝试某种模式,例如Web控件如何保存其视图状态; for example two virtual methods:
例如两个虚拟方法:
public virtual byte[] SaveState(); // it saves the states for your custom control
public virtual void LoadState(byte[] state) // it restore your state back to the control.
The custom code should be like: 自定义代码应类似于:
byte[] state = controlA.SaveState(); // this method saves its own state.
YourControl controlB = new YourControl();
controlB.LoadState(state); // this method load the save state from A to B itself.
For the event, every event has a handler name, you can also serialize its handler name to the state and find it back by the saved name from its naming container alike. 对于事件,每个事件都有一个处理程序名称,您也可以将其处理程序名称序列化为状态,然后从其命名容器中通过保存的名称找到它。 I don't have any experience to save the method.
我没有任何保存该方法的经验。
If you still want to save and load state including fields, events and method, maybe serialization is not the proper way you are looking for I think. 如果您仍然想要保存和加载状态(包括字段,事件和方法),那么序列化可能不是您所期望的正确方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.