XAF之不在controller上下文中创建View

本文介绍了一种不使用控制器和操作即可手动打开详细视图的方法。通过创建 ShowViewParameters 对象并配置显示设置,然后调用 XAF 的 ShowView 方法来显示视图。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自: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);


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值