【问题标题】:Xamarin Forms - how to generate an event based on Visible property changesXamarin Forms - 如何根据可见属性更改生成事件
【发布时间】:2019-11-10 19:54:56
【问题描述】:

在我的应用程序中,我有某些 ContentView 在它们变得可见时需要采取行动(即加载它们的数据)。系统中有几个事件可以导致它们变得可见,因此将加载数据操作与这些事件联系起来是有问题的,因为其中许多事件可能一个接一个地发生。如果我有办法在 Visible 属性变为“真”时简单地加载数据,那么我将处于一个更好的位置。

我创建了一个简单的示例来说明我想要完成的工作。即当 Visible 属性更改时,我需要 ContentView 生成事件。它不起作用,但我认为它很接近,我会很感激有人向我展示如何使它起作用。如果我可以让这个示例正常工作,那么我可以将所有需要此功能的 ContentView 包装在一个基本 ContentView 中,以提供我需要的事件。

所有代码都可以在 GitHub here 上找到。如果有帮助,您可以下载代码以查看整个解决方案。

首先我定义了一个 PanelView 控件。请注意,我在后面的代码中将 IsVisible 属性绑定到 PanelVisible 属性...

然后在后面的代码中我有两个 BindableProperty 道具,PanelVisibleProperty 和 ShowingProperty。 PanelVisible 从上面绑定到 IsVisible。在 propertyChanged 事件中,我正在调用 Showing 命令。

在我的 MainPage.xaml 中,我有一个按钮,单击该按钮会调用 ShowPanelView 命令,该命令设置 PanelViewVisible 属性并使 PanelView 显示(最初在加载时隐藏)。并且,我将 PanelView 中的显示命令绑定到模型中的 PanelShowing 命令。

最后,这里是绑定到页面的 MainPageModel...

我在这里没有收到 propertyChanged 事件,如果我能做到这一点,那么我认为它的其余部分会正常工作。

谁能看出问题出在哪里?非常感谢!

【问题讨论】:

    标签: xamarin xamarin.forms


    【解决方案1】:

    我发现了这个问题。在我的 MainPage.xaml 中,我绑定到 PanelView 的 IsVisible 属性。相反,它需要绑定到 PanelVisible 属性。现在一切正常。

    这是 MainPage.xaml 的更新代码。我已经更新了GitHub repo,以防其他人有类似的需求。

    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 xmlns:local="clr-namespace:PanelViewControl"
                 x:Class="PanelViewControl.MainPage">
    
        <StackLayout Margin="100">
            <Button Text="Show Panel View Control" Command="{Binding ShowPanelView}" />
            <local:PanelView PanelVisible="{Binding PanelViewVisible}" 
                             Showing="{Binding PanelShowing}">
            </local:PanelView>
        </StackLayout>
    
    </ContentPage>
    

    【讨论】:

      【解决方案2】:

      问题是当您更改 PanelViewVisible 值但未通知更改时。要解决此问题,您必须在模型类中实现 INotifyPropertyChanged

      代码如下:

        public class MainPageModel : FreshBasePageModel,INotifyPropertyChanged
        {
          public event PropertyChangedEventHandler PropertyChanged;
      
          public Command ShowPanelView
          {
              get
              {
                  return new Command(() =>
                  {
                      PanelViewVisible = true;
                  });
              }
          }
          bool _panelViewVisible;
          public bool PanelViewVisible
          {
              set { SetProperty(ref _panelViewVisible, value); }
              get { return _panelViewVisible; }
          }
      
          public Command PanelShowing
          {
              get
              {
                  return new Command(() =>
                  {
                  });
              }
          }
      
          bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
          {
              if (Object.Equals(storage, value))
                  return false;
      
              storage = value;
              OnPropertyChanged(propertyName);
              return true;
          }
      
          protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
          {
              PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
          }
      }
      

      只需替换为您的模型并检查。

      【讨论】:

      • 您好,感谢您的回复,但 FreshMVVM 框架在幕后实现了所有这些内容。优点是它隐藏了所有样板代码,使视图模型更加清晰。只是为了笑,我尝试了你的代码,但它没有帮助。第一个(也许是唯一一个)问题是 PanelVisibleProperty 的第 28 行(见上文)从未被命中。如果我能解决这个问题,那么我相信它的其余部分将按原样工作。
      猜你喜欢
      • 2018-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多