2014-12-06 48 views
0

嗨即時通訊尋找控制.isopen值從一個不同的類,我花了幾個小時看着這個,我試過各種各樣,除了一個我不太瞭解的視圖模型關於,但似乎有點過分改變一個值,如果有人能指出我的方向是正確的,那將是非常有意義的。謝謝訪問Popup.isopen在MainPage FromA不同類

回答

1

對於超出最簡單的應用程序的任何東西,你可能應該考慮分離你的數據與演示文稿。您不一定需要小型應用程序的完整MVVM的複雜性,但總體概念幾乎總是好的。

也就是說,另一種選擇是將您的MainPage作爲單例編寫並導出一個靜態函數,該靜態函數提供MainPage實例並可從其他應用程序調用。標準的MSDN示例模板這是否符合公衆柯倫場

public sealed partial class MainPage : Page 
{ 
    public static MainPage Current; 

    public MainPage() 
    { 
     this.InitializeComponent(); 
     SampleTitle.Text = FEATURE_NAME; 

     // This is a static public property that allows downstream pages to get a handle to the MainPage instance 
     // in order to call methods that are in this class. 
     Current = this; 
    } 

    // Rest of MainPage class 
} 

其他類可以通過電流場訪問從MainPage類的公共方法和字段:

MainPage.Current.MyPopup.IsOpen = true; 

對於封裝你可能想包裝在一個函數,而不是直接暴露MyPopup

public void RequestWidgetData() 
{ 
    WidgetPopup.IsOpen = true; 
} 
0

使用viewmodel是正確的決定。在viewmodel中,你將創建一個屬性IsOpen,然後將你的視圖綁定到它。我強烈推薦你使用MVVM模式,同時爲WinRT設計應用程序。簡單的實現將是這樣的:

public class ViewModelBase : INotifyPropertyChanged 
{ 
   public event PropertyChangedEventHandler PropertyChanged; 
    protected virtual void OnPropertyChanged(string propertyName) 
     { 
     OnPropertyChanged(new PropertyChangedEventArgs(propertyName)); 
    } 
    protected virtual void OnPropertyChanged(PropertyChangedEventArgs args) 
     { 
      var handler = PropertyChanged; 
     if (handler != null) 
      handler(this, args); 
    } 
} 

public class MainPageViewModel : ViewModelBase 
{ 
    private bool _isOpen; 
    public bool IsOpen 
    { 
     get { return _isOpen; } 
     set 
     { 
      _isOpen = value; 
      OnProperyChanged("IsOpen"); 
     } 
    } 
} 
+0

感謝您的意見,但正如我所說的我不知道視圖模型的想法是有在形式的任何實例嗨什麼我在尋找感謝。 – black1stallion2 2014-12-06 15:08:52

+0

這是ViewModel的基本實現。將其設置爲頁面的DateContext,然後將視圖的IsOpen屬性綁定到ViewModel的IsOpen屬性。 – 2014-12-06 15:29:45