由於沒有人回答,我想我可以自問問題後發表我自己的發現。我還沒有想出如何測量修改視覺外觀直到它在屏幕上繪製所花費的時間。
要計算應用程序內部的CPU利用率,您可以使用Analytics class。要計算幀率,只需將其掛接到CompositionTarget類的靜態rendering event即可。
private DispatcherTimer fpsTimer = new DispatcherTimer();
private DateTime lastFpsUpdate;
private Analytics analyzer = new System.Windows.Analytics();
private int frameCount;
public MyClass()
{
fpsTimer.Interval = TimeSpan.FromSeconds(1);
fpsTimer.Tick += new EventHandler(fpsTimer_Tick);
fpsTimer.Start();
lastFpsUpdate = DateTime.Now;
CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering);
}
// Called every second
void fpsTimer_Tick(object sender, EventArgs e)
{
double framerate = 0;
framerate = frameCount/(DateTime.Now - lastFpsUpdate).TotalSeconds;
c_statusMessage.Text = String.Format("Framerate: {0:0} fps, CPU utilization: {1:0.0}%", framerate, analyzer.AverageProcessLoad);
lastFpsUpdate = DateTime.Now;
frameCount = 0;
}
// Called by the framework on every frame
void CompositionTarget_Rendering(object sender, EventArgs e)
{
frameCount++;
}