Can the ScrollOrientation of a ScrollView be changed afer the ScrollView is displayed?

Blaise 41 Reputation points
2021-02-12T23:54:29.607+00:00

I have a Xamarin Editor view in a ScrollView. The Editor has AutoSize set to TextChanges. The ScrollView is defined in XAML like so:

        <ScrollView 
            Orientation="{Binding ScrollViewOrientation}" 
            VerticalScrollBarVisibility="Always"
            Padding="0"
            Margin="0"
            >

In the ctor of page's view model, I set the ScrollViewOrientation property like so:

    if (_ut.Prefs.InfoTextWordWrap)
        ScrollViewOrientation = ScrollOrientation.Vertical;
    else
        ScrollViewOrientation = ScrollOrientation.Both;

And the ScrollViewOrientation property is defined in the view model like so:

    public ScrollOrientation ScrollViewOrientation
    {
        get
        {
            return _scrollOrient;
        }
        set 
        {
            if (_scrollOrient != value)
            {
                _scrollOrient = value;
                OnPropertyChanged(nameof(ScrollViewOrientation));
            }
        }
    }

The WordWrap property in the view model is defined like so:

public bool WordWrap
{
    get { return _wordWrap; }
    set
    {
        if (value != _wordWrap)
        {
            _wordWrap = value;
            _ut.Prefs.InfoTextWordWrap = value;
            OnPropertyChanged(nameof(WordWrap));
            ScrollViewOrientation = _wordWrap ? ScrollOrientation.Vertical : ScrollOrientation.Both;
            if (_loaded)
                _ut.Prefs.SaveAppPrefs(Utils.SavePrefDetail.TextWrap);
        }
    }

}

This works as intended: with word wrap on, I get vertical scrolling only and the text in the Editor wraps; with word wrap off, I get both vertical and horizontal scrolling and the text in the Editor does not wrap.

The word wrap property for the Android app is set on an options page. I got the "bright" idea of putting a Word Wrap checkbox on the page with the ScrollView/Editor so the user could toggle word wrap while viewing the text, instead of having to back out of the page, go to the options page, change word wrap, close the options page and then reload the text page.

But having made the changes, tapping the word wrap checkbox has no effect. For example, if I load this page with word wrap turned OFF, I get no wrapping and scrolling in both directions (as desired). Tapping the word wrap checkbox does nothing to the UI (other than display the check). But if I leave the page and then come back to it, wrap is now ON yielding only vertical scrolling and text wrapping.

That is, the word wrap preference was changed (and persisted), but it would seem that changing the ScrollOrientation of the ScrollView after it has been displayed has no effect.

I could certainly be wrong ... any feedback would be appreciated.

Environment:
VS 2019, c# on Win 10
Xamarin Forms v4.8.0.1560
Yes, the Xamarin Forms version is "old" (but "only" 4 months old). I'd like to understand what the issue is before updating

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,348 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 75,431 Reputation points Microsoft Vendor
    2021-02-15T05:18:27.463+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    Can the ScrollOrientation of a ScrollView be changed afer the ScrollView is displayed?

    Based on my test, this answer is No, I make a vertical ScrollView, then use a button to change the ScrollOrientation.Vertical to ScrollOrientation.Horizontal, but it not work, MVVM is the same with set my way, so it cannot be changed afer the ScrollView is displayed.

    Best Regards,

    Leon Lu


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.