C# Xamarin.Forms+;棱镜导航目标控制

C# Xamarin.Forms+;棱镜导航目标控制,c#,navigation,xamarin.forms,prism,C#,Navigation,Xamarin.forms,Prism,我有一个Xamarin应用程序,由几种页面类型组成。我用的是棱镜 我希望应用程序的导航目标是主页中应用程序中的嵌套视图,而不是主页本身,以便主页用作始终存在的外部框架,嵌套页面承载应用程序的导航服务。 通过这种方式,我可以始终使用一些控件(即后退/前进/主页)。 如何将导航服务主页配置为实际主页中的嵌套页面? 我对XF非常陌生,以前没有使用过导航页面,但它在这里有用吗?我有一种方法。不完全是你想要的,但可以让你思考。您可以使用SomeContent属性创建SomeBaseContentPage:

我有一个Xamarin应用程序,由几种页面类型组成。我用的是棱镜

我希望应用程序的导航目标是主页中应用程序中的嵌套视图,而不是主页本身,以便
主页
用作始终存在的外部框架,嵌套页面承载应用程序的导航服务。
通过这种方式,我可以始终使用一些控件(即后退/前进/主页)。 如何将
导航服务
主页配置为实际
主页
中的嵌套页面?

我对XF非常陌生,以前没有使用过
导航页面
,但它在这里有用吗?

我有一种方法。不完全是你想要的,但可以让你思考。您可以使用
SomeContent
属性创建
SomeBaseContentPage

public View SomeContent
{
  set
  {
    someGrid.Children.Add(value);
  }
}

然后从
SomeBaseContentPage
派生您的
SomeContentPageA
,并在
XAML
中将您的特定内容放入属性

我有一种方法。不完全是你想要的,但可以让你思考。您可以使用
SomeContent
属性创建
SomeBaseContentPage

public View SomeContent
{
  set
  {
    someGrid.Children.Add(value);
  }
}

然后从
SomeBaseContentPage
派生您的
SomeContentPageA
,并在
XAML
中将您的特定内容放入属性

中,您不需要这样做。Prism的导航服务将不支持您尝试执行的操作,因为
INavigationService
是基于
页面的。听起来你的方法很不正统。为什么不直接使用
导航页面
并在自定义渲染器中或作为工具栏项提供选项呢?

您没有。Prism的导航服务将不支持您尝试执行的操作,因为
INavigationService
是基于
页面的。听起来你的方法很不正统。为什么不直接使用
导航页面
并在自定义渲染器中或作为工具栏项提供选项?

Prism的
INavigationService
并不是为完成您希望完成的任务而设计的。幸运的是,自定义视图非常有用!您可以按照前面的建议进行操作,但我通常会避免创建自定义页面,而是创建一个具有自己内容属性的自定义视图

编辑: 这个术语可能会与Xamarin表单特别混淆,尤其是如果您是MVVM和Xamarin表单的新手,并且/或者已经从在其他平台上开发转移到其他平台上

Xamarin表单所指的
视图
在其他平台上通常被称为
控件
。当使用诸如Prism这样的MVVM框架时,这尤其令人困惑,因为我们的MVVM
视图实际上是Xamarin表单
页面

虽然有时我们需要创建一个自定义类型的页面,但通常我们只是实现一些页面类型。例如,假设我有一个用于登录页面的页面:

LoginPage.xaml

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             Title="{Binding Title}"
             x:Class="MyAwesomeApp.Views.LoginPage">

    <Grid BackgroundColor="{DynamicResource primary}">
        <Grid.RowDefinitions>
            <RowDefinition Height="1*" />
            <RowDefinition Height="3*" />
            <RowDefinition Height="1*" />
        </Grid.RowDefinitions>
        <Grid.Resources>
            <ResourceDictionary>
                <Style TargetType="StackLayout">
                    <Setter Property="BackgroundColor" Value="White" />
                    <Setter Property="VerticalOptions" Value="FillAndExpand" />
                </Style>
                <Style TargetType="Entry">
                    <Setter Property="Margin" Value="40,10" />
                </Style>
                <Style TargetType="Button">
                    <Setter Property="Margin" Value="20" />
                    <Setter Property="BackgroundColor" Value="{DynamicResource primaryDark}" />
                    <Setter Property="TextColor" Value="White" />
                    <Setter Property="BorderRadius" Value="5" />
                    <Setter Property="BorderWidth" Value="1" />
                    <Setter Property="BorderColor" Value="Black" />
                </Style>
            </ResourceDictionary>
        </Grid.Resources>

        <Image Source="logo.png" 
               Aspect="AspectFit"
               HorizontalOptions="Center"
               VerticalOptions="Center" />

        <StackLayout Grid.Row="1">
            <Entry Text="{Binding UserName}" 
                   Placeholder="Enter your username"
                   x:Name="userNameEntry"
                   VerticalOptions="EndAndExpand" />
            <Entry Text="{Binding Password}" 
                   Placeholder="Enter your password"
                   IsPassword="True"
                   x:Name="passwordEntry"
                   VerticalOptions="StartAndExpand" />
            <Button Text="Submit"
                    Command="{Binding LoginCommand}" />
        </StackLayout>

    </Grid>

</ContentPage>
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:controls="clr-namespace:MyAwesomeApp.Controls"
             Title="{Binding Title}"
             x:Class="MyAwesomeApp.Views.LoginPage">

        <controls:LoginView UserName="{Binding UserName}" 
                            Password="{Binding Password}"
                            LoginCommand="{Binding LoginCommand}" />
</ContentPage>
虽然XAML中有很多内容,但这只是一个内容页面。无论如何,它都不是一个自定义页面。您会注意到,这里的代码中完全没有任何内容。我可能会创建一些事件处理程序,例如,如果用户点击键盘上的Done/Enter,进入下一个字段,或者调用按钮单击,但这仍然不能使其成为自定义页面

有关“自定义页面”的示例,您可以查看。您会注意到,在选项卡页面的版本中有几个新属性。当然,他们的页面之所以有效,是因为

按照我已经展示的示例,假设我们希望采用我在页面中创建的登录布局,并希望能够跨项目重用它,或者在我的应用程序的不同页面上重用它。然后我想做一些类似的事情:

LoginView.xaml

<?xml version="1.0" encoding="UTF-8"?>
<Grid xmlns="http://xamarin.com/schemas/2014/forms" 
      xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
      BackgroundColor="{DynamicResource primary}"
      x:Name="grid"
      x:Class="MyAwesomeApp.Controls.LoginView">

    <Grid.RowDefinitions>
        <RowDefinition Height="1*" />
        <RowDefinition Height="3*" />
        <RowDefinition Height="1*" />
    </Grid.RowDefinitions>
    <Grid.Resources>
        <ResourceDictionary>
            <Style TargetType="StackLayout">
                <Setter Property="BackgroundColor" Value="White" />
                <Setter Property="VerticalOptions" Value="FillAndExpand" />
            </Style>
            <Style TargetType="Entry">
                <Setter Property="Margin" Value="40,10" />
            </Style>
            <Style TargetType="Button">
                <Setter Property="Margin" Value="20" />
                <Setter Property="BackgroundColor" Value="{DynamicResource primaryDark}" />
                <Setter Property="TextColor" Value="White" />
                <Setter Property="BorderRadius" Value="5" />
                <Setter Property="BorderWidth" Value="1" />
                <Setter Property="BorderColor" Value="Black" />
            </Style>
        </ResourceDictionary>
    </Grid.Resources>

    <Image Source="logo.png" 
           Aspect="AspectFit"
           HorizontalOptions="Center"
           VerticalOptions="Center" />

    <StackLayout Grid.Row="1" BindingContext="{x:Reference grid}">
        <Entry Text="{Binding UserName}" 
               Placeholder="Enter your username"
               x:Name="userNameEntry"
               VerticalOptions="EndAndExpand" />
        <Entry Text="{Binding Password}" 
               Placeholder="Enter your password"
               IsPassword="True"
               x:Name="passwordEntry"
               VerticalOptions="StartAndExpand" />
        <Button Text="Submit"
                Command="{Binding LoginCommand}" />
    </StackLayout>

</Grid>
折射LoginPage.xaml

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             Title="{Binding Title}"
             x:Class="MyAwesomeApp.Views.LoginPage">

    <Grid BackgroundColor="{DynamicResource primary}">
        <Grid.RowDefinitions>
            <RowDefinition Height="1*" />
            <RowDefinition Height="3*" />
            <RowDefinition Height="1*" />
        </Grid.RowDefinitions>
        <Grid.Resources>
            <ResourceDictionary>
                <Style TargetType="StackLayout">
                    <Setter Property="BackgroundColor" Value="White" />
                    <Setter Property="VerticalOptions" Value="FillAndExpand" />
                </Style>
                <Style TargetType="Entry">
                    <Setter Property="Margin" Value="40,10" />
                </Style>
                <Style TargetType="Button">
                    <Setter Property="Margin" Value="20" />
                    <Setter Property="BackgroundColor" Value="{DynamicResource primaryDark}" />
                    <Setter Property="TextColor" Value="White" />
                    <Setter Property="BorderRadius" Value="5" />
                    <Setter Property="BorderWidth" Value="1" />
                    <Setter Property="BorderColor" Value="Black" />
                </Style>
            </ResourceDictionary>
        </Grid.Resources>

        <Image Source="logo.png" 
               Aspect="AspectFit"
               HorizontalOptions="Center"
               VerticalOptions="Center" />

        <StackLayout Grid.Row="1">
            <Entry Text="{Binding UserName}" 
                   Placeholder="Enter your username"
                   x:Name="userNameEntry"
                   VerticalOptions="EndAndExpand" />
            <Entry Text="{Binding Password}" 
                   Placeholder="Enter your password"
                   IsPassword="True"
                   x:Name="passwordEntry"
                   VerticalOptions="StartAndExpand" />
            <Button Text="Submit"
                    Command="{Binding LoginCommand}" />
        </StackLayout>

    </Grid>

</ContentPage>
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:controls="clr-namespace:MyAwesomeApp.Controls"
             Title="{Binding Title}"
             x:Class="MyAwesomeApp.Views.LoginPage">

        <controls:LoginView UserName="{Binding UserName}" 
                            Password="{Binding Password}"
                            LoginCommand="{Binding LoginCommand}" />
</ContentPage>


对于
LoginView
,您最终得到的实际上是一个自定义控件。虽然它可能来自网格,但您通常不关心我如何决定创建布局。。。您只关心我给了您一些控件,这些控件具有可以附加到的
用户名
密码
属性,以及一些可以执行的
登录命令
。事实上,正是这种固有的差异使它成为了一种自定义视图。

Prism的INavigationService
并不是为实现您所期望的功能而设计的。幸运的是,自定义视图非常有用!您可以按照前面的建议进行操作,但我通常会避免创建自定义页面,而是创建一个具有自己内容属性的自定义视图

编辑: 这个术语可能会与Xamarin表单特别混淆,尤其是如果您是MVVM和Xamarin表单的新手,并且/或者已经从在其他平台上开发转移到其他平台上

Xamarin表单所指的
视图
在其他平台上通常被称为
控件
。当使用诸如Prism这样的MVVM框架时,这尤其令人困惑,因为我们的MVVM
视图实际上是Xamarin表单
页面

虽然有时我们需要创建一个自定义类型的页面,但通常我们只是实现一些页面类型。例如,假设我有一个用于登录页面的页面:

LoginPage.xaml

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             Title="{Binding Title}"
             x:Class="MyAwesomeApp.Views.LoginPage">

    <Grid BackgroundColor="{DynamicResource primary}">
        <Grid.RowDefinitions>
            <RowDefinition Height="1*" />
            <RowDefinition Height="3*" />
            <RowDefinition Height="1*" />
        </Grid.RowDefinitions>
        <Grid.Resources>
            <ResourceDictionary>
                <Style TargetType="StackLayout">
                    <Setter Property="BackgroundColor" Value="White" />
                    <Setter Property="VerticalOptions" Value="FillAndExpand" />
                </Style>
                <Style TargetType="Entry">
                    <Setter Property="Margin" Value="40,10" />
                </Style>
                <Style TargetType="Button">
                    <Setter Property="Margin" Value="20" />
                    <Setter Property="BackgroundColor" Value="{DynamicResource primaryDark}" />
                    <Setter Property="TextColor" Value="White" />
                    <Setter Property="BorderRadius" Value="5" />
                    <Setter Property="BorderWidth" Value="1" />
                    <Setter Property="BorderColor" Value="Black" />
                </Style>
            </ResourceDictionary>
        </Grid.Resources>

        <Image Source="logo.png" 
               Aspect="AspectFit"
               HorizontalOptions="Center"
               VerticalOptions="Center" />

        <StackLayout Grid.Row="1">
            <Entry Text="{Binding UserName}" 
                   Placeholder="Enter your username"
                   x:Name="userNameEntry"
                   VerticalOptions="EndAndExpand" />
            <Entry Text="{Binding Password}" 
                   Placeholder="Enter your password"
                   IsPassword="True"
                   x:Name="passwordEntry"
                   VerticalOptions="StartAndExpand" />
            <Button Text="Submit"
                    Command="{Binding LoginCommand}" />
        </StackLayout>

    </Grid>

</ContentPage>
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:controls="clr-namespace:MyAwesomeApp.Controls"
             Title="{Binding Title}"
             x:Class="MyAwesomeApp.Views.LoginPage">

        <controls:LoginView UserName="{Binding UserName}" 
                            Password="{Binding Password}"
                            LoginCommand="{Binding LoginCommand}" />
</ContentPage>
虽然XAML中有很多内容,但这只是一个内容页面。无论如何,它都不是一个自定义页面。您会注意到,这里的代码中完全没有任何内容。我可能会创建一些事件处理程序,例如,如果用户点击键盘上的Done/Enter,进入下一个字段,或者调用按钮单击,但这仍然不能使其成为自定义页面

有关“自定义页面”的示例,您可以签出t