TAIPEI, Taiwan — Just a few weeks ago, President Tsai Ing-wen of Taiwan was struggling politically. Her party had lost in key local elections, imperiling her run for a second term next year.

But then she got help from an unlikely source: the president of China.

In a speech this month to the people of Taiwan, a self-ruled democracy that Beijing considers Chinese territory, President Xi Jinping said the island “must be and will be” united with China and warned that independence efforts could be met by armed force.

Mr. Xi’s speech raised anxieties in Taiwan that Ms. Tsai was able to tap into by delivering a rebuke of Mr. Xi’s proposal, in a rare departure from her usual cautious ambiguity.

“Democratic values are the values and way of life that Taiwanese cherish,” she said, “and we call upon China to bravely move toward democracy.”