2016-08-11 87 views
2

首先感谢我的英语。 我使用Xamarin.FormXamarin可重复使用的xaml用户控件和自定义命令

一个iOS和Android项目的工作,我想有一个“XAML用户控制”在不同的方式重复使用,我需要使它与点击的ICommand

这是StackLayoutButton组分:

<?xml version="1.0" encoding="utf-8" ?> 
<StackLayout xmlns="http://xamarin.com/schemas/2014/forms" 
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
x:Class="Installa.Controls.StackLayoutButton"> 
    <Image x:Name="Icon" Source="{Binding Icon}" /> 
    <Label x:Name="Text" Text="{Binding Title}" HorizontalOptions="Center" LineBreakMode="NoWrap" Font="Small" TextColor="Red" /> 
</StackLayout> 

这在CalendarioPage 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:controls="clr-namespace:Installa.Controls;assembly=Installa" 
      x:Class="Installa.CalendarioPage"> 

    <StackLayout> 
    <Label Text="{Binding ViewName}" Font="42" IsVisible="{Binding IsWindowsPhone}" /> 
    <ActivityIndicator IsRunning="{Binding IsLoading}" IsVisible="{Binding IsLoading}" Color="Red" /> 

    <controls:StackLayoutButton BindingContext="{Binding Blog}" TextColor="Blue" /> <!-- Command="{Binding SubmitCommand}" --> 
    <controls:StackLayoutButton BindingContext="{Binding Facebook}" TextColor="Red" /> <!-- Command="{Binding SubmitCommand}" --> 
    </StackLayout> 

</ContentPage> 

这是个ËCalendarioPage C#页:

public partial class CalendarioPage : ContentPage 
{ 
    private CalendarioViewModel vm; 

    public CalendarioPage() 
    { 
     InitializeComponent(); 

     vm = new CalendarioViewModel(); 

     this.BindingContext = vm; 
    } 
} 

这是ViewModel类:

namespace Installa 
{ 
    public class CalendarioViewModel: BaseViewModel 
    { 

     public CalendarioViewModel() 
     { 
      blog = new Activity(); 
      blog.Link = "www.google.it"; 
      blog.Title = "Titolo del blog"; 
      blog.Icon = "logomenu.png"; 

      facebook = new Activity(); 
      facebook.Title = "Tito Fbook"; 
      facebook.Link = "www.facebook.it"; 
      facebook.Icon = "icon.png"; 

      ViewName = "nome della view"; 
      IsLoading = false;  
     } 

     Activity blog = null; 
     public Activity Blog 
     { 
      get {return blog;} 
     } 

     Activity facebook = null; 
     public Activity Facebook 
     { 
      get { return facebook; } 
     } 

     string viewName = string.Empty; 
     public string ViewName 
     { 
      get { return viewName; } 
      set { SetProperty(ref viewName, value); } 
     } 

     public bool IsWindowsPhone 
     { 
      get 
      { 
       return Device.OS == TargetPlatform.WinPhone; 
      } 
     } 

     bool isLoading = false; 
     public bool IsLoading 
     { 
      get { return isLoading; } 
      set { SetProperty(ref isLoading, value); } 
     }   
    } 
} 

随着活动的简单类:

public string Title { get; set; } 

    public string Link { get; set; } 

    public String Icon { get; set; } 

目前为止,所有的工作的权利,但现在我需要实现ICommand接口。

在StackLayoutButton C#代码我尝试添加:

 var tapGestureRecognizer = new TapGestureRecognizer(); 
     tapGestureRecognizer.SetBinding(TapGestureRecognizer.CommandProperty, "TapCommand"); 
     Icon.GestureRecognizers.Add(tapGestureRecognizer) 
     Text.GestureRecognizers.Add(tapGestureRecognizer) 

而且我尝试添加到CalendarioViewModel INotifyPropertyChanged的和 'OnTapped' 的方法。

进入Activity.cs我添加'ICommand tapCommand'和相关的get ...但不工作。

我尝试了其他..但我无法启用StackLayoutButton组件的水龙头。

我该怎么办? 我希望能够有一个'可编程'命令...例如,我想浏览到'活动的链接属性'或能够打开一个新的视图。

感谢您的帮助!

更新:

我能够TapGestureRecognizer添加到XAML用户控件(StackLayoutButton.xaml.cs), 但我想实现它在MVVM方式,

using Xamarin.Forms; 
namespace Installa.Controls 
{ 
    public partial class StackLayoutButton : StackLayout 
    { 
     public StackLayoutButton() 
     { 
      InitializeComponent(); 


      TapGestureRecognizer tapGestureRecognizer = new TapGestureRecognizer 
      { 
       Command = new Command(OnMyComponentTapped), 
       CommandParameter = "ciao" 
      }; 
      this.Icon.GestureRecognizers.Add(tapGestureRecognizer); 
      this.Text.GestureRecognizers.Add(tapGestureRecognizer); 
     } 


     async void OnMyComponentTapped(object parameter) 
     { 
      // do action 
     } 


     public Color TextColor 
     { 
      get { return this.Text.TextColor; } 
      set { this.Text.TextColor = value; } 
     } 
     public Label TextControl 
     { 
      get { return this.Text; } 
      set { this.Text = value; } 
     } 
    } 
} 

能有人建议我的方式?

感谢

回答

0

这是我在我的XAML视图中添加手势识别:

<Label.GestureRecognizers> 
    <TapGestureRecognizer Command="{Binding SelectEquipmentCommand}"/> 
</Label.GestureRecognizers> 

而且这是在我的视图模型的ICommand的属性:

public ICommand SelectEquipmentCommand 
    { 
     get 
     { 
      return fSelectEquipmentCommand ?? (fSelectEquipmentCommand = new Command(async() => await SelectEquipmentTask())); 
     } 
    } 

    private async Task SelectEquipmentTask() 
    { 

    }