2011-06-15 72 views
0

如果我尝试“var mainpage new Mainpage()” 我将运行主页的构造函数,然后XAML对象中的所有字段将返回为null。如何访问Silverlight中的XAML对象,这些对象来自不同的类,但属于同一名称空间的一部分?如何从一个类访问XAML对象以外的主?

让我举例说明。如果你看一下第一个答案,这里是我我遇到

public class MyPage 
{ 
    MyPage() 
    { 

     // the constructor makes all the variables from the xaml null 
    } 

    public TextBox MyTextBox 
    { 
     get { return SomeTextBox; } 
    } 
} 


public class SomeOtherClass 
{ 
    private void SomeFunction() 
    { 
     var page = new MyPage(); // this makes the text empty 
     var sometext = page.MyTextBox.Text; // so sometext will be empty 
    } 
} 

所以,当我运行SomeFunction无论当程序第一次运行该用户imputs变为零。

我第一次尝试的是看看是否创建了SomeClass时,将值放入该类中。

如果失败了,我将尝试MVVM。我已经看到了http://www.vimeo.com/8915487视频和我得到的样品MVVM代码

这里是型号:

namespace SimpleMVVM.Model 
{ 
    public class SimpleModel 
    { 
     // super easy version 
     //public string SomeSimpleValue { get; set; } 

     private string _SomeSimpleValue = string.Empty; 

     // actually do something version... 
     public string SomeSimpleValue 
     { 
      get 
      { 
       return "some value"; 
      } 
      set 
      { 
       _SomeSimpleValue = value; 
      } 
     } 
    } 
} 

这里的观点:

和这里是viewmodel.cs

using Simple; 
using SimpleMVVM.Model; 

namespace SimpleMVVM.ViewModel 
{ 
    public class SimpleViewModel : SimpleViewModelBase 
    { 
     private SimpleModel MyModel = new SimpleModel(); 

     public string SomeSimpleValue 
     { 
      get { return MyModel.SomeSimpleValue; } 
      set 
      { 
       if (MyModel.SomeSimpleValue != value) 
       { 
        MyModel.SomeSimpleValue = value; 
        RaisePropertyChanged("SomeSimpleValue"); 
       } 
      } 
     } 
    } 
} 

使用这个例子,我想知道它是否会像注入ViewModel一样简单,然后更改模型和视图中的绑定。

是真的MVVM这容易吗?

还有一个。它是视图模型基类

using System.ComponentModel; 

namespace Simple 
{ 
    public class SimpleViewModelBase : INotifyPropertyChanged 
    { 
     public event PropertyChangedEventHandler PropertyChanged; 

     public void RaisePropertyChanged(string PropertyName) 
     { 
      var e = new PropertyChangedEventArgs(PropertyName); 
      PropertyChangedEventHandler changed = PropertyChanged; 
      if (changed != null) changed(this, e); 
     } 
    } 
} 

好了,现在最困难的部分。如果我创建一个新班级。我如何从viewmodel类获取数据?

+0

是的,MVVM就是这么简单(当你需要处理像按钮点击那样的UI操作时,它变得更加复杂)。就我个人而言,我不同意你上面显示的MVVM模式,但它现在会做。去找它,如果你还有问题,然后发布一个新的问题。 – slugster 2011-06-15 08:07:45

回答

3

首先,让我把这个咆哮带出去:你提出的是非常糟糕的设计。它符合smelly code的定义。

如果你坚持这样做,最好的方法是在页面上声明一些返回实际UI元素的公共变量。

<UserControl x:Class="MyNamespace.MyPage" ...> 
    <Grid> 
     <TextBox x:Name="SomeTextBox" Width="100" /> 
    </Grid> 
</UserControl> 


public class MyPage 
{ 
    public TextBox MyTextBox 
    { 
     get { return SomeTextBox; } 
    } 
} 


public class SomeOtherClass 
{ 
    private void SomeFunction() 
    { 
     var page = new MyPage(); 
     page.MyTextBox.Text = "some text"; 
    } 
} 

当然的首选方法是使用类似MVVM模式实现从窗口到它的视图模型绑定,那么你可以从视图模型读取属性值,这样你可以避免试图触摸来自完全不同类的任何UI元素。

另一种方法(无需完整的MVVM路径)就是将必要的值注入到正在实例化的控件/页面的构造函数中,并从那里将它们分配给相应的UI元素属性。这仍然很臭,但比直接从外部访问UI元素更好。

+0

您给出的代码示例的问题是我遇到的确切问题。 Immagine有一个MyPage的构造函数。当你调用“var page = new MyPage();”你调用MyPage类的构造函数。该构造函数使SomeTextBox中的所有值都为null。如果你想在MyTextBox中设置文本,这很好,但我想做相反的事情。我想读取已经存在的文本。 – xarzu 2011-06-15 04:17:21

+0

我要编辑我的原始帖子。我想我会用MVVM去做这件事,因为我没有看到任何其他方式可以工作 – xarzu 2011-06-15 04:57:27

+0

@xarzu - 当你说*我想读取已经存在的文本*你错了 - 那里没有文本因为这个页面还没有被创建,所以**不能是任何文本,并且对于构造者来说放置文本仅仅是为了使其无效是无意义的。这听起来像是页面正在检索文本(或放置默认文本)** **构造函数已运行后,probbaly在OnLoad或OnShow事件处理程序。 – slugster 2011-06-15 08:01:06

相关问题