从 XamlXmlReader 写入时 XamlObjectWriter 引发异常 - .Net 4
由于某种原因我得到了例外。 有人可以向我解释一下为什么以及如何解决这个问题吗? 我正在尝试读取数据模板。如果有更好的选择请告知。 谢谢。
using System;
using System.Windows;
using System.Windows.Data;
using System.IO;
using System.Xaml;
using System.Xml;
namespace TestApp
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Func("logo", "name");
}
public void Func(string img, string name)
{
string imgSource = "{Binding Source={x:Static Res:Resources." + img + "}, Converter={StaticResource ImageConverter}}";
string xamlString =
"<DataTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" " +
"xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" " +
"xmlns:Res=\"clr-namespace:FavoritesOrganizer.Properties\"><StackPanel>" +
"<Image Width=\"16\" Height=\"16\" Source=\"" + imgSource + "\" />" +
"<TextBlock Text=\"" + name + "\" Margin=\"2, 0, 0, 0\" VerticalAlignment=\"Center\" />" +
"</StackPanel></DataTemplate>";
StringReader xaml = new StringReader(xamlString);
XamlXmlWriter xamlWriter = null;
try
{
XmlReader xr = XmlReader.Create(xaml);
XamlXmlReader reader = new XamlXmlReader(xr);
XamlObjectWriter writer = new XamlObjectWriter(reader.SchemaContext);
XmlWriter xw = XmlWriter.Create(@"d:\xaml.xml");
xamlWriter = new XamlXmlWriter(xw, reader.SchemaContext);
int i = 1;
while (reader.Read())
{
try
{
var a = reader.Type;
xamlWriter.WriteNode(reader);
writer.WriteNode(reader);
i++;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message + " " + i);
}
}
DataTemplate datatemplate = (DataTemplate)writer.Result;//.Parse(xaml,pc);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
if (xamlWriter != null)
{
xamlWriter.Flush();
xamlWriter.Close();
}
}
}
}
}
For some reason I get an exception.
Can someone please explain to me why and how can fix this?
I am trying to read a DataTemplate. If there is a better option please tell.
Thanks.
using System;
using System.Windows;
using System.Windows.Data;
using System.IO;
using System.Xaml;
using System.Xml;
namespace TestApp
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Func("logo", "name");
}
public void Func(string img, string name)
{
string imgSource = "{Binding Source={x:Static Res:Resources." + img + "}, Converter={StaticResource ImageConverter}}";
string xamlString =
"<DataTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" " +
"xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" " +
"xmlns:Res=\"clr-namespace:FavoritesOrganizer.Properties\"><StackPanel>" +
"<Image Width=\"16\" Height=\"16\" Source=\"" + imgSource + "\" />" +
"<TextBlock Text=\"" + name + "\" Margin=\"2, 0, 0, 0\" VerticalAlignment=\"Center\" />" +
"</StackPanel></DataTemplate>";
StringReader xaml = new StringReader(xamlString);
XamlXmlWriter xamlWriter = null;
try
{
XmlReader xr = XmlReader.Create(xaml);
XamlXmlReader reader = new XamlXmlReader(xr);
XamlObjectWriter writer = new XamlObjectWriter(reader.SchemaContext);
XmlWriter xw = XmlWriter.Create(@"d:\xaml.xml");
xamlWriter = new XamlXmlWriter(xw, reader.SchemaContext);
int i = 1;
while (reader.Read())
{
try
{
var a = reader.Type;
xamlWriter.WriteNode(reader);
writer.WriteNode(reader);
i++;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message + " " + i);
}
}
DataTemplate datatemplate = (DataTemplate)writer.Result;//.Parse(xaml,pc);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
if (xamlWriter != null)
{
xamlWriter.Flush();
xamlWriter.Close();
}
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
像这样创建 XamlXmlReader:
并且不要忘记在 XAML 命名空间声明中使用
assemble=
。这是需要访问的当您使用 XamlXmlReader/Writer API 时,请键入您自己的程序集 (
Resources
)。Create XamlXmlReader like this:
and don't forget to use
assembly=
in XAML namespace declaration. This is needed to accesstypes in your own assembly (
Resources
) when you use XamlXmlReader/Writer API.