[英]Unable to Serialize and deserialize an UserControl
我在 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>
你不应该序列化用户控件,而是序列化它们的数据,因为用户控件基本上是你的用户界面,比如文本框、按钮等。
您应该序列化仅重新创建用户控件所需的数据,然后设计您的用户控件以允许从序列化的数据中重新构建并作为新的用户控件。 那么你的问题就迎刃而解了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.