是否可以从xml文件创建对象?
<?xml version="1.0" encoding="utf-8"?>
<Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="Document" Width="257" Height="177.667" Clip="F1 M 0,0L 257,0L 257,177.667L 0,177.667L 0,0">
<Canvas x:Name="Layer_1" Width="800" Height="599.875" Canvas.Left="0" Canvas.Top="0">
<Path x:Name="Path" Width="257.165" Height="177.715" Canvas.Left="0" Canvas.Top="-2.09808e-005" Stretch="Fill" StrokeLineJoin="Round" Stroke="#FF000000" Fill="#FF1EA81E" Data="F1 M 0.5,46.5C 5.08123,46.5 9.30166,43.6262 13.1667,41.1667C 16.2416,39.2099 19.6465,37.4157 21.8333,34.5C 22.5876,33.4944 23.7458,32.839 24.5,31.8334C 25.0963,31.0383 25.4844,30.0972 25.8333,29.1667C 26.1551,28.3088 26.1783,27.3579 26.5,26.5C 26.849,25.5694 27.4844,24.7639 27.8333,23.8334C 28.1551,22.9754 28.0902,20.3471 28.5,21.1667C 29.0963,22.3592 28.5,23.8334 28.5,25.1667C 28.5,26.0555 28.5,26.9444 28.5,27.8334C 28.5,32.2778 28.9908,36.7494 28.5,41.1667C 28.3988,42.0773 27.947,42.9242 27.8333,43.8334C 27.7231,44.7154 27.3923,45.7282 27.8333,46.5C 29.5018,49.4198 35.4555,47.5445 37.8333,45.1667C 38.9665,44.0335 39.6926,42.5509 40.5,41.1667C 41.2511,39.879 41.8831,38.5238 42.5,37.1667C 42.9952,36.0772 43.1695,34.8291 43.8333,33.8334C 44.5306,32.7874 45.2429,31.1667 46.5,31.1667C 47.3889,31.1667 46.5738,32.9475 46.5,33.8334C 46.3512,35.6188 46.3255,37.4439 45.8333,39.1667C 44.3861,44.2321 40.5752,48.3245 38.5,53.1667C 37.9128,54.5368 37.1667,55.8334 36.5,57.1667C 35.8333,58.5 35.1667,59.8334 34.5,61.1667C 34.0556,62.0555 33.5156,62.9028 33.1667,63.8334C 32.845,64.6913 32.6988,65.6056 32.5,66.5C 32.2542,67.6061 30.7813,69.4125 31.8333,69.8333C 33.2174,70.387 34.6881,68.7877 35.8333,67.8333C 37.4073,66.5217 38.4392,64.668 39.8333,63.1667C 41.3303,61.5546 43.0592,60.1625 44.5,58.5C 53.0287,48.6592 60.1324,37.6679 67.8333,27.1667C 70.2024,23.9361 72.0963,20.3716 74.5,17.1667C 75.1667,16.2778 75.4221,14.2305 76.5,14.5C 77.5993,14.7748 77.0539,16.7059 77.1667,17.8334C 77.2772,18.9389 77.1667,20.0555 77.1667,21.1667C 77.1667,22.7222 77.3596,24.2898 77.1667,25.8334C 76.9125,27.8663 76.4812,29.8897 75.8333,31.8334C 72.1866,42.7735 65.899,52.6504 61.1667,63.1667C 60.2685,65.1625 59.2284,67.1028 58.5,69.1667C 57.8901,70.8947 57.5261,72.7031 57.1667,74.5C 56.8585,76.0408 56.7389,77.6136 56.5,79.1667C 56.2945,80.5027 55.9678,81.8217 55.8333,83.1667C 55.7449,84.0512 54.9901,86.1144 55.8333,85.8333C 57.0626,85.4236 57.1804,83.6193 57.8333,82.5C 58.7361,80.9524 59.561,79.3592 60.5,77.8333C 63.9254,72.2671 68.1515,67.2333 71.8333,61.8334C 80.6968,48.8335 90.1737,36.2529 99.8333,23.8334C 101.309,21.936 102.559,19.8717 103.833,17.8334C 104.783,16.3141 105.699,14.7691 106.5,13.1667C 107.035,12.0963 107.252,10.8794 107.833,9.83335C 108.373,8.86204 108.722,7.16667 109.833,7.16667C 110.722,7.16667 109.886,8.94596 109.833,9.83335C 109.663,12.7257 109.758,15.6635 109.167,18.5C 108.636,21.0464 107.201,23.3286 106.5,25.8334C 105.642,28.8972 105.223,32.0682 104.5,35.1667C 99.4052,57.0014 93.1979,78.5764 88.5,100.5C 87.8667,103.455 86.5664,106.234 85.8333,109.167C 85.2308,111.577 84.8875,114.046 84.5,116.5C 84.2206,118.27 84.0701,120.057 83.8333,121.833C 83.6257,123.391 83.425,124.95 83.1667,126.5C 82.9804,127.618 81.6295,130.559 82.5,129.833C 89.1776,124.269 91.7996,115.145 96.5,107.833C 103.099,97.5676 109.948,87.4627 116.5,77.1667C 122.787,67.287 129.594,57.7431 135.833,47.8334C 137.641,44.9629 139.519,42.1319 141.167,39.1667C 142.856,36.1261 144.432,33.0171 145.833,29.8334C 146.881,27.4526 147.534,24.915 148.5,22.5C 149.313,20.4679 150.289,18.5051 151.167,16.5C 151.845,14.9495 152.457,13.37 153.167,11.8334C 153.791,10.4798 154.185,8.95518 155.167,7.83335C 155.821,7.08543 157.057,5.87919 157.833,6.49998C 158.889,7.3444 158.404,9.15171 158.5,10.5C 158.627,12.2733 158.593,14.058 158.5,15.8334C 158.033,24.7155 155.231,33.3684 152.5,41.8334C 144.561,66.4427 129.705,88.3359 120.5,112.5C 118.148,118.674 116.032,124.936 113.833,131.167C 113.151,133.099 112.878,135.153 112.5,137.167C 112.21,138.711 112.114,140.287 111.833,141.833C 111.669,142.735 110.434,143.95 111.167,144.5C 112.073,145.18 113.594,144.513 114.5,143.833C 116.39,142.416 117.54,140.213 119.167,138.5C 121.764,135.766 124.5,133.167 127.167,130.5C 131.389,126.278 135.557,122 139.833,117.833C 144.224,113.555 148.682,109.346 153.167,105.167C 178.12,81.9148 203.049,58.6175 227.167,34.5C 234.957,26.7096 243.055,19.0003 249.167,9.83335C 250.436,7.92967 251.365,5.8198 252.5,3.83335C 253.143,2.70829 253.204,0.499979 254.5,0.499979C 255.796,0.499979 256.128,2.5922 256.5,3.83335C 256.819,4.89756 256.592,6.05943 256.5,7.16667C 256.37,8.73258 256.193,10.3038 255.833,11.8334C 255.302,14.0917 254.567,16.299 253.833,18.5C 250.165,29.505 245.773,40.256 241.833,51.1667C 231.279,80.3955 221.505,110.077 215.167,140.5C 213.638,147.839 211.211,155.037 210.5,162.5C 210.288,164.723 210.094,166.949 209.833,169.167C 209.65,170.727 209.297,172.267 209.167,173.833C 209.074,174.941 208.145,177.604 209.167,177.167C 211.338,176.236 212.163,173.504 213.833,171.833C 216.835,168.831 220.165,166.169 223.167,163.167C 232.302,154.031 241.853,144.912 248.5,133.833C 251.863,128.229 252.915,121.507 254.5,115.167"/>
</Canvas>
</Canvas>
我试过
StringReader stringReader = new StringReader(pathToFile);
XmlReader xmlReader = XmlReader.Create(stringReader);
Canvas savedCanvasChildren = (Canvas)XamlReader.Load(xmlReader);
但没有成功
所有这些技巧都是用于将xaml(保持画布)转换为任何rastr格式图片(png / jpeg)。我找到了几个解决方案,但我需要一个Object(Canvas或FremeworkElement)。我试图从文件中重新创建它。我已将此画布粘贴到测试项目中的网格中 - 并且看到了一张图片,因此xaml有效。
答案 0 :(得分:1)
您从ConsoleApplication调用此代码的事实是一个重要因素,因为任何DependencyObject
,Canvas
为1,都需要在单线程关联上下文中创建。您应该将控制台线程标记为STAThread:
[STAThread]
static void Main(string[] args)
{
StreamReader sr = new StreamReader(@"C:\Users\silviun\Desktop\Canvas.txt");
XmlReader xmlReader = XmlReader.Create(sr);
Canvas savedCanvasChildren = (Canvas)XamlReader.Load(xmlReader);
}
同样在检查Exception
时也检查InnerException,在这种情况下它将有以下消息 - &#34;调用线程必须是STA,因为许多UI组件都需要这个。&#34;这是处理WPF时的常见问题。
答案 1 :(得分:0)
XmlReader xmlReader = XmlReader.Create(fileName);
Canvas savedCanvasChildren = (Canvas)XamlReader.Load(xmlReader);
placeHolder.Children.Add(savedCanvasChildren);
以下是一些经过测试的代码(添加了命名空间):
void Main()
{
string pathToFile = @"d:\temp\wpfInject.xml";
var reader = XmlReader.Create( pathToFile );
Canvas savedCanvasChildren = (Canvas)XamlReader.Load(reader);
var content = savedCanvasChildren;
new Window { Content = content }.ShowDialog();
}