2016-09-17 117 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中的该属性。偏偏这是行不通的:

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

我怎样才能ActivityIndi​​cator的值绑定到IsBusy页面属性? 我已经知道,在设置这些值是这样的:

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

但我不想这样做,我想设置一个值,而不是三个。

+0

1 - 您应该考虑分离视图和ViewModel为清晰2-你应该添加TextCell在你的xaml 3 - 你应该使用e.Text –

回答

0

这里是我会怎么把它改写:

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

你当然可以去一个单独的视图模式,这是不是一个坏主意的路线。然而,对于具体的问题,它看起来并不像你在任何地方设置BindingContext,所以它不会得到你想要的IsBusy属性。

我还没有尝试设置的BindingContext的控制本身,而是这样的事情应该工作:

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

你需要给你的页面的名称(x:Name="myPage"),然后结合使用declare使用{Binding Source={x:Reference myPage}, Path=IsBusy}作为ActivityIndi​​cator的IsVisibleIsRunning值的参考。有关this answer的更多信息。