Detect click on resize window UWP

空扰寡人 提交于 2020-07-05 05:11:46

问题


I would like to launch a message dialog at the click of the resize button..

I inserted the message dialog in any click, but how can I launch it in the resize window?

Resize button

The code:

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();
    }
}

I approached a possible solution but, I just need the resize button click, is it possible?

The code:

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

Thanks in advance!


回答1:


You can subscribe to Page Size changed event for this

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;
    };
}

Alternately handle it in c# Recommended

using this event

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  */


来源:https://stackoverflow.com/questions/47610112/detect-click-on-resize-window-uwp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!