gpt4 book ai didi

c# - XamlObjectWriter 在从 XamlXmlReader - .Net 4 写入时抛出异常

转载 作者:太空宇宙 更新时间:2023-11-03 14:26:23 24 4
gpt4 key购买 nike

出于某种原因,我得到了一个异常(exception)。有人可以向我解释为什么以及如何解决这个问题吗?我正在尝试读取数据模板。如果有更好的选择请告诉。谢谢。

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();
}
}
}
}
}

最佳答案

像这样创建 XamlXmlReader:

XamlXmlReader reader = new XamlXmlReader(xr, System.Windows.Markup.XamlReader.GetWpfSchemaContext());

并且不要忘记在 XAML 命名空间声明中使用 assembly=。这是需要访问使用 XamlXmlReader/Writer API 时,在您自己的程序集 (Resources) 中键入。

关于c# - XamlObjectWriter 在从 XamlXmlReader - .Net 4 写入时抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3844301/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com