2016-09-17 9 views
1

をバインドすることはできません:は、私は次のコードでアクティビティインジケータとページを持っているIsBusyプロパティ

<?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="MyApp.ClientSearch" Title="Search"> 
    <ContentPage.Content> 
    <StackLayout Orientation="Vertical"> 
     <StackLayout.Children> 
     <SearchBar x:Name="txtSearchClient" TextChanged="OnTextChanged"></SearchBar> 
     <ActivityIndicator IsVisible="{Binding IsBusy}" IsRunning="{Binding IsBusy}" x:Name="indicadorCargando" /> 
     <ListView x:Name="lstClients"></ListView> 
     </StackLayout.Children> 
    </StackLayout> 
    </ContentPage.Content> 
</ContentPage> 

このXAMLに関連する部分クラスでは、私が持っている:

namespace MyApp 
{ 
    public partial class ClientSearch: ContentPage 
    { 
     public BusquedaClientes() 
     { 
      InitializeComponent(); 
     } 

     async void OnTextChanged(object sender, TextChangedEventArgs e) 
     { 

      if (this.txtSearchClient.Text.Length >= 3) 
      { 
       var list_clients = App.ClientsManager.GetTasksAsync(txtSearchClient.Text); 
       this.IsBusy = true; 

       var template = new DataTemplate(typeof(TextCell)); 

       template.SetBinding(TextCell.DetailProperty, "name_ct"); 
       template.SetBinding(TextCell.TextProperty, "cod_ct"); 

       lstClients.ItemTemplate = template; 
       lstClients.ItemsSource = await list_clients; 

       this.IsBusy = false; 

      } 
     } 
    } 
} 

することができますようにthis.IsBusyがPageプロパティを設定しているため、XAMLのそのプロパティにバインドしようとしました。残念ながらそれは動作しません:

IsBusyページプロパティにActivityIndi​​catorの値をバインドする方法

this.IsBusy = true; 
indicadorCargando.IsVisible=true; 
indicadorCargando.IsRunning=true; 

しかし、私は、私が代わりに3の一つの値を設定する、ことを行うにはしたくない: は、私はすでに、このような値を設定することを知っています。

public class ClientSearch : ContentPage 
    { 
     public ClientSearch() 
     { 
      BindingContext = new ClientSearchViewModel(); 
      var stack = new StackLayout(); 
      var searchBar = new SearchBar(); 
      searchBar.SetBinding<ClientSearchViewModel>(SearchBar.TextProperty, x => x.Text); 
      var actInd = new ActivityIndicator(); 
      actInd.SetBinding<ClientSearchViewModel>(ActivityIndicator.IsVisibleProperty, x => x.IsBusy); 
      actInd.SetBinding<ClientSearchViewModel>(ActivityIndicator.IsRunningProperty, x => x.IsBusy); 
      var lv = new ListView 
      { 
       ItemTemplate = new DataTemplate(() => 
       { 
        var txtCell = new TextCell(); 
        txtCell.SetBinding<MyItemModel>(TextCell.TextProperty, x => x.Name_Ct); 
        txtCell.SetBinding<MyItemModel>(TextCell.TextProperty, x => x.Cod_Ct); 
        return txtCell; 
       }) 
      }; 
      lv.SetBinding<ClientSearchViewModel>(ListView.ItemsSourceProperty, x => x.items); 
      stack.Children.Add(searchBar); 
      stack.Children.Add(actInd); 
      stack.Children.Add(lv); 
      Content = stack; 
     } 
    } 

    public class ClientSearchViewModel : BaseViewModel 
    { 
     public string Text { get; set; } 
     public bool IsBusy { get; set; } 
     public IEnumerable<MyItemModel> items { get; set; } 

     protected override async void OnPropertyChanged(string propertyName = null) 
     { 
      if (propertyName != nameof(Text) || Text.Length < 3) return; 
      IsBusy = true; 
      items = await App.ClientsManager.GetTasksAsync(Text); 
      IsBusy = false; 
     } 
    } 

    [ImplementPropertyChanged] 
    public abstract class BaseViewModel : INotifyPropertyChanged 
    { 
     public event PropertyChangedEventHandler PropertyChanged; 

     protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
    } 
+0

1 - 分かりやすくするために、ViewとViewModelを分けることを検討する必要があります。2 - xamlにTextCellを追加する必要があります。3 - e.Text –

答えて

0

は、私はそれを書き換えるだろうかです。しかし、特定の質問については、どこにでもBindingContextを設定しているようには見えないので、必要なIsBusyプロパティを取得することはできません。

私は自分自身へのコントロールのBindingContextを設定しようとしていないが、このような何かが動作するはず

<ActivityIndicator 
    IsVisible="{Binding IsBusy}" 
    IsRunning="{Binding IsBusy}" 
    x:Name="indicadorCargando" 
    BindingContext="{x:Reference indicadorCargando}" /> 
0

あなたは確かに悪い考えではない別のビューモデルのルートを行くことができる:ここで

0

あなたのページの名前(x:Name="myPage")を得た後、使用してバインディングを宣言する必要がありますアクティビティインジケータのIsVisibleIsRunningの値に{Binding Source={x:Reference myPage}, Path=IsBusy}を使用してその参照を返します。 this answerの詳細

関連する問題