对象序列化(六):应用实例-WPF对象的XAML序列化
生活随笔
收集整理的這篇文章主要介紹了
对象序列化(六):应用实例-WPF对象的XAML序列化
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
WPF使用XAML來描述用戶界面,每個XAML元素往往對應著相應的WPF對象,元素的屬性對應著對象的屬性。
由于XAML元素與WPF對象之前存在著這種對應關系,其實可以將XAML代碼看成是WPF對象“序列化”后的結果。
WPF提供了XamlWriter 和 XamlReader? 兩個類實現WPF對象的XAML序列化和反序列化。
?
示例項目:
展示了一個繪圖板,用戶用鼠標點擊此繪圖板,程序將以隨機顏色和大小繪制一個矩形,點擊“保存”按鈕,當前圖形將被保存到磁盤上一個名為“PicData.dat” 的文件中,點擊“重建”按鈕,將恢復上次保存的圖形。
?
前臺的XAML代碼:
<Window x:Class="SaveAndReloadXAMLObject.Window1"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WPF對象的序列化" Height="300" Width="320">
<DockPanel>
<StackPanel DockPanel.Dock="Top" Orientation="Horizontal" HorizontalAlignment="Center" Margin="10">
<Button Margin="5" Width="80" Name="btnSave" Click="btnSave_Click">保存</Button>
<Button Margin="5" Width="80" Name="btnClear" Click="btnClear_Click">清空</Button>
<Button Margin="5" Width="80" Name="btnLoad" Click="btnLoad_Click">重建</Button>
</StackPanel>
<Border x:Name="DrawPanelBorder" BorderBrush="Black" Background="Black" CornerRadius="5" Margin="5" Padding="2">
<Canvas x:Name="DrawPanel" Background="White" MouseDown="Canvas_MouseDown"></Canvas>
</Border>
</DockPanel>
</Window>
?
后臺CS代碼:
using System;using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Markup;
using System.IO;
namespace SaveAndReloadXAMLObject
{
/// <summary>
/// Window1.xaml 的交互邏輯
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
CurCanvas = DrawPanel;
}
private Canvas CurCanvas = null;
private void Canvas_MouseDown(object sender, MouseButtonEventArgs e)
{
//獲取當前鼠標的位置
Point curP = e.GetPosition(sender as Canvas);
Random ran = new Random();
Color clr = Color.FromRgb((byte)ran.Next(0, 255), (byte)ran.Next(0, 255), (byte)ran.Next(0, 255));
Brush br = new SolidColorBrush(clr);
Rectangle rect = new Rectangle();
rect.Width = ran.Next(1, (int)(CurCanvas.ActualWidth - curP.X - 5));
rect.Height = ran.Next(1, (int)(CurCanvas.ActualHeight - curP.Y - 5));
rect.SetValue(Canvas.TopProperty, curP.Y);
rect.SetValue(Canvas.LeftProperty, curP.X);
rect.Fill = br;
CurCanvas.Children.Add(rect);
}
private void btnSave_Click(object sender, RoutedEventArgs e)
{
using (FileStream fs = new FileStream("PicData.dat", FileMode.Create))
{
//當需要保存當前圖形時,調用XamlWriter的以下方法將Canvas對象直接序列化到文件流中
XamlWriter.Save(CurCanvas, fs);
MessageBox.Show("數據已保存");
}
}
private void btnLoad_Click(object sender, RoutedEventArgs e)
{
using (FileStream fs = new FileStream("PicData.dat", FileMode.Open))
{
//double oldWidth = DrawPanelBorder.ActualWidth;
//double oldHeight = DrawPanelBorder.ActualHeight;
//調用XamlReader的方法從文件流中重建canvas對象
CurCanvas = XamlReader.Load(fs) as Canvas;
//CurCanvas.Width = oldWidth;
//CurCanvas.Height = oldHeight;
CurCanvas.MouseDown += Canvas_MouseDown;
DrawPanelBorder.Child = CurCanvas;
}
}
private void btnClear_Click(object sender, RoutedEventArgs e)
{
CurCanvas.Children.Clear();
}
}
}
?
如果用記事本打開 PicData.dat文件。可以看到其內容為XAML代碼。它就是XamlWriter 的序列化結果。
<Canvas Background="#FFFFFFFF" Name="DrawPanel" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"><Rectangle Fill="#FF0A83CB" Width="139" Height="103" Canvas.Left="58" Canvas.Top="24.1633333333333" /></Canvas>
?
轉載于:https://www.cnblogs.com/eagle1986/archive/2012/02/15/2351997.html
總結
以上是生活随笔為你收集整理的对象序列化(六):应用实例-WPF对象的XAML序列化的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ModifyStyle函数的用法
- 下一篇: Apache开启Gzip压缩,LAMP网