我需要在另一个Usercontrol的主窗口中打开一个Usercontrol

时间:2018-03-24 08:04:52

标签: c# wpf

基本上我有这个Usercontrol,它是一个主菜单,带有一个新的游戏按钮,一个加载按钮和一个设置按钮。 我需要这个在我启动时打开,但更重要的是当点击新游戏按钮时我需要用另一个用户控件替换主菜单。 我一直在尝试和查找几个小时的东西,但还没有弄清楚如何,所以我希望有人可以告诉我我做错了什么。这是我目前的代码。

我仍然非常喜欢编程并使用它作为一种变大的方法,如果我看起来很慢,那就很抱歉。

MainWindow XAML:

#include <thread>
#include <iostream>
#include <string>
#include <unistd.h> // for sleep

int main()
{
    std::thread t{
        [] {
            std::string s;
            while (std::cin >> s)
                if (s == "exit")
                {
                    std::cout << "exit command given on stdin\n";
                    exit(0);
                }
        }
    };
    t.detach();

    while (true)
    {
        sleep(2);
        std::cout << "beat\n";
    }
}

MainWindow代码

<Window.Resources>
    <DataTemplate DataType="{x:Type local:MainInterfaceViewModel}">
        <local:MainInterface/>
    </DataTemplate>
    <DataTemplate DataType="{x:Type local:Character_CreationViewModel}">
        <local:Character_Creation/>
    </DataTemplate>
</Window.Resources>
<Window.Background>
    <ImageBrush ImageSource="pack://application:,,,/Pokemorph Island;component/images/cover1.jpg"/>
</Window.Background>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="535*"/>
        <RowDefinition Height="36*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="90*"/>
        <ColumnDefinition Width="307*"/>
    </Grid.ColumnDefinitions>
    <DockPanel Grid.ColumnSpan="2" Grid.RowSpan="2">
        <ContentControl x:Name="FullScreen" DockPanel.Dock="Right" Content="{Binding SelectedViewModel}"/>
    </DockPanel>
    <DockPanel Grid.Column="0" Grid.RowSpan="2">
        <ContentControl x:Name="User" />
    </DockPanel>
    <Button Content="&gt;&gt;&gt;" HorizontalAlignment="Left" Click="Change_Image_UP" Style="{StaticResource RoundCorner}" Margin="66,0,0,0" VerticalAlignment="Top" Width="51" Height="31" Foreground="White" FontSize="16" FontWeight="Bold" FontFamily="Rockwell Extra Bold" Grid.Row="1"/>
    <Button Content="&lt;&lt;&lt;" HorizontalAlignment="Left" Click="Change_Image_Down" Style="{StaticResource RoundCorner}" Margin="10,0,0,0" VerticalAlignment="Top" Width="51" Height="31" Foreground="White" FontSize="16" FontWeight="Bold" FontFamily="Rockwell Extra Bold" Grid.Row="1"/>
    <Button Content="Image Source" HorizontalAlignment="Left" Click="Image_Source" Style="{StaticResource RoundCorner}" Margin="431,0,0,0" VerticalAlignment="Top" Width="84" Height="31" Foreground="White" Grid.Column="1" Grid.Row="1"/>
    <Button Content="PATREON" HorizontalAlignment="Left" Click="Patreon_Link" Style="{StaticResource RoundCorner}" Margin="520,0,0,0" VerticalAlignment="Top" Width="84" Height="31" Foreground="White" Grid.Column="1" Grid.Row="1"/>
    <Button Content="Button" Margin="0,507,0,0" VerticalAlignment="Top" Width="75" Command="{Binding MainCommand}"/>
    <Button Content="Button" Margin="134,507,584,0" VerticalAlignment="Top" Width="75" Command="{Binding CharCreaCommand}" Grid.ColumnSpan="2"/>


</Grid>

Mainmenu Usercontrol

public partial class MainWindow : Window
{
    int cover = 1;

    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = new NavigationViewModel();
        //FullScreen.Content = new MainInterface();
    }

    private void Image_Source(object sender, RoutedEventArgs e)
    {
        if (cover == 1)
            System.Diagnostics.Process.Start("---------");
        if (cover == 2)
            System.Diagnostics.Process.Start("---------");
    }
    private void Change_Image_UP(object sender, RoutedEventArgs e)
    {
        cover = cover + 1;
        if (cover > 2)
            cover = 1;
        if (cover == 1)
        {
            this.Background = new ImageBrush(new BitmapImage(new Uri(@"pack://application:,,,/Pokemorph Island;component/images/cover1.jpg", UriKind.Absolute)));
        }
        else if (cover == 2)
        {
            this.Background = new ImageBrush(new BitmapImage(new Uri(@"pack://application:,,,/Pokemorph Island;component/images/cover2.png", UriKind.Absolute)));
        }
    }
    private void Change_Image_Down(object sender, RoutedEventArgs e)
    {
        cover = cover - 1;
        if (cover < 1)
            cover = 2;
        if (cover == 1)
        {
            this.Background = new ImageBrush(new BitmapImage(new Uri(@"pack://application:,,,/Pokemorph Island;component/images/cover1.jpg", UriKind.Absolute)));
        }
        else if (cover == 2)
        {
            this.Background = new ImageBrush(new BitmapImage(new Uri(@"pack://application:,,,/Pokemorph Island;component/images/cover2.png", UriKind.Absolute)));
        }
    }
    private void Patreon_Link(object sender, RoutedEventArgs e)
    {
        System.Diagnostics.Process.Start("https://www.patreon.com/user?u=3253293");
    }
}

当前MVVM我一直在尝试使用

<Grid>
    <Button Content="New Game" HorizontalAlignment="Left" Style="{StaticResource RoundCorner}" Margin="10,10,0,0" VerticalAlignment="Top" Width="138" Height="76" Background="#338B0000" Foreground="White" FontFamily="Segoe Print" FontSize="24" FontWeight="Bold" Command="{Binding CharCreaCommand}" CommandParameter="CharCrea"/>
    <Button Content="Load Game" HorizontalAlignment="Left" Style="{StaticResource RoundCorner}" Margin="10,91,0,0" VerticalAlignment="Top" Width="138" Height="76" Background="#338B0000" Foreground="White" FontFamily="Segoe Print" FontSize="24"/>
    <Label Content="Pokémorph Island" HorizontalAlignment="Left" Margin="103,313,0,0" VerticalAlignment="Top" Height="111" Width="616" Foreground="#CCFF00C5" FontWeight="Bold" FontFamily="Rage Italic" FontSize="80"/>
    <Button Content="Settings" HorizontalAlignment="Left" Style="{StaticResource RoundCorner}" Margin="10,172,0,0" VerticalAlignment="Top" Width="138" Height="76" Background="#338B0000" Foreground="White" FontFamily="Segoe Print" FontSize="24"/>
</Grid>

1 个答案:

答案 0 :(得分:0)

WPF的学习曲线非常艰难。部分原因是因为使用了MVVM模式,它需要一定程度的横向思维。 如果你在想“这很难”那么这是正常的。你不是很慢。

你那里有很多代码。尝试并转向绑定一切,远离事件。 我还建议你使用mvvmlightlibs框架。右键单击解决方案资源管理器中的项目,选择manage nuget packages并添加包。对大多数icommands使用relaycommand。还有一个基本的viewmodel实现了inotifypropertychange,你可以从中继承viewmodels。除非你需要序列化它们。

执行此操作的一种方法是托管要在contentcontrol中切换的用户控件。将内容绑定到viewmodel中的属性。然后,您将其设置为两个视图模型之一。我不知道该怎么称呼它们,让我们说开始和游戏。因此,您将拥有一个包含初始按钮的StartView和一个带有游戏期间所需按钮的GameView。然后,您有一个startVM和GameVM视图模型,其中包含与您的按钮对应的命令。

我认为您在代码中几乎有这个想法,但示例模板可能如下所示:

<DataTemplate DataType="{x:Type local:StartVM}">
    <local:Start/>
</DataTemplate>

那些有范围。所以你可以将它放在一个窗口的资源中,它是整个窗口或网格,它只是那个网格。 您还可以将它们放在资源字典中,在app.xaml中合并该资源字典,它会影响应用程序中的任何位置。

然后你需要你的contentcontrol将它的内容绑定到一个属性,这将给它gamevm或starvm。你看起来就像那样。排序。

<ContentControl Content="{Binding CurrentViewModel}"

我试图解释所有这些东西,但我认为这会带给我们你的问题。 您需要一个命令来设置SelectedViewModel属性,而您拥有的只是一个方法。 以下是一些示例relaycommands: https://msdn.microsoft.com/en-gb/magazine/dn237302.aspx

您的布局看起来有点奇怪。 我不会使用dockpanel,我建议只使用一个网格。 通常情况下,我希望工具栏类型的交易是一个顶级的横幅,下面是一些内容。