日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網(wǎng)站首頁 編程語言 正文

WPF實現(xiàn)頁面的切換的示例代碼_C#教程

作者:鯉籽鯤 ? 更新時間: 2023-03-26 編程語言

前言

本文主要講述如何在同一個窗體內(nèi),實現(xiàn)不同功能模塊的頁面切換。

一、準備工作

1.搭建一個簡單的mvvm項目結(jié)構(gòu)

在這里插入圖片描述

首先搭建一個簡單的項目框架,然后有紅和綠兩個頁面,ViewModels中的Base 中簡單實現(xiàn)了ICommand 和 INotifyPropertyChanged接口

二、實現(xiàn)

1.使用Frame控件的方式實現(xiàn)

利用Frame的Source 屬性加載內(nèi)部的控件,使用Frame的時候,用于切換的頁面可以是UserControl 或者Page,如案例中使用的就是Page

實現(xiàn)代碼如下:

<Window x:Class="WpfApp2.Views.MainView"
        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:WpfApp2.Views"
        xmlns:vm="clr-namespace:WpfApp2.ViewModels"
        mc:Ignorable="d"
        Title="MainView" Height="450" Width="800">
    <Window.DataContext>
        <vm:MainViewModel></vm:MainViewModel>
    </Window.DataContext>
    <DockPanel Grid.Column="0">
        <StackPanel Background="LightBlue">
            <RadioButton Command="{Binding ChangePageCommand}" CommandParameter="PageRedView.xaml" Content="紅色" Margin="10"></RadioButton>
            <RadioButton Command="{Binding ChangePageCommand}" CommandParameter="PageGreenView.xaml" Content="綠色" Margin="10"></RadioButton>
        </StackPanel>
        <Frame NavigationUIVisibility="Hidden" Source="{Binding PageName}"/>
    </DockPanel>
</Window>


注意:這里的CommandParameter傳入的是PageRedView.xaml文件

    public class MainViewModel:ViewModelBase
    {
        private string _pageName;

        public string PageName
        {
            get { return _pageName; }
            set { _pageName = value; OnPropertyChanged(); }
        }

        public ICommand ChangePageCommand { get; set; }

        public MainViewModel()
        {
            ChangePageCommand = new CommandBase(ChangePage);
        }

        private void ChangePage(object obj)
        {
            PageName = obj.ToString();
        }
    }

2.使用反射的方式實現(xiàn)

使用反射+ContentControl 的方式也可使用頁面切換,不過該方式下ContentControl 的Content不可以承接Page,Page只有Frame 和Window可以承接,但是可以承接UserControl。
首先將紅色和綠色兩個界面修改為UserControl并命名為UserControlRed和UserControlGreen ,然后修改代碼如下:

    <DockPanel Grid.Column="0">
        <StackPanel Background="LightBlue">
            <RadioButton Command="{Binding ChangePageCommand}" CommandParameter="UserControlRed" Content="紅色" Margin="10"></RadioButton>
            <RadioButton Command="{Binding ChangePageCommand}" CommandParameter="UserControlGreen" Content="綠色" Margin="10"></RadioButton>
        </StackPanel>
        <ContentControl Content="{Binding MainContent}"/>
    </DockPanel>
  public class MainViewModel:ViewModelBase
    {
        private FrameworkElement mainContent;

        public FrameworkElement MainContent
        {
            get { return mainContent; }
            set { mainContent = value; OnPropertyChanged(); }
        }


        public ICommand ChangePageCommand { get; set; }

        public MainViewModel()
        {
            ChangePageCommand = new CommandBase(ChangePage);
        }

        private void ChangePage(object obj)
        {
        	//【 * 】這里需要拼接路徑
            Type type = Type.GetType("WpfApp2.Views." + obj.ToString());
            MainContent = (FrameworkElement)System.Activator.CreateInstance(type);
        }
    }

3.實現(xiàn)效果

在這里插入圖片描述

總結(jié)

原文鏈接:https://blog.csdn.net/qq_39847278/article/details/128386432

欄目分類
最近更新