--App.xaml
namespace WpfMyproject
{
/// <summary>
/// App.xaml 的交互逻辑
/// </summary>
public partial class App : PrismApplication
{
protected override Window CreateShell()
{
return Container.Resolve<MainView>();
}
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
第二种方式
//containerRegistry.RegisterForNavigation<AutoView>();
//containerRegistry.RegisterForNavigation<ManuView>();
//containerRegistry.RegisterForNavigation<SetView>();
//containerRegistry.RegisterForNavigation<ParameterView>();
//containerRegistry.RegisterForNavigation<StatisticView>();
//containerRegistry.RegisterForNavigation<AlarmView>();
//containerRegistry.RegisterForNavigation<UserView>();
}
protected override IModuleCatalog CreateModuleCatalog()
{
return new DirectoryModuleCatalog() {ModulePath = @".\Modules" };
}
}
}
--MainView.xaml
<Window.Resources>
<Style x:Key="MenuButtonStyle" TargetType="Button">
<Setter Property="Margin" Value="5,5"/>
<Setter Property="Width" Value="100"/>
<Setter Property="Height" Value="60"/>
<Setter Property="FontSize" Value="20"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="Background" Value="#666666"/>
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="100"/>
<RowDefinition/>
<RowDefinition Height="60"/>
</Grid.RowDefinitions>
<Border Grid.Row="0" Background="#333366"/>
<Border Grid.Row="2" Background="#cccccc"/>
<Border Grid.Row="3" Background="Green"/>
<Grid Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition/>
<ColumnDefinition Width="200"/>
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0">
<TextBlock Margin="0,40" Text="图片" Width="100" FontSize="30" Foreground="Yellow" />
</StackPanel>
<StackPanel Grid.Column="2">
<TextBlock Margin="0,20" Text="当前登录用户:" FontSize="18" Foreground="Purple"/>
</StackPanel>
<Grid Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition Height="40"/>
<RowDefinition/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0">
<TextBlock Margin="0,5" Text="智能控制系统" Width="100" FontSize="20" Foreground="White"/>
</StackPanel>
<StackPanel Grid.Row="1" Orientation="Horizontal">
<Button Style="{StaticResource MenuButtonStyle}" Content="自动" Command="{Binding OpenCommand}" CommandParameter="AutoView"/>
<Button Style="{StaticResource MenuButtonStyle}" Content="手动" Command="{Binding OpenCommand}" CommandParameter="ManuView"/>
<Button Style="{StaticResource MenuButtonStyle}" Content="设置" Command="{Binding OpenCommand}" CommandParameter="SetView"/>
<Button Style="{StaticResource MenuButtonStyle}" Content="参数" Command="{Binding OpenCommand}" CommandParameter="ParameterView"/>
<Button Style="{StaticResource MenuButtonStyle}" Content="统计" Command="{Binding OpenCommand}" CommandParameter="StatisticView"/>
<Button Style="{StaticResource MenuButtonStyle}" Content="报警" Command="{Binding OpenCommand}" CommandParameter="AlarmView"/>
<Button Style="{StaticResource MenuButtonStyle}" Content="登录" Command="{Binding OpenCommand}" CommandParameter="UserView"/>
</StackPanel>
</Grid>
</Grid>
<!--//第一种方式-->
<!--<ContentControl Grid.Row="1" Content="{Binding Body}"/>-->
<!--//第二种方式-->
<ContentControl Grid.Row="1" prism:RegionManager.RegionName="ContentRegion"/>
</Grid>
--MainViewModel
namespace WpfMyproject.ViewModels
{
public class MainViewModel : BindableBase
{
//第一种方式
//public DelegateCommand<string> OpenCommand { get; private set; }
//public MainViewModel()
//{
// OpenCommand = new DelegateCommand<string>(Open);
//}
//private object body;
//public object Body
//{
// get { return body; }
// set { body = value; RaisePropertyChanged(); }
//}
//private void Open(string obj)
//{
// switch (obj)
// {
// case "AutoView": Body = new AutoView(); break;
// case "ManuView": Body = new ManuView(); break;
// case "SetView": Body = new SetView(); break;
// case "ParameterView": Body = new ParameterView(); break;
// case "StatisticView": Body = new StatisticView(); break;
// case "AlarmView": Body = new AlarmView(); break;
// case "UserView": Body = new UserView(); break;
// }
//}
//第二种方式
private readonly IRegionManager regionManager;
public DelegateCommand<string> OpenCommand { get; private set; }
public MainViewModel(IRegionManager regionManager)
{
OpenCommand = new DelegateCommand<string>(Open);
this.regionManager = regionManager;
}文章来源:https://www.toymoban.com/news/detail-629331.html
private void Open(string obj)
{
regionManager.Regions["ContentRegion"].RequestNavigate(obj);
}
}
}文章来源地址https://www.toymoban.com/news/detail-629331.html
到了这里,关于C# wpf程序的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!