添加XAML的C#代码出现问题[未处理XAML分析错误] [英] Issue with C# code for adding XAML [XAML parse error was unhandled]

查看:200
本文介绍了添加XAML的C#代码出现问题[未处理XAML分析错误]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一块从上到下均匀间隔的画布上绘制大量线条(当我将其工作为一种方法时,它也会从左到右).我的方法可能全错了,或者有一些我找不到的错误.请查看我的代码,看看是否可以找到它.我还包含了详细的错误消息:

C#代码(令人讨厌的部分;在程序启动时执行):

I want to draw a huge number of line in a canvas uniformly spaced from top to bottom, (will have left to right also, when I''ve med it work one way). My approach might be all wrong, or there are some error I can''t find. Please review my code and see if you can find it. I have also included the detailed error message:

C# code, (the offending part; executed at program start):

public void createGrid()
{
    int Ypositions = -1000;

    while (Ypositions < 1001)
    {
        // Create a white Brush
        SolidColorBrush whiteBrush = new SolidColorBrush();
        whiteBrush.Color = Colors.White;
        
        // Add line
        Line gridLine = new Line();
        gridLine.X1 = 1;
        gridLine.Y1 = 1;
        gridLine.X2 = 1;
        gridLine.Y2 = 2000;
        gridLine.SetValue(Canvas.LeftProperty, 1);
        gridLine.SetValue(Canvas.TopProperty, Ypositions);
        gridLine.Stroke = whiteBrush;
        gridLine.StrokeThickness = 1;
        gridLine.SetValue(Panel.ZIndexProperty, -900);
        
        // Increment by ten
        Ypositions += 10;

        gridPattern.Children.Add(gridLine);
    }     
}


让我彻底弄清楚自己,如果不执行此代码段,程序将正常运行!

XAML代码:


Let me make myself completely clear, if I don''t execute this snippet of code the program works flawlessly!

XAML code:

<TabItem Header="Starmap" Name="tab_itm_starmap">
    <Grid>
        <Canvas removed="Black" Panel.ZIndex="-1500" />
        <Canvas Name="gridPattern"

                VerticalAlignment="Center"

                HorizontalAlignment="Center"

                removed="Blue"

                Height="1px"

                Width="1px"

                Panel.ZIndex="-1100">
            <!-- Function createGrid() places Lines here on program startup -->
        </Canvas>
    </Grid>  
</TabItem>



详细错误消息:



Detailed Error message:

System.Windows.Markup.XamlParseException was unhandled
  Message='The invocation of the constructor on type 'WpfApplication1.MainWindow' that matches the specified binding constraints threw an exception.' Line number '3' and line position '9'.
  Source=PresentationFramework
  LineNumber=3
  LinePosition=9
  StackTrace:
       at System.Windows.Markup.XamlReader.RewrapException(Exception e, IXamlLineInfo lineInfo, Uri baseUri)
       at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
       at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri)
       at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
       at System.Windows.Application.LoadBamlStreamWithSyncInfo(Stream stream, ParserContext pc)
       at System.Windows.Application.LoadComponent(Uri resourceLocator, Boolean bSkipJournaledProperties)
       at System.Windows.Application.DoStartup()
       at System.Windows.Application.<.ctor>b__1(Object unused)
       at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
       at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
       at System.Windows.Threading.DispatcherOperation.InvokeImpl()
       at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
       at System.Threading.ExecutionContext.runTryCode(Object userData)
       at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Windows.Threading.DispatcherOperation.Invoke()
       at System.Windows.Threading.Dispatcher.ProcessQueue()
       at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
       at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
       at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
       at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
       at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
       at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
       at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
       at System.Windows.Application.RunDispatcher(Object ignore)
       at System.Windows.Application.RunInternal(Window window)
       at System.Windows.Application.Run(Window window)
       at System.Windows.Application.Run()
       at WpfApplication1.App.Main() in C:\Users\Frank Haugen\Documents\Visual Studio 2010\Projects\WpfApplication1\WpfApplication1\obj\x86\Debug\App.g.cs:line 0
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.ArgumentException
       Message='0' is not a valid value for property 'Left'.
       Source=WindowsBase
       StackTrace:
            at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
            at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
            at WpfApplication1.MainWindow.createGrid() in C:\Users\Frank Haugen\Documents\Visual Studio 2010\Projects\WpfApplication1\WpfApplication1\MainWindow.xaml.cs:line 107
            at WpfApplication1.MainWindow..ctor() in C:\Users\Frank Haugen\Documents\Visual Studio 2010\Projects\WpfApplication1\WpfApplication1\MainWindow.xaml.cs:line 27
       InnerException: 



非常感谢您提供的所有帮助!



Thanks very much for any and all help!

推荐答案


TileBrush类 [ ^ ]

希望对您有所帮助.
罗马
WPF - How can I make a brush that paints graph-paper-like squares?[^]

As i understand you want to draw a grid.
I am using approach that posted above, for drawing ECG like paper in my application. DrawingBrush is what you need, draw 2 lines and then use the TileMode="Tile" property.

TileBrush Class[^]

I hope it''s helps.
Roman


这篇关于添加XAML的C#代码出现问题[未处理XAML分析错误]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆