转自:http://www.devexpress.com/Support/Center/p/K18080.aspx
Solution
As you may observe, a regular code that shows a view via an action is described here. Usually, this task requires adding a controller and an action. When adding the code to the action's Execute event handler we mainly need to set theShowViewParameters.CreatedView property to the view we have created. Also, using the ShowViewParameters class, we can configure the appearance of the view is being created. The remaining functionality (like showing this view) are performed by XAF automatically, and you don't need to do anything else. The view will be shown as expected. However, we can force showing our view manually, and not using an action with the controller:
private static void OpenDetailView(XafApplication app) {
IObjectSpace os = app.CreateObjectSpace();
//Find an existing object.
//Contact obj = os.FindObject<Contact>(CriteriaOperator.Parse("FirstName=?", "My Contact"));
//Or create a new object.
Contact obj = os.CreateObject<Contact>();
obj.FirstName = "My Contact";
//Save the changes if necessary.
//os.CommitChanges();
ShowViewParameters svp = new ShowViewParameters();
svp.CreatedView = app.CreateDetailView(os, obj);
//Specify various display settings.
svp.TargetWindow = TargetWindow.NewModalWindow;
svp.Context = TemplateContext.PopupWindow;
svp.CreateAllControllers = true;
// Here we show our detail view.
app.ShowViewStrategy.ShowView(svp, new ShowViewSource(null, null));
}
For example, you can call this method from a custom form or user control. To get access to the XafApplication instance in this context, pass it through a constructor of your custom form or user control from the invoked context (e.g. from a ViewController, custom ListEditor, etc.)
We can manually create the ShowViewParameters, configure them, and then use the ShowViewStrategy.ShowView method to show the view. This method requires the ShowViewParameters and the ShowViewSource that determines a frame where the view will be shown (here we pass null, since the default settings will be applied). Since we are not using an action to show our detail view, the second parameter will be null. Although, if you want to show our view in the main application window, we can pass the app.MainWindow as the first parameter of the ShowViewSource constructor. As I have already mentioned above, there we use null, and that means that the default frame object using the XafApplication.CreateFrame will be created.
Note
An alternative approach, allowing you to create a view is use of shortcuts. We need to use the XafApplication.ProcessShortcutmethod for this purpose. This is a sample code:
[C#]
ShowViewParameters svp = new ShowViewParameters();
ViewShortcut shortcut = new ViewShortcut(Application.FindDetailViewId(typeof(Contact)), obj.Oid);
svp.CreatedView = Application.ProcessShortcut(shortcut);