简体   繁体   English

为什么我不能在视图的演示者背后的视图代码中调用方法?

[英]Why can't I call a method in my View's code-behind from the view's presenter?

This is the code-behind of my view: 这是我观点的代码背后

using System.Windows.Controls;

namespace TestBoundTabControl.Views
{
    public partial class SmartFormView : UserControl
    {
        public SmartFormView()
        {
            InitializeComponent();
        }

        public void BeforeLoad()
        {
            MainTabControl.SelectedIndex = MainTabControl.Items.Count - 1;
        }
    }
}

But why can't I access the method "BeforeLoad()" from the view's presenter? 但是,为什么不能从视图的演示者访问方法“ BeforeLoad()”?

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using TestBoundTabControl.Views;

namespace TestBoundTabControl.Presenters
{
    public class SmartFormPresenter : PresenterBase
    {
        #region ViewModelProperty: SmartFormAreaPresenters
        private ObservableCollection<SmartFormAreaPresenter> _smartFormAreaPresenters = new ObservableCollection<SmartFormAreaPresenter>();
        public ObservableCollection<SmartFormAreaPresenter> SmartFormAreaPresenters
        {
            get
            {
                return _smartFormAreaPresenters;
            }

            set
            {
                _smartFormAreaPresenters = value;
                OnPropertyChanged("SmartFormAreaPresenters");
            }
        }
        #endregion

          public SmartFormPresenter()
        {
            View = new SmartFormView();
            View.DataContext = this;


            for (int i = 0; i < 5; i++)
            {
                SmartFormAreaPresenters.Add(new SmartFormAreaPresenter());
            }

            View.BeforeLoad(); //method not found

        }
    }
}

My guess is that the property View has type UserControl and not SmartFormView . 我的猜测是,属性View类型为UserControl而不是SmartFormView If that is true you will have to cast it (or change it's type): 如果是这样,则必须强制转换(或更改其类型):

((SmartFormView) View).BeforeLoad();

View is obviously of some base type, like FrameworkElement. 视图显然是某种基本类型,例如FrameworkElement。 Try this code: 试试这个代码:

SmartFormView myView = new SmartFormView();

View = myView;

myView.BeforeLoad();

You don't show your PresenterBase class, but the PresenterBase.View property probably isn't of type SmartFormView. 您没有显示PresenterBase类,但是PresenterBase.View属性可能不是SmartFormView类型。 I'm not sure what type it is, but I'm guessing UserControl or one of its ancestors. 我不确定它是什么类型,但是我猜是UserControl或其祖先之一。

Here are some options: 以下是一些选项:

  1. Make a base class for all of your Views, put a virtual BeforeLoad method on that base class, and make your PresenterBase's View property be of that type. 为所有View创建一个基类,在该基类上放置一个虚拟的BeforeLoad方法,并使PresenterBase的View属性为该类型。
  2. Insert a typecast, as Martin suggested (this is more of a hack than a solution, IMHO). 按照Martin的建议插入类型转换(恕我直言,这比解决方案更像是一种技巧)。
  3. Make your base class generic on the view type, so that in SmartFormPresenter, the View property really can be of type SmartFormView. 使您的基类对视图类型通用,以便在SmartFormPresenter中,View属性实际上可以是SmartFormView类型。 Eg: 例如:

     public class PresenterBase<T> { ... public T View { get; set; } ... 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 翻译DataGrid MouseEvent,以便在View的Code-Behind中没有代码 - Translate DataGrid MouseEvent so I do not have code in View's Code-Behind 如何通过Javascript或Jquery调用代码隐藏的方法 - How can I call a method that are in my code-behind via Javascript or Jquery 如何从后台代码调用方法 - how to call method from code-behind 如何在ViewModel中处理Validation.Error而不是我后面的View代码? - How can I handle a Validation.Error in my ViewModel instead of my View's code behind? 如果usercontrol的datacontext是视图模型,您如何绑定到xaml代码后面的属性? - How do you bind to a property in a xaml code-behind if the usercontrol's datacontext is a view-model? 从代码隐藏Web方法中调用Javascript方法 - Call a Javascript Method from code-behind Web Method 我应该将此功能放在View(代码隐藏)或ViewModel中吗? - Should I put this function in View (code-behind) or in ViewModel? WPF从视图模型访问代码隐藏 - WPF accessing code-behind from view models 来自客户端(JavaScript)的代码隐藏(安全页面)中的调用方法 - Call method in code-behind (secure page) from client (JavaScript) 我如何通过AngularJS视图/控制器将文件上传到C#代码背后? - How can I upload a file to C# code-behind via an AngularJS view/controller?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM