黑马程序员技术交流社区

标题: 高手进啊!!!小弟让这给搞崩溃了!!! [打印本页]

作者: me黄小峰    时间: 2012-3-7 11:38
标题: 高手进啊!!!小弟让这给搞崩溃了!!!
这是WPF的xaml代码:<Window x:Class="BellRingers.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="协会成员" Height="470" Width="600">
    <!--<Window.Resources>
        
        <Style x:Key="buttonStyle">
            <Setter Property="Button.Background" Value="Gray"/>
            <Setter Property="Button.Foreground" Value="white"/>
            <Setter Property="Button.FontFamily" Value="comic sans ms"/>
        </Style>
    </Window.Resources>-->
    <Window.Resources>
        <Style x:Key="bellRingerFontStyle" TargetType="Control">
            <Setter Property="FontFamily" Value="comic sans MS"/>
        </Style>
        <Style x:Key="bellRingersStyle" TargetType="Control">
            <Setter Property="Background" Value="gray"/>
            <Setter Property="Foreground" Value="white"/>
            <Setter Property="FontFamily" Value="comic sans MS"/>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="true">
                    <Setter Property="Background" Value="blue"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid>
        <Image  Panel.ZIndex="0" Name="image1">
            <Image.Source>
                <BitmapImage UriSource="Bell.gif"/>
            </Image.Source>
        </Image>
        <Label Content="First Name:" Height="28" HorizontalAlignment="Left" Margin="39,25,0,0" Name="label1" VerticalAlignment="Top" Width="75" Style="{StaticResource bellRingerFontStyle}" />
        <TextBox Style="{DynamicResource bellRingersStyle}" Height="23" HorizontalAlignment="Left" Margin="121,25,0,0" Name="firstName" VerticalAlignment="Top" Width="175" />
        <Label Content="Last Name:" Height="28" HorizontalAlignment="Left" Margin="305,25,0,0" Name="label2" VerticalAlignment="Top" Width="75" Style="{StaticResource bellRingerFontStyle}" />
        <TextBox Height="23" Style="{DynamicResource bellRingersStyle}" HorizontalAlignment="Left" Margin="380,25,0,0" Name="lastName" VerticalAlignment="Top" Width="175" />
        <Label Content="挂钟地点:" Style="{StaticResource bellRingerFontStyle}" Height="28" HorizontalAlignment="Left" Margin="29,72,0,0" Name="label3" VerticalAlignment="Top" Width="75" />
        <ComboBox Height="23" HorizontalAlignment="Left" Margin="121,72,0,0" Name="towerNames" VerticalAlignment="Top" Width="275" />
        <CheckBox Content="Captain" Style="{StaticResource bellRingerFontStyle}" Height="23" HorizontalAlignment="Left" Margin="420,72,0,0" Name="isCaptain" VerticalAlignment="Top" Width="75" />
        <Label Content="Member Since:" Style="{StaticResource bellRingerFontStyle}" Height="28" HorizontalAlignment="Left" Margin="29,134,0,0" Name="label4" VerticalAlignment="Top" Width="90" />
        <DatePicker Height="23" HorizontalAlignment="Left" Margin="121,134,0,0" Name="memberSince" VerticalAlignment="Top" Width="275" />
        <GroupBox Header="资历" Height="200" HorizontalAlignment="Left" Margin="29,174,0,0" Name="yearsExperience" VerticalAlignment="Top" Width="258">
            <StackPanel  Margin="0,0,0,0" Name="stackPanel1">
                <RadioButton Style="{StaticResource bellRingerFontStyle}" Content="Up to 1 year" Height="16" Margin="0,20,0,0" Name="novice" Width="120" />
                <RadioButton Style="{StaticResource bellRingerFontStyle}" Content="1 to 4 year" Height="16"  Margin="0,20,0,0" Name="intermediate"  Width="120"/>
                <RadioButton Style="{StaticResource bellRingerFontStyle}" Content="5 to 9 year" Height="16" Margin="0,20,0,0" Name="experienced" Width="120" />
                <RadioButton Style="{StaticResource bellRingerFontStyle}" Content="10 to more year" Height="16" Margin="0,20,0,0" Name="accoplished" Width="120" />
            </StackPanel>
        </GroupBox>
        <ListBox Height="200" Style="{StaticResource bellRingerFontStyle}" HorizontalAlignment="Left" Margin="310,174,0,0" Name="methods" VerticalAlignment="Top" Width="245" />
        <Button Style="{StaticResource bellRingersStyle}" Content="ADD" Height="23" HorizontalAlignment="Left" Margin="188,388,0,0" Name="add" VerticalAlignment="Top" Width="75" />
        <Button Style="{StaticResource bellRingersStyle}"  Content="Clear" Height="23" HorizontalAlignment="Left" Margin="313,388,0,0" Name="clear" VerticalAlignment="Top" Width="75" />
    </Grid>
</Window>
这是后台的cs代码:using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace BellRingers
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private string[] towers = { "天津", "北京", "大上海", "秦皇岛", "牛头崖" };
        private string[] ringingMethods = { "大声响", "小声响", "蜂鸣", "狗叫", "鸟叫", "猫叫", "狼叫", "呻吟", "啊噢" };
        public MainWindow()
        {
            InitializeComponent();
            this.Reset();
        }
        public void Reset()
        {
            firstName.Text = string.Empty;
            lastName.Text = string.Empty;
            towerNames.Items.Clear();
            foreach (string towerName in towers)
            {
                towerNames.Items.Add(towerNames);
            }
            towerNames.Text = towerNames.Items[0] as string;
            methods.Items.Clear();
            CheckBox method = null;
            foreach (string methodName in ringingMethods)
            {
                method = new CheckBox();
                method.Margin = new Thickness(0, 0, 0, 10);
                method.Content = methodName;
                methods.Items.Add(method);
            }
            isCaptain.IsChecked = false;
            novice.IsChecked = true;
            memberSince.Text = DateTime.Today.ToString();
        }   
      
        
    }
}
这些东西我写了一个上午啊,可运行的时候就是出错啊,看了半天也不知道哪里出错。我用的是VS2010,64位系统,请各位大侠来帮帮忙啊

作者: me黄小峰    时间: 2012-3-7 11:39
错误的信息:未处理 System.Windows.Markup.XamlParseException
  Message=“对类型“BellRingers.MainWindow”的构造函数执行符合指定的绑定约束的调用时引发了异常。”,行号为“3”,行位置为“9”。
  Source=PresentationFramework
  LineNumber=3
  LinePosition=9
  StackTrace:
       在 System.Windows.Markup.XamlReader.RewrapException(Exception e, IXamlLineInfo lineInfo, Uri baseUri)
       在 System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
       在 System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri)
       在 System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
       在 System.Windows.Application.LoadBamlStreamWithSyncInfo(Stream stream, ParserContext pc)
       在 System.Windows.Application.LoadComponent(Uri resourceLocator, Boolean bSkipJournaledProperties)
       在 System.Windows.Application.DoStartup()
       在 System.Windows.Application.<.ctor>b__1(Object unused)
       在 System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
       在 MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
       在 System.Windows.Threading.DispatcherOperation.InvokeImpl()
       在 System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
       在 System.Threading.ExecutionContext.runTryCode(Object userData)
       在 System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
       在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       在 System.Windows.Threading.DispatcherOperation.Invoke()
       在 System.Windows.Threading.Dispatcher.ProcessQueue()
       在 System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       在 MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       在 MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
       在 System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
       在 MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
       在 System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
       在 MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       在 MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
       在 System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
       在 System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
       在 System.Windows.Application.RunDispatcher(Object ignore)
       在 System.Windows.Application.RunInternal(Window window)
       在 System.Windows.Application.Run(Window window)
       在 System.Windows.Application.Run()
       在 BellRingers.App.Main() 位置 C:\Users\海峰\Documents\Visual Studio 2010\Microsoft Press\Chapter 22\BellRingers\BellRingers\obj\x86\Debug\App.g.cs:行号 0
       在 System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       在 System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.InvalidOperationException
       Message=元素已具有逻辑父级。将其附加到新的父级之前必须将其与旧父级断开。
       Source=PresentationFramework
       StackTrace:
            在 MS.Internal.Controls.InnerItemCollectionView.AssertPristineModelChild(Object item)
            在 MS.Internal.Controls.InnerItemCollectionView.Add(Object item)
            在 System.Windows.Controls.ItemCollection.Add(Object newItem)
            在 BellRingers.MainWindow.Reset() 位置 C:\Users\海峰\Documents\Visual Studio 2010\Microsoft Press\Chapter 22\BellRingers\BellRingers\MainWindow.xaml.cs:行号 36
            在 BellRingers.MainWindow..ctor() 位置 C:\Users\海峰\Documents\Visual Studio 2010\Microsoft Press\Chapter 22\BellRingers\BellRingers\MainWindow.xaml.cs:行号 27
       InnerException:





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2