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的範圍以後在窗口中顯示一些圖像。就像下棋,在棋盤裏顯示棋子一顆一顆落下的圖片。路過的有經驗的朋友給我提些建議吧~圖像總是顯不出來。
發佈了25 篇原創文章 · 獲贊 6 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章