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
How can I detect which platform the application is running on (Windows or Web)?
Answers
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.
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
@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
}