C# 班级间的交流

C# 班级间的交流,c#,winrt-xaml,C#,Winrt Xaml,我已经对mandelbrot女士进行了大量修改,现在距离发布基于它的应用程序已经有一半的路程了 我现在正在尝试将这些页面链接在一起,但是我注意到主类没有访问任何辅助类的权限 这些类似乎与页面紧密耦合,而主类似乎能够动态地创建和加载这些页面,而无需获得类的句柄(我可以将事件绑定到委托) 从mainpage.xaml.cs: public void LoadScenario(Type scenarioClass) { AutoSizeInputSectionWhenSnapped = tru

我已经对mandelbrot女士进行了大量修改,现在距离发布基于它的应用程序已经有一半的路程了

我现在正在尝试将这些页面链接在一起,但是我注意到主类没有访问任何辅助类的权限

这些类似乎与页面紧密耦合,而主类似乎能够动态地创建和加载这些页面,而无需获得类的句柄(我可以将事件绑定到委托)

从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);
    }
}
// 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) }
};
这些类/页面紧密地绑定到一个UI控件(listbox),但我去掉了它,因为listbox不属于我的用户流

同样在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);
    }
}
// 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) }
};
//更改下面的数组以反映场景的名称。
//这将用于填充主页上的场景列表
//用户将选择他们感兴趣的特定场景。
//它们的格式应为:“导航到网页”。
//主页中的代码将负责将其转换为:“1)导航到网页”
列表场景=新列表
{
新场景(){Title=“Main Title”,ClassType=typeof(TitleScreen)},
新场景(){Title=“来自文件流的图像”,ClassType=typeof(Scenario2)},
新场景(){Title=“显示NineGrid图像”,ClassType=typeof(Scenario3)},
新场景(){Title=“使用可写的位图”,ClassType=typeof(Scenario4)}
};
我是否应该修改scenario类以保存所有这些场景的实例化


或者我应该设置某种IPC来传回信息?我不确定Windows应用程序是否可以执行IPCs。

显然,MainPage类本身有一个静态引用,因此我可以调用它来从辅助页更改辅助页:

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

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