C# 无法序列化和反序列化UserControl

C# 无法序列化和反序列化UserControl,c#,xml,wpf,serialization,mvvm,C#,Xml,Wpf,Serialization,Mvvm,我用C#(WPF)中的MVVM制作了一个应用程序。将用户界面从XML序列化和反序列化为WPF时,我遇到了一个错误 我正在动态添加控件,但反序列化时会出现错误,如图所示: 这是我的密码。请帮我做序列化和反序列化的工作 <UserControl x:Class="DecisionSuite.Creator.SetValue" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation

我用C#(WPF)中的MVVM制作了一个应用程序。将用户界面从XML序列化和反序列化为WPF时,我遇到了一个错误

我正在动态添加控件,但反序列化时会出现错误,如图所示:

这是我的密码。请帮我做序列化和反序列化的工作

   <UserControl x:Class="DecisionSuite.Creator.SetValue"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                 xmlns:local="clr-namespace:DecisionSuite.Creator"
                 mc:Ignorable="d" 
                 d:DesignHeight="360" d:DesignWidth="800">
        <UserControl.Resources>
            <DataTemplate x:Key="SelectionTemplate">        
                <local:SetValueSet ></local:SetValueSet>
            </DataTemplate>
        </UserControl.Resources>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="2*"/>
                <RowDefinition Height="8*"/>
                <RowDefinition Height="2*"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <Border BorderThickness="1" Grid.Row="0" BorderBrush="LightGray" Background="LightGray" Grid.ColumnSpan="2"/>
            <Viewbox Grid.Row="0" Grid.ColumnSpan="2">
                <TextBlock Margin="5" Text="Set Value"/>
            </Viewbox>
            <Border BorderThickness="1,1,1,1" Grid.Row="1"  BorderBrush="LightGray">
                <ItemsControl ItemTemplate="{StaticResource SelectionTemplate}" ItemsSource="{Binding SetValueModels}"/>            
            </Border>
            <Grid Grid.Row="1">
            </Grid>
            <Border BorderThickness="1,0,1,1" Grid.Row="2" BorderBrush="LightGray" Grid.ColumnSpan="2"/>
            <Button Grid.Row="2" Grid.ColumnSpan="2" Command="{Binding RefreshList}">
                <Viewbox >
                    <TextBlock Text="Add New Item" Margin="0" Padding="5" RenderTransformOrigin="1.079,1.079"></TextBlock>
                </Viewbox>
            </Button>
        </Grid>
    </UserControl>

您不应该序列化用户控件,而应该序列化它们的数据,因为用户控件基本上就是您的用户界面,如文本框、按钮等


您应该序列化仅重新创建用户控件所需的数据,然后将用户控件设计为允许从序列化数据重新构造并作为新的用户控件。然后您的问题就会解决。

按“将异常详细信息复制到剪贴板”。然后打开记事本并“粘贴”。从记事本发布异常。将指令分成3行,这样您就可以知道指令的哪一部分失败了。下面是错误。。。LineNumber=1 LinePosition=918 Message=无法将“System.Windows.DataTemplate”类型的内容添加到“System.Windows.ResourceDictionary”类型的对象中。对象“SelectionTemplate”第1行位置918处出错。NameContext=Resources Source=PresentationFramework我不知道行位置918的位置。错误可能是由于xml与xaml不兼容。将指令分成几段,然后找到918所在的位置。如何检查我正在尝试标识,但没有得到任何解决方案。您遇到了一个资源字典错误。查看此链接是否有帮助: