【问题标题】:Xamarin share is firing OnAppearing eventXamarin 共享正在触发 OnAppearing 事件
【发布时间】:2020-10-07 12:03:22
【问题描述】:

我在项目中有一个带有共享选项的集合视图,它按预期工作。但是,它通过再次调用 OnAppearing 事件来刷新页面。这会导致包含如此多项目的集合视图刷新并丢失状态。

await Share.RequestAsync(new ShareTextRequest
{
    Text = message,
    Title = title
});

【问题讨论】:

    标签: xamarin xamarin.forms xamarin.essentials


    【解决方案1】:

    这导致包含这么多项目的集合视图刷新并丢失状态。

    你可以为CollectionViewItemsSource绑定ViewModel,ViewModel和Model继承自INotifyPropertyChanged。然后当 ViewModel 的属性发生变化时,它会与 UI 交互。

    例如:

    public class ShareItem: INotifyPropertyChanged
    {
        private string text,title;
    
        public string Text 
        {
            set
            {
                if (text != value)
                {
                    text = value;
                    OnPropertyChanged("Text");
                }
            }
            get
            {
                return text;
            }
        }
    
        public string Title 
        {
            set
            {
                if (title != value)
                {
                    title = value;
                    OnPropertyChanged("Title");
                }
            }
            get
            {
                return title;
            }
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    

    现在当属性改变时,它会更新 ViewModel 的数据。您将不需要调用OnAppearing 方法。

    请参阅Interactive MVVM

    【讨论】:

      猜你喜欢
      • 2014-09-19
      • 1970-01-01
      • 2021-03-09
      • 2016-12-08
      • 1970-01-01
      • 2016-07-30
      • 1970-01-01
      • 2021-12-01
      • 2022-11-30
      相关资源
      最近更新 更多