【问题标题】:How to store hyperlink in XML file and retrive it to WPF ListBox?如何将超链接存储在 XML 文件中并将其检索到 WPF ListBox?
【发布时间】:2014-05-25 09:09:13
【问题描述】:

我又有问题了。我的问题是 我不知道如何在 XML 文件中存储超链接以及如何将其从那里检索到 WPF ListBox。在我的应用程序中,我按以下方式编写 XML 文件:

<?xml version="1.0" encoding="utf-8" ?>
<Books xmlns="">
  <Category name="Computer Programming">
    <Book>
      <Author>H. Schildt</Author>
      <Title>C# 4.0 The Complete Reference</Title>
      <!--This is a hyperlink description-->
      <Link>https://www.mcgraw-hill.co.uk/html/007174116X.html</Link>
    </Book>
  </Category>
</Books>

在窗口资源部分的 XAML 中,我以以下方式编写:

<Window.Resources>
  . . . . . . . . . .
  <DataTemplate x:Key="detailDataTemplate">
     <StackPanel Orientation="Horizontal">
        <TextBlock Text="{Binding XPath=Title}"/>
        <TextBlock Text="{Binding XPath=Author}"/>
        <TextBlock Text="{Binding XPath=Link}"/>
     </StackPanel>
  </DataTemplate>
  . . . . . . . . . . .
</Window.Resources>

最后我在 ListBox 标记中写道:

<ListBox Name="lbxBooks" Grid.Row="1" Grid.Column="0"
. . . . . . . . . .
ItemTemplate="{StaticResource detailDataTemplate}" 
            IsSynchronizedWithCurrentItem="True"/>

但在应用程序启动后,超链接在屏幕上显示为简单字符串,该字符串未启用且不允许鼠标单击以获取互联网资源。所以它不能作为互联网资源的参考正常工作。它显示为简单的文本字符串。 如何更正此错误并强制链接正常工作?我必须在 XML 文件和 XAML 中更正什么?非常感谢您的帮助。

【问题讨论】:

    标签: c# xml wpf xaml listbox


    【解决方案1】:

    @超链接是一个字符串
    @WPF 示例中的超链接看here
    @通用 XML 工具:

      public class SerializationHelper
        {
            public static void SerializeToXML<T>(T t, String inFilename) where T : class
            {
                StreamWriter textWriter = null;
    
                try
                {
                    var serializer = new XmlSerializer(t.GetType());
                    textWriter = new StreamWriter(inFilename);
                    serializer.Serialize(textWriter, t);
    
                }
                finally
                {
                    if (textWriter != null) textWriter.Close();
                }
            }
    
            public static T DeserializeFromXML<T>(String inFilename) where T : class
            {
                TextReader textReader = null;
                T retVal = default(T);
                try
                {
                    var deserializer = new XmlSerializer(typeof(T));
                    textReader = new StreamReader(inFilename);
                    retVal = (T)deserializer.Deserialize(textReader);
                    return retVal;
    
                }
                finally
                {
                    if (textReader != null) textReader.Close();
                }
            }
        }
    



            public class Book
            {
                public string Author { get; set; }
                public string Title { get; set; }
                public string Link { get; set; }
                public string Catgegory { get; set; }
            }
    
            public class Data
            {
                public List<Book> Books { get; set; }
    
                public Data()
                {
                    Books = new List<Book>();
                }
            }
    
                   // ....
                   var filePath = "fileName.xml";
                   var bookList = new List<Book>();
                    bookList.Add(new Book
                    {
                        Author = "H. Schildt",
                        Link = @"https://www.mcgraw-hill.co.uk/html/007174116X.html",
                        Catgegory= "Computer Programming",
                        Title = "C# 4.0 The Complete Reference",
                    });
    
                    // save
                    SerializeToXML(new Data { Books = bookList }, filePath);
                    // load
                    var data = DeserializeFromXML<Data>(filePath);
    

    【讨论】:

    • 对不起,我是 XML 的新手。请给我现成的例子,以便我可以轻松地检查它。请。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多