Dbl
Dbl

Reputation: 5914

How can i detect the Forms Previewer mode?

Visual Studio 2017 introduces a new window to help with mobile development - The "Forms Previewer"-window.

Has anyone found out how to detect this mode yet?

I have some telemetry code, which throws exceptions because it is not initialized yet - hence the need to detect this "design" mode.

Upvotes: 1

Views: 583

Answers (2)

Bret Johnson - MSFT
Bret Johnson - MSFT

Reputation: 594

Now Xamarin.Forms supports this directly and you can use its built in Xamarin.Forms.DesignMode class:

if (DesignMode.IsDesignModeEnabled)
{
  // Previewer only code  
}

See https://learn.microsoft.com/en-us/xamarin/xamarin-forms/xaml/xaml-previewer

Upvotes: 4

Michał Żołnieruk
Michał Żołnieruk

Reputation: 2105

Previously it was possible to detect design mode if the app instance is not created, like this:

if (Application.Current==null) 
{  
    // design mode
}

In Xamarin Studio 6.2 the app instance is created even in design mode, so we can't use it like that. What you can do is creating your custom flag IsInDesignMode = true and change it to false from place which is not run in design mode, like OnStart:

protected override void OnStart()
{
    FakeViewModels.IsInDesignMode = false;
}

and then use it wherever you want:

if (FakeViewModels.IsInDesignMode == false) 
{
    this.BindingContext = this;
}

Also remember that App() constructor and therefore first navigation happens before OnStart(), so using above method is not going to cover the first page you'll navigate to.

Upvotes: 1

Related Questions