Xamarin 移动上位机实现滑动导航

配置+依赖注入

<?xml version="1.0" encoding="utf-8" ?>
<prism:PrismApplication
    x:Class="MyXamarin11.App"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:d="http://xamarin.com/schemas/2014/forms/design"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:prism="http://prismlibrary.com"
    mc:Ignorable="d">
    <Application.Resources>
        <Color x:Key="PrimaryColor">#7E7D81</Color>
        <Color x:Key="AccentColor">#00A5F2</Color>
        <Color x:Key="TextColor">White</Color>

        <Style TargetType="TabbedPage">
            <Setter Property="BarBackgroundColor" Value="{StaticResource PrimaryColor}" />
            <Setter Property="BarTextColor" Value="{StaticResource TextColor}" />
            <Setter Property="SelectedTabColor" Value="{StaticResource AccentColor}" />
            <Setter Property="UnselectedTabColor" Value="{StaticResource PrimaryColor}" />
        </Style>

        <Style TargetType="NavigationPage">
            <Setter Property="BarBackgroundColor" Value="{StaticResource PrimaryColor}" />
            <Setter Property="BarTextColor" Value="{StaticResource TextColor}" />
        </Style>

        <Style TargetType="Button">
            <Setter Property="BackgroundColor" Value="{StaticResource AccentColor}" />
            <Setter Property="TextColor" Value="{StaticResource TextColor}" />
            <Setter Property="CornerRadius" Value="15" />
        </Style>

        <Style x:Key="DarkButton" TargetType="Button">
            <Setter Property="BackgroundColor" Value="{StaticResource PrimaryColor}" />
            <Setter Property="TextColor" Value="{StaticResource TextColor}" />
            <Setter Property="CornerRadius" Value="15" />
        </Style>
    </Application.Resources>
</prism:PrismApplication>
using System;
using Prism.Ioc;
using MyXamarin11.ViewModels;
using MyXamarin11.Views;
using Xamarin.Forms;
using MyXamarin11.Services;

namespace MyXamarin11
{
    public partial class App
    {
        public App()
        {
        }

        protected override async void OnInitialized()
        {
            InitializeComponent();

            //var result = await NavigationService.NavigateAsync("MainPage");
            //var result = await NavigationService.NavigateAsync("NavigationPage/MainPage");

            var result = await NavigationService.NavigateAsync("NavigationPage/MainTabbedPage");

            if (!result.Success)
            {
                System.Diagnostics.Debugger.Break();
            }
        }

        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            //导航注册
            containerRegistry.RegisterForNavigation<MainTabbedPage, MainTabbedPageViewModel>();
            containerRegistry.RegisterForNavigation<NavigationPage>();
            containerRegistry.RegisterForNavigation<TabA, TabViewModel>();
            containerRegistry.RegisterForNavigation<TabB, TabViewModel>();
            containerRegistry.RegisterForNavigation<TabC, TabViewModel>();
            //顶端的公共命令
            containerRegistry.RegisterSingleton<IApplicationCommands, ApplicationCommands>();


            containerRegistry.RegisterForNavigation<MainPage, MainPageViewModel>();
            containerRegistry.RegisterForNavigation<ViewA, ViewAViewModel>();
            containerRegistry.RegisterForNavigation<ViewB, ViewBViewModel>();


            //依赖注入
            //Singleton 单一实例模式:单一实例对象对每个对象和每个请求都是相同的,可以说是不同客户端不同请求都是相同的。
            //Transient 暂时性模式:暂时性对象始终不同,无论是不是同一个请求(同一个请求里的不同服务)同一个客户端,每次都是创建新的实例。
            //Scoped作用域模式:作用域对象在一个客户端请求中是相同的,但在多个客户端请求中是不同的。
            containerRegistry.RegisterSingleton<IExampleAlphaService, ExampleAlphaService>();
            containerRegistry.Register<IExampleBetaService, ExampleBetaService>();
        }
    }



}

主页

<?xml version="1.0" encoding="utf-8" ?>
<TabbedPage
    x:Class="MyXamarin11.Views.MainTabbedPage"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:MyXamarin11.Views"
    xmlns:prism="http://prismlibrary.com"
    Title="Zg滑动导航测试"
    prism:ViewModelLocator.AutowireViewModel="True">
    <!--  Pages can be added as references or inline  -->

    <TabbedPage.ToolbarItems>
        <ToolbarItem Command="{Binding ApplicationCommands.SaveCommand}" Text="所有页面保存时间" />
        <ToolbarItem Command="{Binding ApplicationCommands.ResetCommand}" Text="清除所有界面" />
    </TabbedPage.ToolbarItems>

    <local:TabA />
    <local:TabB />
    <local:TabC />
</TabbedPage>

其他测试页面

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
    x:Class="MyXamarin11.Views.TabA"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:prism="http://prismlibrary.com"
    Title="分页 A"
    prism:ViewModelLocator.AutowireViewModel="True">
    
    <StackLayout>
            <Label Text="{Binding LastSaved}" VerticalOptions="Center" HorizontalOptions="Center" />
            <Button Command="{Binding SaveCommand}" Text="保存时间" />
        </StackLayout>
</ContentPage>
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
    x:Class="MyXamarin11.Views.TabB"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:prism="http://prismlibrary.com"
    Title="分页 B"
    prism:ViewModelLocator.AutowireViewModel="True">


    <StackLayout>
        <Label
            HorizontalOptions="Center"
            Text="{Binding LastSaved}"
            VerticalOptions="Center" />
        <Button Command="{Binding SaveCommand}" Text="保存时间" />
    </StackLayout>
</ContentPage>
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
    x:Class="MyXamarin11.Views.TabC"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:prism="http://prismlibrary.com"
    Title="分页 C"
    prism:ViewModelLocator.AutowireViewModel="True">
    <StackLayout>
        <Label
            HorizontalOptions="Center"
            Text="{Binding LastSaved}"
            VerticalOptions="Center" />
        <Button Command="{Binding SaveCommand}" Text="保存时间" />
    </StackLayout>
</ContentPage>

ViewModel

using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Text;

namespace MyXamarin11.ViewModels
{
    public class MainTabbedPageViewModel: BindableBase
    {
        private IApplicationCommands _applicationCommand;
        public IApplicationCommands ApplicationCommands
        {
            get { return _applicationCommand; }
            set { SetProperty(ref _applicationCommand, value); }
        }

        public MainTabbedPageViewModel(IApplicationCommands applicationCommand)
        {
            _applicationCommand = applicationCommand;
        }
    }
}

using Prism.Commands;
using Prism.Mvvm;
using System;

namespace MyXamarin11.ViewModels
{
    public class TabViewModel : BindableBase
    {
        private string _lastSaved;
        public string LastSaved
        {
            get { return _lastSaved; }
            set { SetProperty(ref _lastSaved, value); }
        }

        //推荐
        public DelegateCommand SaveCommand => new DelegateCommand(Save);
        public DelegateCommand ResetCommand => new DelegateCommand(Reset);


        public TabViewModel(IApplicationCommands applicationCommands)
        {            
            applicationCommands.SaveCommand.RegisterCommand(SaveCommand);
            applicationCommands.ResetCommand.RegisterCommand(ResetCommand);

            //方法2
            //SaveCommand = new DelegateCommand(() =>
            //{
            //    Save();
            //});

            //ResetCommand = new DelegateCommand(() =>
            //{
            //    Reset();
            //});

        }


        private void Save()
        {
            LastSaved = DateTime.Now.ToString();
        }

        private void Reset()
        {
            LastSaved = "Reset - no value";
        }

        //以下可以实现 方法3
        //public DelegateCommand SaveCommand
        //{
        //    get
        //    {
        //        return new DelegateCommand(() =>
        //        {
        //            Save();
        //        });
        //    }
        //}
        //public DelegateCommand ResetCommand
        //{
        //    get
        //    {
        //        return new DelegateCommand(() =>
        //        {
        //            Reset();
        //        });
        //    }
        //}

        //方法2
        //public DelegateCommand SaveCommand { get; private set; }
        //public DelegateCommand ResetCommand { get; private set; }

        /*
         公共命令
            public interface IApplicationCommands
            {
                CompositeCommand SaveCommand { get; }
                CompositeCommand ResetCommand { get; }
            }

            public class ApplicationCommands : IApplicationCommands
            {
                CompositeCommand _saveCommand = new CompositeCommand();
                public CompositeCommand SaveCommand
                {
                    get { return _saveCommand; }
                }

                CompositeCommand _resetCommand = new CompositeCommand();
                public CompositeCommand ResetCommand
                {
                    get { return _resetCommand; }
                }
            }
         */

    }
}

公共命令

using Prism.Commands;

namespace MyXamarin11
{
    public interface IApplicationCommands
    {
        CompositeCommand SaveCommand { get; }
        CompositeCommand ResetCommand { get; }
    }

    public class ApplicationCommands : IApplicationCommands
    {
        CompositeCommand _saveCommand = new CompositeCommand();
        public CompositeCommand SaveCommand
        {
            get { return _saveCommand; }
        }

        CompositeCommand _resetCommand = new CompositeCommand();
        public CompositeCommand ResetCommand
        {
            get { return _resetCommand; }
        }
    }
}

源码:https://download.csdn.net/download/helldoger/85530574

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

潘诺西亚的火山

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值