0
我會給一個完整的例子,編譯:MVP:演示者如何訪問視圖屬性?
using System.Windows.Forms;
interface IView {
string Param { set; }
bool Checked { set; }
}
class View : UserControl, IView {
CheckBox checkBox1;
Presenter presenter;
public string Param {
// SKIP THAT: I know I should raise an event here.
set { presenter.Param = value; }
}
public bool Checked {
set { checkBox1.Checked = value; }
}
public View() {
presenter = new Presenter(this);
checkBox1 = new CheckBox();
Controls.Add(checkBox1);
}
}
class Presenter {
IView view;
public string Param {
set { view.Checked = value.Length > 5; }
}
public Presenter(IView view) {
this.view = view;
}
}
class MainClass {
static void Main() {
var f = new Form();
var v = new View();
v.Param = "long text";
// PROBLEM: I do not want Checked to be accessible.
v.Checked = false;
f.Controls.Add(v);
Application.Run(f);
}
}
這是一個非常簡單的應用程序。它有一個MVP用戶控件。該用戶控件具有控制其外觀的公共屬性Param
。
我的問題是,我想隱藏用戶的Checked
屬性。它只能由演示者訪問。那可能嗎?我做的事情完全不正確嗎?請指教!
+1表示你不需要打擾。代碼審查應該會發現這種濫用:)但是,如果用戶通過'IView'而不是通過'View'來引用它,這很容易獲得:'IView view = new View( );'... –