Xamarin share is firing OnAppearing event

152 views Asked by At

I have a collection view with a Share option in the items, it is working as expected. However, it is refreshing the page by calling OnAppearing event again. This is causing the collection view with so many items to refresh and lose the state.

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

There are 1 answers

0
Junior Jiang On

This is causing the collection view with so many items to refresh and lose the state.

You could bind ViewModel for ItemsSource of CollectionView, and the ViewModel and Model inherit from INotifyPropertyChanged. Then when property of ViewModel changed, it will interacte with UI.

For example:

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));
    }
}

Now when property changed, it will update the data of ViewModel. You will not need to call OnAppearing method.

Refer to Interactive MVVM.