【问题标题】:While I am filling combobox with observable collection from wcf service i am getting following Exception当我用 wcf 服务的可观察集合填充组合框时,我得到以下异常
【发布时间】:2014-03-06 10:42:56
【问题描述】:

当我试图用 wcf 服务消耗的可观察集合填充组合框时,我遇到了以下异常。任何人都可以帮助我

无法创建程序集中定义的“MainWindow”实例 'SchoolManagementSystem,版本=1.0.0.0,文化=中性, PublicKeyToken=null'。异常已被一个目标抛出 调用。标记文件“MainWindow.xaml”第 1 行位置 9 中的错误。

MainWindow.xaml.cs:

 public partial class MainWindow : Window
    {

        public MainWindow()
        {
            InitializeComponent();

          ServiceReference1.Service1Client ClassList = new ServiceReference1.Service1Client();
            ObservableCollection<ClassDO> objClasslist = new ObservableCollection<ClassDO>(ClassList.GetClassList());
           // cmbclass.ItemsSource = objClasslist;
           // cmbclass.DisplayMemberPath = "ClassID";
            //cmbclass.SelectedValuePath = "CLassName";
        } }

MainWindow.xaml

 <Window x:Class="SchoolManagementSystem.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="549" Width="668" Loaded="Window_Loaded">
    <Grid Height="550" Width="600" Background="Beige" >

        <!--TextBlock Heading -->
        <TextBlock Height="23" HorizontalAlignment="Left" Margin="10,5,0,0" Name="TextBlockHeading" Text="Registration: " VerticalAlignment="Top" Width="110" FontSize="17" >
        </TextBlock>

        <!--Button and styling of login-->
        <Button Margin="451,5,12,288" Content="LOGIN" Cursor="Hand" >
            <Button.Template>
                <ControlTemplate TargetType="Button">
                    <TextBlock TextDecorations="underline">
                    <ContentPresenter />
                    </TextBlock>
                </ControlTemplate>
            </Button.Template>
            <Button.Style>
                <Style TargetType="Button">
                    <Setter Property="Foreground" Value="Black"></Setter>
                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Foreground" Value="Red"></Setter>
                        </Trigger>

                    </Style.Triggers>
                </Style>
            </Button.Style>
        </Button>
        <!--End of buttonstyling and link button-->

        <Grid Margin="31,0,29,23" Background="White"  VerticalAlignment="Center">
            <Grid.RowDefinitions>
                <RowDefinition Height="294"></RowDefinition>
            </Grid.RowDefinitions>
            <TextBlock Height="20" HorizontalAlignment="Left" Margin="67,0,0,0" x:Name="errormessage" VerticalAlignment="Top" Width="247" OpacityMask="Crimson"
                       Foreground="#FFE5572C"></TextBlock>
            <TextBlock Height="23" HorizontalAlignment="Left" Margin="67,20,0,0" Name="TextBlockStudentCode" Text="StudentCode" VerticalAlignment="Top" Width="110"></TextBlock>
            <TextBlock Height="23" HorizontalAlignment="Left" Margin="67,50,0,0" Name="TextBlockfullname" Text="FullName" VerticalAlignment="Top" Width="110"></TextBlock>
            <TextBlock Height="23" HorizontalAlignment="Left" Margin="67,80,0,0" Name="TextBlockFatherName" Text="FatherName" VerticalAlignment="Top" Width="110"></TextBlock>
            <TextBlock Height="23" HorizontalAlignment="Left" Margin="67,110,0,0" Name="TextBlockAddress" Text="Address" VerticalAlignment="Top" Width="110"></TextBlock>
            <TextBlock Height="23" HorizontalAlignment="Left" Margin="67,140,0,0" Name="TextBlockMobNo" Text="Mobile No" VerticalAlignment="Top" Width="110"></TextBlock>
            <TextBlock Height="23" HorizontalAlignment="Left" Margin="67,170,0,0" Name="TextBlockEmail" Text="Email" VerticalAlignment="Top" Width="110"></TextBlock>
            <TextBlock Height="23" HorizontalAlignment="Left" Margin="67,200,0,0" Name="TextBlockClass" Text="Class" VerticalAlignment="Top" Width="110"></TextBlock>
            <TextBlock Height="23" HorizontalAlignment="Left" Margin="67,230,0,0" Name="TextBlockSection" Text="Section" VerticalAlignment="Top" Width="110"></TextBlock>

            <TextBox Height="23" HorizontalAlignment="Left" Margin="183,20,0,0"   Name="TextBoxStudentCode" VerticalAlignment="Top" Width="222"></TextBox>
            <TextBox Height="23" HorizontalAlignment="Left" Margin="183,50,0,0"  Name="TextBoxFullname" VerticalAlignment="Top" Width="222"></TextBox>
            <TextBox Height="23" HorizontalAlignment="Left" Margin="183,80,0,0" Name="TextBoxFatherName" VerticalAlignment="Top" Width="222"></TextBox>
            <TextBox Height="23" HorizontalAlignment="Left" Margin="183,110,0,0" Name="TextBoxAddress" VerticalAlignment="Top" Width="222"></TextBox>
            <TextBox Height="23" HorizontalAlignment="Left" Margin="183,140,0,2" Name="TextBoxMobNo" VerticalAlignment="Top" Width="222"></TextBox>
            <TextBox Height="23" HorizontalAlignment="Left" Margin="183,170,0,2" Name="TextBoxEmail" VerticalAlignment="Top" Width="222"></TextBox>
            <ComboBox x:Name="cmbclass" ItemsSource="{Binding}" VerticalAlignment="Top" DisplayMemberPath="ClassName" SelectedValuePath="ClassID"/>

            <TextBox Height="23" HorizontalAlignment="Left" Margin="183,230,0,2" Name="TextBoxSection" VerticalAlignment="Top" Width="222"></TextBox>

            <Button Content="Submit" Height="23" HorizontalAlignment="Left" Margin="183,264,0,0" Name="Submit" VerticalAlignment="Top" Width="70" Click="Submit_Click" />
            <Button Content="Reset" Height="23" HorizontalAlignment="Left" Margin="259,264,0,0" Name="button2" VerticalAlignment="Top" Width="70"  />
            <Button Content="Cancel" Height="23" HorizontalAlignment="Right" Margin="0,264,60,0" Name="button3" VerticalAlignment="Top" Width="70"  />

        </Grid>
    </Grid>
</Window>

感谢任何帮助

【问题讨论】:

  • 在这里发布内部异常。你把这段代码放在哪里了?
  • 在 MyWindow.xaml.cs 文件下面的 Intializecomponent();
  • 标记文件“MainWindow.xaml”第 1 行位置 9 中的错误。如果您发布代码会有所帮助...
  • 在这里发布完整的 XAML 代码,就像我上面提到的那样检查内部异常的确切原因。
  • 这个方法ClassList.GetClassList()是否返回`IEnumerable'?

标签: .net wpf wcf combobox


【解决方案1】:

如果您想让它更易于调试,请将这些行移出构造函数并移到 Window_Loaded 处理程序中(它在 xaml 中,但您没有将它包含在 .cs 中)。

ServiceReference1.Service1Client ClassList = new ServiceReference1.Service1Client();
ObservableCollection<ClassDO> objClasslist = new ObservableCollection<ClassDO> ClassList.GetClassList()); //<-- Does this line even compile?
// cmbclass.ItemsSource = objClasslist;
// cmbclass.DisplayMemberPath = "ClassID";
//cmbclass.SelectedValuePath = "CLassName";

除非您实现INotifyPropertyChanged,否则您的绑定不会拾取它们,但您始终可以让它们工作并稍后将它们移回构造函数(或者更好地重构为方法并根据需要从加载或构造函数调用) .

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多