在两个应用程序之间发送和检索序列化XAML并重新创建Control(或XAML对象)。 我想知道如何序列化XAML 我的代码:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
String s = XamlWriter.Save(this);
Console.WriteLine(s);
//serialize the window
try
{
int port=8900;
string host="211.87.227.183";
IPAddress ip=IPAddress.Parse(host);
IPEndPoint ipe=new IPEndPoint(ip,port);
Socket c=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
Console.WriteLine("Conneting...");
c.Connect(ipe);
string sendStr=s+'@';
byte[]bs=Encoding.ASCII.GetBytes(sendStr);
Console.WriteLine("SendMessage");
c.Send(bs,bs.Length,0);
string recvStr = "";
byte[]recvBytes=new byte[2024];
int bytes;
bytes=c.Receive(recvBytes,recvBytes.Length,0);
recvStr+=Encoding.ASCII.GetString(recvBytes,0,bytes);
Console.WriteLine("ClientGetMessage:{0}",recvStr);
c.Close();
}
catch(ArgumentNullException e1)
{
Console.WriteLine("ArgumentNullException:{0}",e1);
}
catch(SocketException e2)
{
Console.WriteLine("SocketException:{0}",e2);
}
Console.WriteLine("PressEntertoExit"); Console.ReadLine();
//using socket to send the string
}
主持人代码:
StringReader stringReader = new StringReader(s);
XmlReader xmlReader = XmlReader.Create(stringReader);//receive string from socket
Window win = new Window();
win = (Window)XamlReader.Load(xmlReader);// error:It cannot cast to the window type
Application app = new Application();
app.Run(win);
}
以下是我从xaml获得的字符串:
<MainWindow Title="MainWindow" SizeToContent="Manual".... Name="MainGrid"><av:Grid.LayoutTransform><av:ScaleTransform </MainWindow>