2011-06-15 74 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

相關問題