【问题标题】:Detect click on resize window UWP检测点击调整窗口UWP
【发布时间】:2018-05-16 13:06:19
【问题描述】:

我想在单击调整大小按钮时启动一个消息对话框..

我在任何点击中都插入了消息对话框,但如何在调整大小窗口中启动它?

代码:

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
    }

    private async void Button_Click(object sender, RoutedEventArgs e)
    {
        var messageDialog = new MessageDialog("This is a Message dialog");
        await messageDialog.ShowAsync();
    }
}

我找到了一个可能的解决方案,但我只需要单击调整大小按钮,这可能吗?

代码:

Window.Current.CoreWindow.SizeChanged += (ss, ee) =>
{
      var appView = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView();
      if (appView.IsFullScreen)
      {
          //show message
      }
      ee.Handled = true;
};

提前致谢!

【问题讨论】:

    标签: c# uwp titlebar


    【解决方案1】:

    您可以为此订阅页面大小更改事件

    XAML

    <Page
    x:Class="App.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:SO15"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" SizeChanged="Page_SizeChanged"> <!-- SizeChanged event -->
    

    C#

    private void Page_SizeChanged(object sender, SizeChangedEventArgs e)
    {
        Window.Current.CoreWindow.SizeChanged += async (ss, ee) =>
        {
            var appView = ApplicationView.GetForCurrentView();
            if (appView.IsFullScreen)
            {
                var messageDialog = new MessageDialog("Window is Maximized");
                await messageDialog.ShowAsync();
            }
            ee.Handled = true;
        };
        Window.Current.CoreWindow.SizeChanged += async (ss, ee) =>
        {
            var appView = ApplicationView.GetForCurrentView();
            if (!appView.IsFullScreen)
            {
                var messageDialog = new MessageDialog("Window is not Maximized");
                await messageDialog.ShowAsync();
    
            }
            ee.Handled = true;
        };
    }
    

    建议在 c# 中交替处理

    使用这个事件

    Window.Current.CoreWindow.SizeChanged += CoreWindow_SizeChanged;

    bool msgboxshown = false; //add this condition in above solution also if msg dialog shown multiple times at the time of mazimizing window
    
        private async void CoreWindow_SizeChanged(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.WindowSizeChangedEventArgs args)
        {            
            var appView = ApplicationView.GetForCurrentView();
    
            if (!appView.IsFullScreen && !msgboxshown)
            {
                var messageDialog = new MessageDialog("Window is not maximized");
                msgboxshown = true;                
                await messageDialog.ShowAsync();
                msgboxshown = false;
            }
    
            if (appView.IsFullScreen && !msgboxshown)
            {
                var messageDialog = new MessageDialog("Windows is maximized");
                msgboxshown = true;                
                await messageDialog.ShowAsync();
                msgboxshown = false;
    
            }
            args.Handled = true;
        }
    
    /* You can remove msgboxshown condition , it is because message dialog  will show continuously multiple times  */
    

    【讨论】:

    • 注意:IsFullScreen 和 IsFullScreenMode 不同,IsFullScreen 用于最大化(右上角的按钮),IsFullScreenMode 用于全屏(Windows 键 + shift + enter)。
    • MS 文档的链接:docs.microsoft.com/en-us/uwp/api/… 我们应该只使用 AdjacentToLeftDisplayEdge 和 AdjacentToRightDisplayEdge 来检测 SizeChanged 事件中的最大化..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-21
    • 1970-01-01
    • 2014-12-26
    相关资源
    最近更新 更多