【问题标题】:Use difference XAML pages in XBAP在 XBAP 中使用不同的 XAML 页面
【发布时间】:2010-11-15 17:35:52
【问题描述】:

我将如何切换在 XBAP 中使用的主显示 XAML 页面。唯一不同的是我想要一个更大的模式和一个更小的模式,但具有相同的控件(一些隐藏)。

【问题讨论】:

    标签: c# xaml xbap


    【解决方案1】:

    在您的 App.xaml.cs 文件中,您可以通过编程方式更改要在启动时显示的 Window.xaml 文件。这是一个过于简单的例子。

    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
    
        System.Windows.Window startupWindow = null;
    
        if(useLargeMode)
        {
             startupWindow = new LargeMainWindow();
        }
        else 
        {
            startupWindow = new SmallMainWindow();
        }
        window.Show();
    }
    

    您也可以通过更改 App.xaml 文件中的 StartupUri 来做到这一点,但这显然在运行时更难更改。

    <Application x:Class="Main.App"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        StartupUri="MainWindow.xaml" /> <!-- change this -->
    

    我还没有尝试绑定到 XAML 中应用程序声明中的属性,但 VS 2010 并没有抱怨这一点。我担心的是该应用程序的数据上下文设置得足够早,以使其正常工作。试一试,让我们知道它是如何工作的。 :)

    <Application x:Class="Main.App"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        StartupUri="{Binding StartupWindow}">
    

    【讨论】:

    • 我有 MainWindow.xaml 和 MainSmallWindow.xaml。使用这种方法,我可以将它们链接到同一个 xmal.cs 文件而无需重复代码吗?
    • 不,但是你可以让他们都把所有的调用都重传给一个实现所有逻辑的共享类,或者你可以从Window类派生一个父类,然后有共同的逻辑在共享父类中实现。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-16
    • 1970-01-01
    • 1970-01-01
    • 2011-07-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多