简体   繁体   English

如何使用XAML和后面的代码序列化WPF用户控件

[英]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.

相关问题 如何从 xaml 中的 ItemTemplate 访问在代码中定义的用户控件的依赖项属性? - How can I access a dependency property of a user control that was defined in code behind from a ItemTemplate in xaml? 如何从后面的代码刷新自定义WPF用户控件? - How can I refresh a custom wpf user control from code behind? 如何在代码中包装我的用户控件? - How can I wrap my user control in code behind? 如何将XAML内容控件转换为C#中的代码隐藏? - How can I convert this XAML Content Control to Code Behind in C#? 无法在“隐藏代码”中访问WPF用户控件 - Can't access WPF user control in Code Behind 使用WPF自定义控件,如何通过后面的代码为自定义控件提供名称以访问它? - Using a WPF Custom Control, How can I give my custom control a name to access it via the code behind? 如何从背后的代码或通过绑定刷新WPF中的自定义用户控件 - How to refresh custom user control in WPF from code behind or by binding 如何将WinForm用户控件添加到WPF中,以便我可以在xaml.cs文件中引用它 - How to add WinForm User Control Into WPF so that I can reference it in the xaml.cs file 如何在XAML中“创建”的Code Behind中访问控件 - How to access control in Code Behind that was 'created' in XAML 从后面的代码添加WPF用户控件 - Adding a WPF User Control from Code Behind
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM