question

AlexJohanson-0792 avatar image
0 Votes"
AlexJohanson-0792 asked KyleWang-MSFT edited

How to wrap text around a frame in Xamarin?

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?

dotnet-xamarin
jty3q.jpg (84.4 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

KyleWang-MSFT avatar image
1 Vote"
KyleWang-MSFT answered KyleWang-MSFT edited

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.