2016-08-18 36 views
0

我想寫一個Xamarin Page類,模仿Pivot控件,該控件在Windows Phone上由TabbedPage使用,但在Windows桌面上不可用。因此,我想讓它擁有幾個子女Page s,其中只有一個會在同一時間顯示,同時還有一個控制它自己的控件,允許用戶在孩子之間切換。我將如何去做這件事?如何創建一個可以託管其他頁面的Xamarin頁面?

回答

0

你正在尋找CarouselView。它在上使用FlipView WinRTUWP平臺。

你可以從Xamarin blogXamarinhelp.com找到更多的信息。他們還有預發佈nuget包。

+0

CarouselView託管視圖,而不是頁面。 – Simon

0

我想你必須自己寫。使用CarouselView,將它添加到Grid或AbsoluteLayout或RelativeLayout(我的意思是,將它放在背景上,這樣您就可以在其上添加標題的旋轉木馬,或者您可能不想那樣做,在這種情況下,您只需使用垂直堆棧佈局)。您還需要爲導航添加自定義控件,並同步頁面標題輪播和實際頁面輪播之間的更改。

0

我發現this blog post關於如何在視圖中託管頁面。在此基礎上,我提出了以下觀點類:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

using Xamarin.Forms; 

namespace SGB 
{ 
    public class PageView : View 
    { 
     public PageView() 
     { 
     } 

     public static readonly BindableProperty ContentProperty = BindableProperty.Create<PageView,Page> (s => s.Content, null); 

     public Page Content { 
      get 
      { 
       return (Page)GetValue (ContentProperty); 
      } 
      set 
      { 
       SetValue (ContentProperty, value); 
       LayoutContent(); 
      } 
     } 

     protected override void OnSizeAllocated(double width, double height) 
     { 
      base.OnSizeAllocated(width, height); 
      LayoutContent(); 
     } 

     private void LayoutContent() 
     { 
      if (Content != null) 
      { 
       Content.Layout(new Rectangle(0, 0, Width, Height)); 
      } 
     } 
    } 
} 

與此渲染器類:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

using Windows.UI; 
using Windows.UI.Xaml; 
using Windows.UI.Xaml.Controls; 

using Xamarin.Forms; 
using Xamarin.Forms.Platform.WinRT; 

using Page = Xamarin.Forms.Page; 

using SGB; 

[assembly: ExportRenderer(typeof(PageView), typeof(SGB.Windows.PageViewRenderer))] 
namespace SGB.Windows 
{ 
    public class PageViewRenderer : ViewRenderer<PageView, FrameworkElement> 
    { 
     private Page currentPage; 
     private FrameworkElement FrameworkElement; 

     public PageViewRenderer() 
      : base() 
     { 
     } 

     protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) 
     { 
      base.OnElementPropertyChanged(sender, e); 

      if ((e.PropertyName == "Content") || (e.PropertyName == "Renderer")) 
      { 
       SetPage(((PageView)Element).Content); 
      } 
     } 

     private void SetPage(Page page) 
     { 
      if (page == currentPage) 
      { 
       return; 
      } 

      if (currentPage != null) 
      { 
       ((IPageController)page).SendDisappearing(); 

       currentPage.Parent = null; 
      } 

      currentPage = page; 

      if (page != null) 
      { 
       var renderer = page.GetOrCreateRenderer(); 
       FrameworkElement = renderer.ContainerElement; 
       SetNativeControl(FrameworkElement); 

       page.Parent = FindPage(); 

       ((IPageController)page).SendAppearing(); 
      } 
     } 

     private Page FindPage() 
     { 
      for (Element element = Element; element != null; element = element.Parent) 
      { 
       if (element is Page) 
       { 
        return (Page)element; 
       } 
      } 

      return null; 
     } 
    } 
} 

現在我有,我可以創建一個包含PageView一個ContentPage,我有我想要的。

相關問題