为什么在SetListView(ListView listView)
中调用Form1
ListViewItems后没有在ListView中显示?
namespace WindowsFormsApplication10
{
public partial class Form1 : Form, IView
{
Presenter presenter;
public Form1()
{
InitializeComponent();
presenter = new Presenter(this, new Model());
}
public void SwitchToControl(UserControl userControl)
{
this.tableLayoutPanel1.Controls.Clear();
this.tableLayoutPanel1.Controls.Add(userControl);
}
public void SetListView(ListView listView)
{
this.listView1 = listView;
}
private void button1_Click(object sender, EventArgs e)
{
presenter.SwitchToFirst();
}
private void button2_Click(object sender, EventArgs e)
{
presenter.SwitchToSecond();
}
private void button3_Click(object sender, EventArgs e)
{
presenter.SetListView();
}
}
interface IView
{
void SwitchToControl(UserControl userControl);
void SetListView(ListView listView);
}
class Presenter
{
private readonly IView view;
private readonly IModel model;
public Presenter(IView view, IModel model)
{
this.view = view;
this.model = model;
}
public void SwitchToFirst()
{
var control = new UserControl1();
view.SwitchToControl(control);
}
public void SwitchToSecond()
{
var control = new UserControl2();
view.SwitchToControl(control);
}
public void SetListView()
{
ListView listView = new ListView();
listView.Items.Add(new ListViewItem { Text = "First" });
listView.Items.Add(new ListViewItem { Text = "Second" });
listView.Items.Add(new ListViewItem { Text = "Third" });
listView.Items.Add(new ListViewItem { Text = "Fourth" });
view.SetListView(listView);
}
}
interface IModel
{
}
class Model : IModel
{
}
}
答案 0 :(得分:1)
listView1
只是一个变量,指向您在ListView
中创建并添加到InitializeComponent
控件的ListView
控件。如果您为变量分配了新的ListView
,则它对表单的Controls
集合中的ListView
控件没有任何影响。
将新实例分配给变量后,您在表单上看到的Controls
是listView1
集合中的原始实例,但listView1
变量指向不同的您无法看到的实例,它只在内存中,ListView
上的任何更改都不会显示给您。
如果您想为现有export default React.createClass({
getInitialState: function(){
return {
url: null
}
},
componentDidMount: function(){
this.observeResource();
},
observeResource(){
var self = this
function getValue(callback){
chrome.storage.local.get('newswireStoryUrl', callback);
}
getValue(function (url) {
//here you are seting the state of the react component using the 'self' object
self.setState({url: url.newswireStoryUrl})
//not sure if the 'return this.url' is needed here.
//here the 'this' object is not refering to the react component but
//the window object.
return this.url;
});
},
videoNotFound: function(){
return (
<div className="app">
<AppHeader />
<VideoNotFoundOverlay />
</div
)
},
/* RENDER */
render: function(){
if(this.state.url == null){
return this.videoNotFound()
}
return (
<div className="app">
<AppHeader />
<VideoFoundOverlay />
</div>
)
}
});
提供新项目,只需将新项目传递到表单,然后在清除之前的项目后添加这些新项目。