当我从wcf服务填充可观察集合的组合框时,我得到以下异常

时间:2014-03-06 10:42:57

标签: .net wpf wcf combobox

虽然我正在尝试使用wcf服务消耗的可观察集合来填充组合框,但我得到了以下异常。任何人都可以帮助我

  

无法创建程序集中定义的'MainWindow'实例   'SchoolManagementSystem,Version = 1.0.0.0,Culture = neutral,   公钥=空”。一个人的目标抛出了例外   调用。标记文件'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>

任何赞赏的帮助

1 个答案:

答案 0 :(得分:0)

如果你想让它更易于调试,可以将这些行移出构造函数并移入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,否则你的绑定不会接受它们,但是你总是可以让它们工作并稍后将它们移回构造函数(或者甚至更好地重构一个方法并从加载或构造函数调用必要)。