Share via

How to wrap text around a frame in Xamarin?

Alex Johanson 81 Reputation points
2021-06-27T04:01:41.773+00:00

Hi,
I have an Entry and a button controls.
I want to return Entry text into a Label control by pressing the button and have text over a frame and the frame size to change dynamically based on the text size and character count.
for example:
109568-jty3q.jpg

How is it possible?

Developer technologies | .NET | Xamarin
0 comments No comments

Answer accepted by question author

  1. Kyle Wang 5,531 Reputation points Microsoft External Staff
    2021-06-28T02:47:20.66+00:00

    Hi AlexJohanson-0792,

    Welcome to our Microsoft Q&A platform!

    By default, the label text in the frame will wrap automatically.

    So you just need to define xaml as follows.

    <ContentPage.BindingContext>  
        <local:MainPageViewModel/>  
    </ContentPage.BindingContext>  
    
    <StackLayout>  
        <Entry Text="{Binding EntryText}"/>  
        <Frame CornerRadius="25" BackgroundColor="Azure">  
            <StackLayout>  
                <Label Text="here is the title" FontSize="Small"/>  
                <Label Text="{Binding LabelText}" FontSize="Medium"/>  
            </StackLayout>  
        </Frame>  
        <Button Text="Entry to Label" Command="{Binding SaveCommand}"/>  
    </StackLayout>  
    

    Then define the corresponding properties "EntryText" and "LabelText" in the ViewModel, implement interface INotifyPropertyChanged and define the command "SaveCommand" which will pass entry's text to label.

    class MainPageViewModel : INotifyPropertyChanged  
    {  
        string entryText;  
        public string EntryText  
        {  
            get => entryText;  
            set  
            {  
                entryText = value;  
                OnPropertyChanged("EntryText");  
            }  
        }  
      
        string labelText;  
        public string LabelText  
        {  
            get => labelText;  
            set  
            {  
                labelText = value;  
                OnPropertyChanged("LabelText");  
            }  
        }  
      
        public event PropertyChangedEventHandler PropertyChanged;  
      
        protected void OnPropertyChanged(string propertyName)  
        {  
            var handler = PropertyChanged;  
            if (handler != null)  
                handler(this, new PropertyChangedEventArgs(propertyName));  
        }  
      
        public ICommand SaveCommand { get; set; }  
      
        public MainPageViewModel()  
        {  
            SaveCommand = new Command(Save);  
        }  
      
        void Save()  
        {  
            LabelText = entryText;  
        }  
    }  
    

    Regards,
    Kyle


    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.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

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