Variable cannot be changed

Yusuf 691 Reputation points
2022-07-02T02:45:11.68+00:00

I cant change text value

namespace MauiApp7  
{  
    public class GraphicsDrawable : IDrawable  
    {  
        public string text;  
        public void Draw(ICanvas canvas, RectF dirtyRect)  
        {  
            canvas.DrawString(text, 60, 60, HorizontalAlignment.Center);  
        }  
    }  
}  

---------------------------------------------------

namespace MauiApp7;  
  
public partial class MainPage : ContentPage  
{  
    public MainPage()  
    {  
        InitializeComponent();  
    }  
  
    private void Button_Clicked(object sender, EventArgs e)  
    {   
        GraphicsDrawable drawable = new GraphicsDrawable();  
        drawable.text = entry1.Text;  
        graphicsView.Invalidate();  
    }  
}  
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,906 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,289 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 68,661 Reputation points Microsoft Vendor
    2022-07-08T11:11:46.987+00:00

    Hello,​

    If want to change the text of GraphicsDrawable depending on entry1.Text.

    you need to set drawable of graphicsView when you create a new GraphicsDrawable object like following code.

       var draw = new GraphicsDrawable();  
       graphicsView.Drawable = draw;  
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    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.


2 additional answers

Sort by: Most helpful
  1. Karen Payne MVP 35,116 Reputation points
    2022-07-02T05:37:14.9+00:00

    Change it from a field to a property and use proper casing

    public string Text { get; set; }  
    
    1 person found this answer helpful.

  2. Ken Tucker 5,846 Reputation points
    2022-07-03T14:02:36.47+00:00

    I am looking at your code in this function.

      private void Button_Clicked(object sender, EventArgs e)  
      {  
          var draw = new GraphicsDrawable  
          {  
              Text = entText.Text  
    
           };  
    
           Debug.WriteLine("MainPage: " + draw.Text);  
    
           GraphicsDrawable drawable = new GraphicsDrawable();  
           graphicsView.Invalidate();  
      }  
    

    The variable draw is only in scope in the function Button_Clicked. Once Button_Clicked function finishes running draw is gone.

    1 person found this answer helpful.