C# WPF XAML序列化/反序列化

C# WPF XAML序列化/反序列化,c#,wpf,xaml,serialization,C#,Wpf,Xaml,Serialization,我的应用程序基本上是使用一个包含其他控件的WPF画布,将其序列化为XML文件,然后反序列化以显示序列化的数据或以前保存的数据。序列化/反序列化工作正常,所有内容都已保存并恢复。问题是,在反序列化之后,如果我试图用下面的代码更改图像源,它将不起作用: testLogo.Source = new BitmapImage(new Uri(file.FileName)); 该图像在XAML中参考如下: <Canvas Name="mainCanva" Margin="0,0,12,0" Widt

我的应用程序基本上是使用一个包含其他控件的WPF画布,将其序列化为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现在可以更改图像源。“old”Snoop窗口可以看到正在通过testLogo.source=命令更新的图像源。WPF inspector没有这个问题,当反序列化发生时,它会立即更新自身。我的猜测是视觉树有问题。。。由于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;
        }
    }

您需要更新变量引用

调用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;

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

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

这样,无论可视化树发生什么情况,应用程序资源始终指向这一行中的正确项

文件
testLogo.Source=new-BitmapImage(new-Uri(file.FileName)),如果在窗口的
图像查看器中打开文件,您会看到什么,这里:
Canvas Canvas=DeSerializeXAML(appPath+“\\tmp\\mainCanva.xml”)作为画布是否为
canvas
null?反序列化XAML方法是如何实现的!?文件显示得很好,画布不是空的,因为反序列化后所有内容都正确显示。更新问题中发布的序列化/反序列化函数。
Resources["AVItestLogoic"] = ...