2013-02-21 117 views
0

我已經大量修改了MS mandelbrot example,並且基於它發佈應用程序的一半。類之間的通信

我想將頁面鏈接到一起,但我注意到主類沒有訪問任何輔助類。

這些類看起來與頁面緊密耦合,主類看起來能夠創建並加載這些頁面,而不需要獲取類的句柄(我可以將一個事件綁定到一個委託)。

從MainPage.xaml.cs中:

public void LoadScenario(Type scenarioClass) 
{ 
    AutoSizeInputSectionWhenSnapped = true; 

    // Load the ScenarioX.xaml file into the Frame. 
    HiddenFrame.Navigate(scenarioClass, this); 

    if (scenarioClass == typeof(TitleScreen)) 
    { 
     // connect event handler here? how? 
     //  EventHandler e = scenarioClass.PageEventHandler(HandleChangePageCall); 
     // scenarioClass is a Type, so any events are not in scope 
    } 

    // Get the top element, the Page, so we can look up the elements 
    // that represent the input and output sections of the ScenarioX file. 
    Page hiddenPage = HiddenFrame.Content as Page; 

    // Get each element. 
    UIElement input = hiddenPage.FindName("Input") as UIElement; 
    UIElement output = hiddenPage.FindName("Output") as UIElement; 
    UIElement entireScreen = hiddenPage.FindName("LayoutRoot") as UIElement; 

    if (entireScreen == null) 
    { 
     // Malformed input section. 
     NotifyUser(String.Format(
      "Cannot load this screen: {0}. Make sure root of input section markup has x:Name of 'entireScreen'", 
      scenarioClass.Name), NotifyType.ErrorMessage); 
     return; 
    } 

    if (output == null) 
    { 
     // Malformed input section. 
     NotifyUser(String.Format(
      "Cannot load this screen: {0}. Make sure root of input section markup has x:Name of 'Output'", 
      scenarioClass.Name), NotifyType.ErrorMessage); 
     return; 
    } 

    // Find the LayoutRoot which parents the input and output sections in the main page. 
    Panel panel = hiddenPage.FindName("LayoutRoot") as Panel; 

    if (panel != null) 
    { 
     // Get rid of the content that is currently in the intput and output sections. 
     panel.Children.Remove(input); 
     panel.Children.Remove(output); 
     //panel.Children.Remove(entireScreen); 

     //// Populate the input and output sections with the newly loaded content. 
     //InputSection.Content = input; 
     OutputSection.Content = output; 
     //OutputSection.Content = entireScreen; // doesn't fall within the expected range 
    } 
    else 
    { 
     // Malformed Scenario file. 
     NotifyUser(String.Format(
      "Cannot load scenario: '{0}'. Make sure root tag in the '{0}' file has an x:Name of 'LayoutRoot'", 
      scenarioClass.Name), NotifyType.ErrorMessage); 
    } 
} 

這些類/頁面被緊緊地綁定到UI控件(列表框),但我還是把說出來,因爲列表框並沒有在我的userflow屬於。

而且在MainPage.xaml.cs:

// Change the array below to reflect the name of your scenarios. 
// This will be used to populate the list of scenarios on the main page with 
// which the user will choose the specific scenario that they are interested in. 
// These should be in the form: "Navigating to a web page". 
// The code in MainPage will take care of turning this into: "1) Navigating to a web page" 
List<Scenario> scenarios = new List<Scenario> 
{ 
    new Scenario() { Title = "Main Title", ClassType = typeof(TitleScreen) }, 
    new Scenario() { Title = "Images from a file stream", ClassType = typeof(Scenario2) }, 
    new Scenario() { Title = "Displaying a NineGrid image", ClassType = typeof(Scenario3) }, 
    new Scenario() { Title = "Using a WriteableBitmap", ClassType = typeof(Scenario4) } 
}; 

我應該修改方案類來保存所有這些情景實例?

或者我應該設置某種IPC來傳回信息嗎?我不確定Windows應用程序是否可以執行IPC。

回答

0

顯然MainPage類有一個靜態引用本身,這樣我就可以調用從二級頁面改變二級頁面:

// in the secondary page: 
MainPage rootPage = MainPage.Current; 
rootPage.SetScreen(2); 

// definition in the MainPage class: 
public static MainPage Current;