2017-01-27 58 views
0

我习惯了Android开发,并且在完成我认为是一件简单的任务时遇到了一些困难。Xamarin Forms - 如何在一个xaml内显示另一个内容

我有一个MasterDetailPage(称为ContainerView.xaml)。

师父是我的导航栏(称为NavbarView.xaml)。

细节应该是一个带有固定标题栏的页面,以及一个可以交换每个用户选择的“视图”。

Details页面被称为MainView.xaml。

标题我想在顶部显示,名为TitleBarView.xaml。

然后我有一些内容页面,如Page1View.xaml。

在我ContainerView.xaml

<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"    
        xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
        x:Class="MyApp.ContainerView" 
        IsGestureEnabled="True" 
        MasterBehavior="Popover" 
        Title="MasterDetail Page"> 
    <MasterDetailPage.Master> 
    </MasterDetailPage.Master> 
    <MasterDetailPage.Detail> 
    </MasterDetailPage.Detail> 
</MasterDetailPage> 
在我NavbarView.xaml

- 这是主人

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
      xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
      x:Class="MyApp.NavBarView" 
      Title="Nav Bar"> 
    <ContentPage.Content> 
    <StackLayout Orientation="Vertical"> 
     <Label Text="{Binding Item1}"/> 
     <Button Text="Options" Command="{Binding Option1Command}"/> 
    </StackLayout > 
    </ContentPage.Content> 
</ContentPage> 
在我MainView.xaml

- 这是细节

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
      xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
      x:Class="MyApp.MainView" 
      Title="Main View"> 
    <ContentPage.Content> 
    // what to put here to show the TitleBarView.xaml? 
    // what to put here to show my content xaml pages? 
    </ContentPage.Content> 
</ContentPage> 

在我的TitleBarView.xaml

<?xml version="1.0" encoding="UTF-8"?> 
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
      xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
      x:Class="MyApp.TitleBarView"> 
    <ContentView.Content>  
    <StackLayout Orientation="Horizontal"> 
     <Label Text="{Binding Item1}"/> 
     <Button Text="Options" Command="{Binding OptionsCommand}"/> 
    </StackLayout>  
    </ContentView.Content> 
</ContentView> 

和通用内容来看,当然会有很多人我要和交换机之间

<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
      xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
      x:Class="MyApp.Page1View"> 
    <ContentView.Content> 
    <StackLayout Orientation="Vertical"> 
     <Label Text="{Binding Info}"/> 
     <Button Text="Log In" Command="{Binding GoToPage2Command}"/> 
    </StackLayout > 
    </ContentView.Content> 
</ContentView> 

我使用MVVM模式有这样的代码,但似乎无法得到公正的基本工作。 母版页显示正常。 如果详细信息页面只是一个简单的页面,它可以工作,但我无法弄清楚如何插入TitleBar并换出“内容”。

ContainerView containerPage = new ContainerView(); 
ContainerViewModel containerVM = new ContainerViewModel(); 
containerPage.BindingContext = containerVM; 

NavBarView navigationBar = new NavBarView(); 
navigationBar.Title = "Navigation Bar"; // required, otherwise I get an exception. 
NavBarViewModel navigationBarVM = new NavBarViewModel(); 
navigationBar.BindingContext = navigationBarVM; 

MainView mainView = new MainView(); 
mainView.Title = "MainView"; 
MainViewModel mainViewVM = new MainViewModel(); 
mainView.BindingContext = mainViewVM; 

TitleBarView titleBar = new TitleBarView(); 
TitleBarViewModel titleBarVM = new TitleBarViewModel(); 
titleBar.BindingContext = titleBarVM; 

Page1View page1 = new Page1View(); 
Page1ViewModel page1VM = new Page1ViewModel(); 
page1.BindingContext = page1VM; 

mainView.Content = new StackLayout() 
{ 
    Orientation = StackOrientation.Vertical, 
    Children = 
    { 
     new Label { Text = "I'm Content!" }, 
     new Label { Text = "I'm Content!" }, 
     //titleBar.Content, 
     //loginView.Content 
    } 
}; 

containerPage.MasterBehavior = MasterBehavior.Popover; 
containerPage.Master = navigationBar; 
containerPage.Detail = new NavigationPage(mainView); 

我确定我错过了一个基本概念。任何帮助,将不胜感激

回答

1

的XAML可以实例任何控制,在代码或XAML中定义的,所以在TitleBarView的情况下,可以通过

<xmlnsprefix:TitleBarView /> 

馋它在任何XAML的问题是确立正确的xmlnsprefix。每个XAML文件定义一些的xmlns,你可以添加你自己的,就像这样:

xmlns:local="clr-namespace:MyApp" 

,它会意味着在目前组件的XML命名空间“本地”将引用的CLR命名空间“MyApp的”。

所以你的MainView成为

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
    xmlns:local="clr-namespace:MyApp" 
    x:Class="MyApp.MainView" 
    Title="Main View"> 
    <ContentPage.Content> 
    <local:TitleBarView /> 
    </ContentPage.Content> 
</ContentPage> 
相关问题