【问题标题】:Unable to Serialize and deserialize an UserControl无法序列化和反序列化 UserControl
【发布时间】:2018-03-06 05:17:20
【问题描述】:

我在 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 所在的位置。
  • 如何检查我正在尝试识别但没有得到任何解决方案,.
  • 您收到资源字典错误。看看这个链接是否有帮助:docs.microsoft.com/en-us/dotnet/api/…

标签: c# xml wpf serialization mvvm


【解决方案1】:

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

您应该仅序列化重新创建用户控件所需的数据,然后设计您的用户控件以允许从序列化数据中重新构造并作为新的用户控件。那么你的问题就迎刃而解了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-17
    • 2016-12-30
    • 1970-01-01
    相关资源
    最近更新 更多