【问题标题】:WPF XAML serialize/deserializeWPF XAML 序列化/反序列化
【发布时间】:2023-03-13 20:57:01
【问题描述】:

我的应用程序基本上是使用一个包含其他控件的 WPF Canvas 并将其序列化为 XML 文件,然后对其进行反序列化以显示序列化数据或以前保存的数据。序列化/反序列化工作正常,所有内容都已保存并恢复。问题是,反序列化后,如果我尝试使用以下代码更改图像源,它不起作用:

testLogo.Source = new BitmapImage(new Uri(file.FileName));

图像在 XAML 中的引用如下:

<Canvas Name="mainCanva" Margin="0,0,12,0" Width="1729" Height="150" VerticalAlignment="Top">
     <Border BorderThickness="3" BorderBrush="#FF009B80" FlowDirection="LeftToRight" VerticalAlignment="Top" HorizontalAlignment="Left" Width="1729" Height="150">
          <Grid Margin="0" Background="White" Height="Auto" Width="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Uid="">
               <Grid HorizontalAlignment="Left" Margin="3,28,0,57">
                   <Image HorizontalAlignment="Left" Margin="0,0,0,0" x:Name="testLogo" Stretch="UniformToFill" Width="317" Source="file:///C:/Users/logo.jpg" />
               </Grid>
          </Grid>
     </Border>
</Canvas>

反序列化代码如下:

Canvas canvas = DeSerializeXAML(appPath + "\\tmp\\mainCanva.xml") as Canvas;
mainCanva.Children.Clear();

while (canvas.Children.Count > 0)
{
       UIElement obj = canvas.Children[0]; 
       canvas.Children.Remove(obj); 
       mainCanva.Children.Add(obj); // Add to canvas
}

需要注意的另一点是,我尝试使用 Snoop 找出发生了什么,在反序列化之后,Snoop 也无法更改图像源,尽管如果我通过拖放十字准线将 Snoop 重新连接到应用程序,Snoop 现在能够更改图像源。 “旧”Snoop 窗口可以看到正在从 testLogo.Source = 命令更新的图像源。 WPF 检查器没有这个问题,它会在反序列化发生时立即自我更新。我的猜测是可视化树有问题......而且 WPF 可以做到这一点,我认为它可以排序。

感谢大家的帮助。

根据要求,序列化/反序列化功能:

public static void SerializeToXAML(UIElement element, string filename)
    {
        string strXAML = System.Windows.Markup.XamlWriter.Save(element);

        using (System.IO.FileStream fs = System.IO.File.Create(filename))
        {
            using (System.IO.StreamWriter streamwriter = new System.IO.StreamWriter(fs))
            {
                streamwriter.Write(strXAML);
            }
        }
    }

    public static UIElement DeSerializeXAML(string filename)
    {
        using (System.IO.FileStream fs = System.IO.File.Open(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read))
        {
            return System.Windows.Markup.XamlReader.Load(fs) as UIElement;
        }
    }

【问题讨论】:

  • 文件在这一行testLogo.Source = new BitmapImage(new Uri(file.FileName));,如果你在窗口的ImageViewer中打开文件,你看到了什么? ,这里:Canvas canvas = DeSerializeXAML(appPath + "\\tmp\\mainCanva.xml") as Canvas;canvas null ?
  • 你的 DeSerializeXAML 方法是如何实现的!?
  • 文件显示得很好,画布不为空,因为反序列化后一切都正常显示了。更新问题中发布的序列化/反序列化函数。

标签: c# wpf xaml serialization


【解决方案1】:

您需要更新您的变量引用。

当您调用 mainCanva.Children.Clear() 时,它会删除所有子项,包括 testLogo。您的变量 testLogo 仍将指向原始 testLogo 对象,即使它不再是 UI 的一部分。

试试这个:

Canvas canvas = DeSerializeXAML(appPath + "\\tmp\\mainCanva.xml") as Canvas;
mainCanva.Children.Clear();

while (canvas.Children.Count > 0)
{
       UIElement obj = canvas.Children[0]; 
       canvas.Children.Remove(obj); 
       mainCanva.Children.Add(obj); // Add to canvas
}
testLogo = mainCanva.FindName("testLogo") as Image;

【讨论】:

    【解决方案2】:

    我最终使用了应用程序资源:

    <Application.Resources>
        <BitmapImage x:Key="testLogo" >file:///C:/Users/test_B&amp;W.png</BitmapImage>
    </Application.Resources>
    

    然后在代码中:

    Resources["AVItestLogoic"] = ...
    

    这样无论可视树发生什么,应用程序资源总是指向正确的项目

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-26
      • 2020-10-29
      • 1970-01-01
      • 2011-08-10
      • 2012-03-17
      • 2012-05-18
      • 1970-01-01
      相关资源
      最近更新 更多