Prism MVVM实现全屏功能

由于MVVM的设置,ViewModel中无法直接对Window的属性进行访问修改,所以可以在View中创建相关的方法,之后在ViewModel中直接调用方法即可。

View:

MainWindow.xaml

图1

图1的箭头即为控制全屏的按钮,注释1是对按钮内容的绑定,注释2是对按钮命令的绑定。

MainWindow.xaml.cs

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        //切换全屏和窗口模式
        WindowState m_WindowState;
        WindowStyle m_WindowStyle;
        bool m_WindowTopMost;
        ResizeMode m_WindowResizeMode;
        Rect m_WindowRect;
        public void ToFullscreen()
        {
            //存储窗体信息
            m_WindowState = this.WindowState;
            m_WindowStyle = this.WindowStyle;
            m_WindowTopMost = this.Topmost;
            m_WindowResizeMode = this.ResizeMode;
            m_WindowRect.X = this.Left;
            m_WindowRect.Y = this.Top;
            m_WindowRect.Width = this.Width;
            m_WindowRect.Height = this.Height;

            //变成无边窗体
            this.WindowState = WindowState.Normal;//假如已经是Maximized,就不能进入全屏,所以这里先调整状态
            this.WindowStyle = WindowStyle.None;
            this.ResizeMode = ResizeMode.NoResize;
            //this.Topmost = true;//最大化后总是在最上面

            // 调整窗口最大化。
            this.Width = SystemParameters.PrimaryScreenWidth;
            this.Height = SystemParameters.PrimaryScreenHeight;
            this.WindowState = WindowState.Maximized;
        }
        public void ExitFullscreen() 
        {
            //恢复窗口先前信息,这样就退出了全屏
            this.Topmost = m_WindowTopMost;
            this.WindowStyle = m_WindowStyle;

            this.ResizeMode = ResizeMode.CanResize;//设置为可调整窗体大小
            this.Left = m_WindowRect.Left;
            this.Width = m_WindowRect.Width;
            this.Top = m_WindowRect.Top;
            this.Height = m_WindowRect.Height;
            this.WindowState = m_WindowState;//恢复窗口状态信息
            this.ResizeMode = m_WindowResizeMode;//恢复窗口可调整信息
            WindowStartupLocation = WindowStartupLocation.CenterScreen;

        }

    }

Viewmodel:

两个Binding的处理

       private string _BtnCloseFSContent = "全屏";//绑定按钮的Content
       public string BtnCloseFSContent
        {

            get { return _BtnCloseFSContent; }
            set { _BtnCloseFSContent = value;
            RaisePropertyChanged(nameof(BtnCloseFSContent));}
        }

        public ICommand CloseFullScreen => new DelegateCommand(
            ToggleFullscreen//不应该有()
        );

        private void ToggleFullscreen()
        {
            if(_BtnCloseFSContent == "全屏")
            {
                ToFullscreen();
                _BtnCloseFSContent = "退出全屏";
            }
            else
            {
                ExitFullscreen();
                _BtnCloseFSContent = "全屏"; ;
            }
            RaisePropertyChanged(nameof(BtnCloseFSContent));//通知UI层BtnCloseFS属性已经改变
        }

最后是View中两个定义好的方法调用

        private void ToFullscreen()
        {
            //() => ((MainWindow)Application.Current.MainWindow).ToFullscreen();
            ((MainWindow)Application.Current.MainWindow).ToFullscreen();
        }

        private void ExitFullscreen()
        {
            //() => ((MainWindow)Application.Current.MainWindow).ExitFullscreen();
            ((MainWindow)Application.Current.MainWindow).ExitFullscreen();
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值