The main idea of Sonnet 146 is that it is pointless to try to adorn the body or to paint "thy outward walls so costly gay" In other words, it is silly to try to seem outwardly happy at the expense of feeding one's soul. Shakespeare writes:
"Then soul, live thou upon thy servant's loss,
And let that pine to aggravate thy store;
Buy terms divine in selling hours of dross;"
In other words, one should focus on one's soul since worms will eventually "Eat up thy charge" or body. This does echo some of the thoughts of Hamlet when he is speaking to Claudius about worms eating kings, who are then eaten by fish who are then eaten by humans.
"A man may fish with the worm that hath eat of aking, and eat of the fish that hath fed of that worm.."( IV.iii, 30-31)
The main idea of the sonnet, however, seems to be about the importance of feeding one's soul and not adorning one's outward appearance or appearing happy when one is not. Hamlet does not try to appear happy, but instead appears mad when he is unhappy. In addition, Hamlet is more interested in revenge than either feeding his soul or adorning his body. Perhaps if he had heeded the advice in Sonnet 146, Hamlet would have been much happier but the play, unfortunately, would be much duller.