如何使XAML文件打开另一个XAML文件

时间:2018-09-24 20:40:24

标签: c# wpf xaml

我正在尝试创建WPF应用程序,并且我想知道当您单击按钮时如何使一个xaml文件打开另一个xaml文件。

编辑: 我尝试过:

this.Frame.Navigate(typeof(Window2));

我对MainWindow.xaml.cs的代码是:

using System; // unused
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation; // unused

namespace WindowRemake
{

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class Qwerty : Window
{
    public Qwerty()
    {
        InitializeComponent();
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {

        if (WindowsOption1.IsChecked == true)
        {
            Windows1 win = new Windows1();
            win.ShowDialog();
        }
        // add later versions when i can
    }

    private string UsernameValue = string.Empty;

    private void Username_TextChanged(object sender, TextChangedEventArgs e)
    {
        UsernameValue = Username.Text;
    }
}
}

我对MainWindow.xaml的代码是:

<Window x:Name="ControlPanel" x:Class="Windows3Remake.Qwerty"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WindowRemake"
    mc:Ignorable="d"
    Title="Control Panel" Height="450" Width="800" Cursor="Hand">

    <Grid Cursor="Hand">
        <Button x:Name="StartWindows" Content="Boot Windows" HorizontalAlignment="Left" Margin="77,84,0,0" VerticalAlignment="Top" Width="99" FontWeight="Bold" Cursor="Hand"/>
        <Label x:Name="LabelSetting" Content="Boot Options" HorizontalAlignment="Left" Margin="635,73,0,0" VerticalAlignment="Top" Cursor="Help"/>
        <TextBox x:Name="Username" HorizontalAlignment="Left" Height="23" Margin="595,117,0,0" TextWrapping="Wrap" Text="Username" VerticalAlignment="Top" Width="120"/>
        <RadioButton x:Name="WindowsOption1" Content="Windows 1.0" HorizontalAlignment="Left" Margin="630,161,0,0" VerticalAlignment="Top" IsChecked="True" Cursor="Hand"/>
    </Grid>
</Window>

编辑2:我正在使用在Visual Studio 2017中选择“新建项目”时可以使用的默认WPF

编辑3:这是Windows1.xaml的代码:

<Window x:Name="ControlPanel" x:Class="Windows3Remake.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Windows3Remake"
        mc:Ignorable="d"
        Title="Options" Height="450" Width="800" Cursor="Hand">
    <Grid Cursor="Hand">

    </Grid>
</Window>

Windows1.xaml.cs的代码为:

using System;
using System.Windows; // all imports are unused except for this one
using System.Windows.Controls;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Windows3Remake
{
    public partial class Windows1 : Window
    {
    }
}

编辑

我已经解决了。

1 个答案:

答案 0 :(得分:0)

ButtonClick()
{
    Windows1 win = new Windows1();
    win.ShowDialog();
}

这符合您的要求吗?