WPF Page和UserControl的区别

Page 是从 UserControl 继承的,首先就有着“is a UserControl” 的关系;
Page 封装了对 Frame 的导航,这也是 Page 的最大特点和职责,而 UserControl 没有这个职责。

因为 UserControl 从 UIElement 继承,这样就导致了 UserControl 可以用在  
  this.RootVisual = userControl ;
  Grid.Children.Add( userControl );
  等等地方使用(其效果往往就是使UserControl呈现在界面上),而由于“Page is a UserControl” ,这就往往让人搞不清楚是要使用Page还是UserControl。在使用场合上,个人认为是顾名思义就可以了,如果你用过 ASP.NET ,那么 Page 就相当于是一个个的 .aspx 页面,而 UserControl 则是 .aspx 中使用的自定义控件。
转载注明出处,源网址:http://blog.163.com/liulei0729@126/blog/static/32530612201211362532114/
自己做东西的时候编译报错:
未处理System.Windows.Markup.XamlParseException
  HResult=-2146233087
  Message=“对类型“WpfApplication2.MainWindow”的构造函数执行符合指定的绑定约束的调用时引发了异常。”,行号为“3”,行位置为“9”。
  Source=PresentationFramework
  LineNumber=3
  LinePosition=9
  StackTrace:
       在 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.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       在 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.LegacyInvokeImpl(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.Threading.Dispatcher.Run()
       在 System.Windows.Application.RunDispatcher(Object ignore)
       在 System.Windows.Application.RunInternal(Window window)
       在 System.Windows.Application.Run(Window window)
       在 System.Windows.Application.Run()
       在 WpfApplication2.App.Main() 位置 e:\2013program\WpfApplication2\WpfApplication2\obj\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.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       在 System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.InvalidOperationException
       HResult=-2146233079
       Message=Page 只能具有 Window 或 Frame 父级。
       Source=PresentationFramework
       StackTrace:
            在 System.Windows.Controls.Page.OnVisualParentChanged(DependencyObject oldParent)
            在 System.Windows.Media.Visual.FireOnVisualParentChanged(DependencyObject oldParent)
            在 System.Windows.Media.Visual.AddVisualChild(Visual child)
            在 System.Windows.Media.VisualCollection.ConnectChild(Int32 index, Visual value)
            在 System.Windows.Media.VisualCollection.Add(Visual visual)
            在 System.Windows.Controls.UIElementCollection.AddInternal(UIElement element)
            在 System.Windows.Controls.UIElementCollection.Add(UIElement element)
            在 WpfApplication2.MainWindow.init() 位置 e:\2013program\WpfApplication2\WpfApplication2\MainWindow.xaml.cs:行号 38
            在 WpfApplication2.MainWindow..ctor() 位置 e:\2013program\WpfApplication2\WpfApplication2\MainWindow.xaml.cs:行号 26
       InnerException: 
这报错,我能看懂的只有这句:
Message=“对类型“WpfApplication2.MainWindow”的构造函数执行符合指定的绑定约束的调用时引发了异常。”,行号为“3”,行位置为“9”。
  Source=PresentationFramework
  LineNumber=3
  LinePosition=9
上网查了半天,由对比其他人写的东西发现了page和usercontrol这里有不同。也正好是用到文章里说的那个函数在报错。查到这个我还是挺高兴的。但是只把xaml和相应的.cs改了好像还是没起到我想要的效果,虽然不报错。我还是再去看看吧。。
我想在一个grid中嵌套一个grid1,然后在鼠标进入grid1的范围以后在窗口中显示一些图像。就像下棋,在棋盘里显示棋子一颗一颗落下的图片。路过的有经验的朋友给我提些建议吧~图像总是显不出来。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值