繁体   English   中英

WPF XAML序列化/反序列化

[英]WPF XAML serialize/deserialize

我的应用程序基本上是使用一个包含其他控件的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;
        }
    }

您需要更新变量参考。

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

尝试这个:

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>

然后在代码中:

Resources["AVItestLogoic"] = ...

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM