2016-08-11 6 views
2

먼저 내 영어로는 유감입니다. 내가 Xamarin 재사용 가능한 xaml 사용자 정의 컨트롤 및 사용자 지정 명령

Xamarin.Form

를 사용하여 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> 

<?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의 XAML 페이지이다 전자 CalendarioPage C#을 페이지 : 간단한 클래스와 활동으로

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 partial class CalendarioPage : ContentPage 
{ 
    private CalendarioViewModel vm; 

    public CalendarioPage() 
    { 
     InitializeComponent(); 

     vm = new CalendarioViewModel(); 

     this.BindingContext = vm; 
    } 
} 

지금까지

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'방식으로 추가하려고합니다.

Into Activity.cs 'ICommand tapCommand'와 관련 get ...을 추가했지만 작동하지 않습니다.

다른 시도도했지만 StackLayoutButton 구성 요소에서 탭을 활성화 할 수 없습니다.

나는해야합니까? '프로그래밍 가능'명령을 사용하고 싶습니다. 예를 들어 Activity의 '링크 속성'을 찾아 보거나 새보기를 열 수 있습니다.

도움 주셔서 감사합니다.

업데이트 :

내가 (StackLayoutButton.xaml.cs), XAML 사용자 컨트롤에 TapGestureRecognizer를 추가 할 수 있었다하지만 난 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() 
    { 

    }