我想定制Xamarin MasterDetail(Xamarin Crossplatform项目)

时间:2017-06-06 12:11:54

标签: xamarin xamarin.forms uwp-xaml master-detail

只需查看下面的图片即可。我想更改图像上标记区域的颜色。该图是使用 xamarin表单跨平台项目 (PCL)开发的UWP应用程序的屏幕截图。 我正在使用MasterDetail页面导航。

enter image description here

现在根据我的第一个要求改变颜色。

现在我想要的是下面的屏幕设计 enter image description here

现在我得到的结果如下所示 result of first screen 第二个屏幕的结果,即导航到股票输入页面之后 Result of second screen ie, after navigating to stock entry page

源代码

1。 MasterDetailsPage.Xaml

<?xml version="1.0" encoding="utf-8" ?>
<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="GST.Views.MasterDetailsPage"
             Title="Stock Manager"

                  MasterBehavior="SplitOnPortrait"
              BackgroundColor="#0063b1"
             xmlns:pages="clr-namespace:GST.Views;assembly=GST">

    <MasterDetailPage.Master >
        <pages:MasterDetailsPageMaster  x:Name="MasterPage" />
  </MasterDetailPage.Master>

    <MasterDetailPage.Detail>

        <NavigationPage >
      <x:Arguments>
        <pages:GST_Home  />

      </x:Arguments>
    </NavigationPage> 
  </MasterDetailPage.Detail>
</MasterDetailPage>

2。 MasterDetailsPage.cs

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

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace GST.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class MasterDetailsPage : MasterDetailPage
    {
        public MasterDetailsPage()
        {
            InitializeComponent();
            MasterPage.ListView.ItemSelected += ListView_ItemSelected;           
        }

        private void ListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
        {
            var item = e.SelectedItem as MasterDetailsPageMenuItem;
            if (item == null)
                return;



            var page = (Page)Activator.CreateInstance(item.TargetType);
            Detail.Title = item.Title;


            IsPresented = false;

             Detail.Navigation.PushAsync(page);

            MasterPage.ListView.SelectedItem = null;

        }
    }
}

第3。 MasterDetailsPageDetail.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"
             x:Class="GST.Views.MasterDetailsPageDetail"
             BackgroundColor="{StaticResource Key=background-color}"
               Title="Master GST Title Detailed" >
  <StackLayout Padding="10">
    <Label Text="This is a detail page. To get the 'triple' line icon on each platform add a icon to each platform and update the 'Master' page with an Icon that references it."/>
  </StackLayout>
</ContentPage>

4.MasterDetailsPageDetail.cs

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

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace GST.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class MasterDetailsPageDetail : ContentPage
    {
        public MasterDetailsPageDetail()
        {
            InitializeComponent();

           // NavigationPage.SetHasBackButton(this, false);
            // NavigationPage.SetHasNavigationBar(this, false);
        }
    }
}

5。 MasterDetailsPageMaster.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"
             x:Class="GST.Views.MasterDetailsPageMaster"

             Title="Home">
  <StackLayout>
    <ListView x:Name="ListViewMenuItems"
              SeparatorVisibility="None"
              HasUnevenRows="true"
              ItemsSource="{Binding MenuItems}">
      <ListView.Header>
        <Grid BackgroundColor="#03A9F4">
          <Grid.ColumnDefinitions>
            <ColumnDefinition Width="10"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="10"/>
          </Grid.ColumnDefinitions>
          <Grid.RowDefinitions>
            <RowDefinition Height="30"/>
            <RowDefinition Height="80"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="10"/>
          </Grid.RowDefinitions>
          <Label
              Grid.Column="1"
              Grid.Row="2"
              Text="AppName"
              Style="{DynamicResource SubtitleStyle}"/>
        </Grid>
      </ListView.Header>
      <ListView.ItemTemplate>
        <DataTemplate>
          <ViewCell>
            <StackLayout Padding="15,10" HorizontalOptions="FillAndExpand">
              <Label VerticalOptions="FillAndExpand" 
                    VerticalTextAlignment="Center" 
                    Text="{Binding Title}" 
                    FontSize="24"/>
            </StackLayout>
          </ViewCell>
        </DataTemplate>
      </ListView.ItemTemplate>
    </ListView>
  </StackLayout>
</ContentPage>

6.MasterDetailsPageMaster.cs

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace GST.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class MasterDetailsPageMaster : ContentPage
    {
        public ListView ListView => ListViewMenuItems;

        public MasterDetailsPageMaster()
        {
            InitializeComponent();
            BindingContext = new ViewModels.MasterDetailsPageMasterViewModel();

        }

    }
}

7.MasterDetailsPageMenuItem.cs

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

namespace GST.Views
{

    public class MasterDetailsPageMenuItem
    {
        public MasterDetailsPageMenuItem()
        {
            TargetType = typeof(MasterDetailsPageDetail);
        }
        public int Id { get; set; }
        public string Title { get; set; }

        public Type TargetType { get; set; }
    }
}

8.GST_Home.xaml - Home Dash

 <?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             NavigationPage.HasNavigationBar="False"
             Title="Home Dash"
             x:Class="GST.Views.GST_Home">

    <StackLayout Padding="10">
        <Label Text="This is a detail page. To get the 'triple' line icon on each platform add a icon to each platform and update the 'Master' page with an Icon that references it."/>
    </StackLayout>
</ContentPage>

9.GST_Home.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;   
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace GST.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class GST_Home : ContentPage
    {
        public GST_Home()
        {
            InitializeComponent();
        }
    }
}

如果您需要完整的源代码,请参阅我在Google云端硬盘上上传的代码:https://drive.google.com/file/d/0B2XtD2dQvEhzb1NnNGdydG91S0k/view?usp=sharing

1 个答案:

答案 0 :(得分:3)

如果您希望通过应用程序保持标题栏的颜色,那么您可以在Xamarin.UWP客户端项目的App.xaml.cs中的OnLaunched内编写以下代码。有关更多信息,请参阅Customizing Title Bar and Status Bar in Universal Windows Platform (UWP)

if (ApiInformation.IsTypePresent("Windows.UI.ViewManagement.ApplicationView"))
{
    var titleBar = ApplicationView.GetForCurrentView().TitleBar;
    if (titleBar != null)
    {
        titleBar.ButtonBackgroundColor = Colors.Red;
        titleBar.ButtonForegroundColor = Colors.White;
        titleBar.BackgroundColor = Colors.Red;
        titleBar.ForegroundColor = Colors.White;
    }
}

如果您想自定义工具栏背景颜色,可以设置BarBackgroundColor的{​​{1}}。您可以执行类似以下代码的操作。

NavigationPage

enter image description here