【发布时间】:2018-03-24 08:04:52
【问题描述】:
基本上我有这个用户控件,它是一个主菜单,带有一个新游戏按钮、一个加载按钮和一个设置按钮。 当我启动它时,我需要打开它,但更重要的是,当点击新游戏按钮时,我需要将主菜单替换为另一个用户控件。 我一直在尝试查找东西几个小时,但还没有弄清楚如何去做,所以我希望有人能告诉我我做错了什么。这是我当前的代码。
我仍然是编程的初学者,并以此作为一种变大的方式,如果我看起来很慢,请见谅。
主窗口 XAML:
<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=">>>" 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="<<<" 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>
主窗口代码
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");
}
}
主菜单用户控件
<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>
我一直在尝试使用的当前 MVVM
class NavigationViewModel : INotifyPropertyChanged
{
public ICommand MainCommand { get; set; }
public ICommand CharCreaCommand { get; set; }
private object selectedViewModel;
public object SelectedViewModel
{
get { return selectedViewModel; }
set { selectedViewModel = value; OnPropertyChanged("SelectedViewModel"); }
}
public NavigationViewModel()
{
MainCommand = new BaseCommand(OpenMain);
CharCreaCommand = new BaseCommand(OpenCharCrea);
}
private void OpenMain(object obj)
{
SelectedViewModel = new MainInterfaceViewModel();
}
private void OpenCharCrea(object obj)
{
SelectedViewModel = new Character_CreationViewModel();
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
}
public class BaseCommand : ICommand
{
private Predicate<object> _canExecute;
private Action<object> _method;
public event EventHandler CanExecuteChanged;
public BaseCommand(Action<object> method)
: this(method, null)
{
}
public BaseCommand(Action<object> method, Predicate<object> canExecute)
{
_method = method;
_canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
if (_canExecute == null)
{
return true;
}
return _canExecute(parameter);
}
public void Execute(object parameter)
{
_method.Invoke(parameter);
}
}
【问题讨论】: