在 Windows 通用应用程序中使用 LaunchUriAsync 时更改目标应用程序大小

Change the Target App Size when using LaunchUriAsync in Windows Universal Application

我刚开始玩 Windows 应用程序开发。我试图通过单击按钮从应用程序打开浏览器 window。我希望 Target App(在本例中是我的 window browser)的尺寸较小。如果我能通过某种方式给出尺寸就更好了。

我发现 DesiredRemainingView 可以解决问题,但不知何故没有。

任何帮助都是

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.System;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

namespace App1
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public ViewSizePreference UseHalf { get; private set; }

        //public ViewSizePreference UseHalf { get; private set; }
        public MainPage()
        {
            this.InitializeComponent();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            greetingOutput.Text = "Hello, " + nameInput.Text + "!";
        }

        async private void Button_Click(object sender, RoutedEventArgs e)
        {
            //greetingOutput.Text = UseHalf.ToString();
            // The URI to launch
            var uriBing = new Uri("http://www.bing.com");

            // Set the option to show a warning
            var promptOptions = new Windows.System.LauncherOptions();
            //promptOptions.TreatAsUntrusted = true;
            promptOptions.DesiredRemainingView = UseHalf;

            // Launch the URI
            var success = await Windows.System.Launcher.LaunchUriAsync(uriBing, promptOptions);

            if (success)
            {
                // URI launched
            }
            else
            {
                // URI launch failed
            }
        }

    }
}

是的,我们可以使用 LauncherOptions.DesiredRemainingView 来指定目标应用程序所需的视图大小。但是您使用的是哪种 Windows 浏览器?

如果您的 Windows 浏览器是 Internet Explorer 或 Edge,请确保您在 运行 之前关闭了 Windows 浏览器] 源应用程序,之后它将打开一个新的 Windows 浏览器以显示目标应用程序,该目标应用程序将转到 www.bing.com 网站并且 LauncherOptions.DesiredRemainingView 可以正常工作。

但是如果我们不关闭 Windows 浏览器(Internet Explorer 或 Edge),之后当我们 运行 源应用程序时,目标应用程序将附加为当前 Windows 浏览器的新选项卡,而不是打开新的 Windows 浏览器。这样我们就看不到LauncherOptions.DesiredRemainingView应该带来的效果了。

但如果您的Windows浏览器是Chrome或其他第三方浏览器,LauncherOptions.DesiredRemainingView可能无法正常工作。第三方浏览器无法回复 LauncherOptions.DesiredRemainingView 以执行正确行为的原因可能与浏览器的内部设置有关。因为如果我们要正确实现启动功能,目标应用程序需要了解协议或规则或其他理论。

以下是在 运行 应用程序之前关闭 Windows Brower Edge 后的结果:

谢谢。