Ticket Q248793
Visible to All Users

How can I detect which platform the application is running on (Windows or Web)?

created 15 years ago

Hello,
I have been working on creating a menu system for the reports in a XAF Windows application. I would like to be able to provide the same menu in the web application as well. However, the code to display a report in the web application appears to be a bit different then that of the Windows application.
Is there any way to determine programmatically within a view controller if it is running as a MS Windows Forms application or a Web application?
Thank you,
Brian Wheatley

Answers

created 15 years ago (modified 7 years ago)

Hi Brian,
XAF doesn't provide an ultimate way to determine the platform on which the application is currently executing. We recommend that you don't include platform-specific code into platform-independent modules. Instead, separate platform-independent logic from platform-specific code and implement parts of your functionality in corresponding modules. Declare abstract or virtual methods in the base controller where the behavior should be different depending on the platform and override these methods in descendant controllers. For example:

implement common code in a platform-agnostic module

C#
namespace MainDemo.Module.Controllers { public class MyController : ViewController { protected SimpleAction action; public MyController() { this.action = new SimpleAction(this, "MyAction", PredefinedCategory.View); this.action.Execute += Action_Execute; } private void Action_Execute(object sender, SimpleActionExecuteEventArgs e) { //... PlatformDependentMethod(e); } protected virtual void PlatformDependentMethod(SimpleActionExecuteEventArgs e) { throw new NotImplementedException(); } } }

in a Win module:

C#
namespace MainDemo.Module.Win.Controllers { public class MyControllerWin: MyController { protected override void PlatformDependentMethod(SimpleActionExecuteEventArgs e) { //... WinForms specific code } } }

in a Web module:

C#
namespace MainDemo.Module.Web.Controllers { public class MyControllerWeb : MyController { protected override void PlatformDependentMethod(SimpleActionExecuteEventArgs e) { //... ASP.NET specific code } } }

In situations where this separation is difficult to implement, you can use implicit approaches to determine the platform. For example, in a view controller, after the Frame is assigned to it, you can test the type of the Frame.Template property. In a WinForms application, it will be the System.Windows.Forms.Control descendant. Also, you can check the value of the DevExpress.Persistent.Base.ValueManager.ValueManagerType static property. It will be equal to SimpleValueManager<T> in WinForms applications and to ASPSessionValueManager<T> in ASP.NET applications. In addition, you can check the actual XafApplication object type: it will be WinApplication or WebApplication.

Thanks,
Michael.

    Show previous comments (2)

      How can we check inXafApplication type in view controller on action button ?

      I tried above code on controller level but not worked.  We would like to write separate code for windows and Web in controller.

      Please suggest how we can do that?

      Thank
      Deepak

      DevExpress Support Team 7 years ago

        @Deepak: I recommend that you create platform-specific descendants of your controller, and use abstract or virtual methods to implement platform-specific processing in descendant classes. I have updated my answer with an example. If this doesn't help, submit a new ticket and attach a sample project demonstrating your attempts.

          We are able to slove this using below logic:

          if (HttpContext.Current != null) // check this for web
                          {

          }
                          else { /// run win code

          }

          Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

          Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.