SilverlightでUserControlをシリアル化する必要があります。私がしたいのは、「ディープコピー」だけです。私は動作しません。このコードを試してみました:SilverlightでUserControlをシリアライズする
using System.Windows;
using System.Windows.Controls;
using System.Runtime.Serialization;
using System.IO;
namespace SilverlightApplication1
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(MainPage_Loaded);
}
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
DataContractSerializer serializer = new DataContractSerializer(typeof(UserControl));
using (MemoryStream ms = new MemoryStream())
{
serializer.WriteObject(ms, this);
}
}
}
}
私はこの例外を取得:私はこの例外を取り除くことができますどのように
Type 'System.Windows.UIElement' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. Alternatively, you can ensure that the type is public and has a parameterless constructor - all public members of the type will then be serialized, and no attributes will be required.
を?
私がUserControlをシリアル化する理由は何ですか?理由は、SilverlightのPrinting APIを使用しようとしているからです。 WriteableBitmapをUserControlから作成してから印刷しようとしています。しかし私のUserControlは黒いテーマを持っていますが、印刷中は白いはずです。 UserControlの "Background"を直接変更すると、私の望むものではない画面上の視覚に影響します!だから私は、メモリ内のusercontrolのクローンを作成し、その背後にバックグラウンドの背景を変更し、そこからWriteableBitmapを作成して印刷しようとしています。しかし今まで運がない!
例外メッセージに記載されている手順を試してみましたか? –
どうすればいいですか? UIElementはすべてDLLに存在します。シリアル化可能とマークするにはどうすればよいですか? – TCM
クラスをシリアライズ可能としてマークすることができますが、ベースオブジェクトである必要はありません。 –