The fungus gnats are there because the soil is staying too moist and the roots are rotting. The gnats are simply a symptom of a larger problem and that is rotting roots caused by soil that is staying too moist, not by the gnat larvae.
If you allow the Cactus soil to dry out very deep into the pot, which is best for the Cactus, you will deprive the larvae of the moisture they need to survive and you may have a chance to save your Cactus from root rot. No further treatment of the gnats is required once they have been deprived of damp soil.
When you repotted, I hope you used a very porous potting mix and a relatively small pot that will dry out quickly and thoroughly.