Xamarin Forms-如何在另一个内部显示一个xaml的内容 [英] Xamarin Forms - How to display content from one xaml inside another
问题描述
我已经习惯了Android开发,并且在完成我认为很简单的任务时遇到了一些困难.
I'm used to Android development, and am having some difficulty accomplishing what I would think is a simple task.
我有一个MasterDetailPage(称为ContainerView.xaml).
I have a MasterDetailPage (called ContainerView.xaml).
Master是我的导航栏(称为NavbarView.xaml).
The Master is my navigation bar (called NavbarView.xaml).
详细信息"应该是带有固定标题栏的页面,并且可以根据用户选择交换视图".
The Details is supposed to be a page with a fixed title bar, and a "view" I can can swap per user choices.
详细信息"页面称为MainView.xaml.
The Details page is called MainView.xaml.
我想在顶部显示的标题称为TitleBarView.xaml.
The Title I'd like to display at the top and is called TitleBarView.xaml.
然后我有很多内容页面,例如Page1View.xaml.
Then I have a number of content pages such as Page1View.xaml.
在我的ContainerView.xaml中:
in my 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中-这是Master
in my NavbarView.xaml - this is the Master
<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中-这是详细信息
in my MainView.xaml - this is the details
<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中
in my 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>
和通用内容视图,当然,我想在其他许多内容之间进行切换
and a generic content view, of course there will be many others I want to switch between
<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和换出"Content".
I am using the MVVM model and have this code, but can't seem to get just the basics working. The Master page displays fine. If the Details page is just a simple page, it works, but I can't figure out how to insert the TitleBar and swap out the "Content".
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);
我确定我缺少一个基本概念.任何帮助将不胜感激
I'm sure I'm missing a fundamental concept. Any help would be appreciated
推荐答案
Xaml可以实例化在代码或Xaml中定义的任何控件,因此对于TitleBarView,您可以在任何Xaml中实例化它,方法是
Xaml can instantiate any control, defined in code or in Xaml, so in case of TitleBarView, you can instantiate it in any Xaml by
<xmlnsprefix:TitleBarView />
问题是设置正确的xmlnsprefix.每个xaml文件都定义了几个xmlns,您可以添加自己的xmlns,如下所示:
The problem is to set the right xmlnsprefix. Every xaml file define a few xmlns and you can add your own, like this:
xmlns:local="clr-namespace:MyApp"
这意味着Xml名称空间"local"将引用当前程序集中的clr名称空间"MyApp".
and it'll mean that the Xml namespace 'local' will reference the clr namespace 'MyApp' in the current assembly.
因此MainView变为:
So you MainView becomes:
<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>
这篇关于Xamarin Forms-如何在另一个内部显示一个xaml的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!