在Xamarin Forms PCL上与ICommand绑定问题

时间:2017-09-09 05:36:17

标签: c# xamarin.forms

我目前正在研究VS2017 pcl项目中的xamarin表单。我正在尝试进行绑定,以通过图像上的轻击手势切换视图的可见性。但是,看起来似乎是使用绑定切换2个视图的可见性的问题。在任何时候,我只想展示其中一个观点。 有人可以帮助我吗?

以下是我项目中的内容,涉及的所有代码

MainViewModel:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Xamarin.Forms;

namespace App3
{
    public class MainViewModel:INotifyPropertyChanged
    {
        private bool _isView1Visible;
        private bool _isView2Visible;
        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            var ev = PropertyChanged;
            ev?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        public bool isView1Visible
        {
            get { return _isView1Visible; }

            set
            {
                if (_isView1Visible != value)
                {
                    _isView1Visible = value;
                    OnPropertyChanged();
                }
            }
        }

        public bool isView2Visible
        {
            get { return _isView2Visible; }

            set
            {
                if (_isView2Visible != value)
                {
                    _isView2Visible = value;
                    OnPropertyChanged();
                }
            }
        } 


        public static void AddTapAction(View view, Action action)
        {
            var tapGestureRecognizer = new TapGestureRecognizer();
            tapGestureRecognizer.Tapped += (s, e) =>
            {
                action();
            };
            view.GestureRecognizers.Add(tapGestureRecognizer);
        }

        public ICommand ShowView1Command
    {
        get
        {
            return _showView1Command ??
                   (_showView1Command =
                       new Command(() =>  showView1()));
        }
    }

        public ICommand ShowView2Command
        {
            get
            {
                return _showView2Command ??
                       (_showView2Command =
                           new Command(() => showView2()));
            }
        }

        bool showView1()
        {
            isView1Visible = true;
            isView2Visible = false;
            return true;
        }

        bool showView2()
        {
            isView1Visible = false;
            isView2Visible = true;
            return true;
        }

        ICommand _showView1Command;
    ICommand _showView2Command;

 }
}

Subview1.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 App3
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class SubView1 : ContentView
    {
        public SubView1()
        {
            ViewModel = new MainViewModel();
            BindingContext = ViewModel;
            InitializeComponent();
        }

        public MainViewModel ViewModel { get; set; }

    }
}

Subview1.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="App3.SubView1">
  <ContentView.Content>
      <StackLayout Orientation="Vertical">
        <Label Text="View 1" IsVisible="{Binding isView1Visible}"></Label>
          <Label Text="View 2" IsVisible="{Binding isView2Visible}"></Label>

        </StackLayout>
  </ContentView.Content>

Subview2.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="App3.SubView2">
  <ContentView.Content>
      <StackLayout>
          <Image x:Name="Btn1" Source="icon.png">
              <Image.GestureRecognizers>
                    <TapGestureRecognizer Command="{Binding ShowView1Command}" CommandParameter="Btn1" />
              </Image.GestureRecognizers>

            </Image>
          <Image x:Name="Btn2" Source="icon.png">
          <Image.GestureRecognizers>
                    <TapGestureRecognizer Command="{Binding ShowView2Command}" CommandParameter="Btn2" />
          </Image.GestureRecognizers></Image>  
        </StackLayout>
  </ContentView.Content>
</ContentView>
    </ContentView>

Subview2.xaml.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 App3
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class SubView2 : ContentView
    {
        public SubView2()
        {
            ViewModel = new MainViewModel();
            BindingContext = ViewModel;
            InitializeComponent();

        }
        public MainViewModel ViewModel { get; set; }
    }
}

MainPage.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"
             xmlns:local="clr-namespace:App3"
             x:Class="App3.MainPage">

     <StackLayout Orientation="Vertical">
         <local:SubView1></local:SubView1>
         <local:SubView2></local:SubView2>
         <Label Text="This is loaded"></Label>
     </StackLayout>

</ContentPage>

MainPage.xaml.cs中

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

namespace App3
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            ViewModel = new MainViewModel();
            BindingContext = ViewModel;
            InitializeComponent();
        }
        public MainViewModel ViewModel { get; set; }

    }
}

0 个答案:

没有答案