Is there a way to prevent from getting eye cancer?

No. You cannot prevent it, but you can lower your risk by using uv protection from a young age.
No. Sunlight is a risk factor for some cancers of the outer eye, famously basal cell carcinomas of the lids, and for eye melanomas to a lesser extent. But there's no preventing them, and nothing to be gained by regretting enjoyable time in the sun. Life's a series of calculated risks. Good luck.