如何在 UWP 多视图中获得不同大小的窗口? [英] How get different size windows in UWP Multiple Views?

查看:21
本文介绍了如何在 UWP 多视图中获得不同大小的窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,我无法修改 ViewSizePreference 选项并使子项小于父项.我正在从页面制作一个弹出窗口.那部分工作正常.但是,两个窗口的大小完全相同.

In the following code I can not tinker with the ViewSizePreference options and get the child to be smaller than the parent. I am making a popup from a page. That part works fine. However, both windows are the exact same size.

private async void Test_Click(object sender,RoutedEventArgs e){
var currentAV = ApplicationView.GetForCurrentView();
var newAV = CoreApplication.CreateNewView();
await newAV.Dispatcher.RunAsync(
                CoreDispatcherPriority.Normal,
                async () =>
                {
                    var newWindow = Window.Current;
                    var newAppView = ApplicationView.GetForCurrentView();
                    newAppView.Title = "New window";

                    var frame = new Frame();
                    frame.Navigate(typeof(MainPage), null);
                    newWindow.Content = frame;
                    newWindow.Activate();

                    await ApplicationViewSwitcher.TryShowAsStandaloneAsync(
                        newAppView.Id,
                        ViewSizePreference.UseMinimum,
                        currentAV.Id,
                        ViewSizePreference.UseMinimum);
                });
}

推荐答案

然而,两个窗口的大小完全相同.

However, both windows are the exact same size.

是的,我刚刚发现了同样的问题,我已经报告了这个问题.一旦我得到这个问题的回应,我会在这里发布.

Yes, I just found the same issue, and I've reported this issue. As soon as I get the response of this issue, I will post here.

现在,我们可以使用一种变通方法来解决这个问题,因为您想扩展一个比主窗口尺寸更小的新窗口,您可以使用 ApplicationView.TryResizeView |tryResizeView 方法以编程方式设置新窗口的大小.

By now, we can use a workaround to solve this problem, since your want to expand a new window with a smaller size than the main window, you can use ApplicationView.TryResizeView | tryResizeView method to set the size of new window programmatically.

例如:

private async void Test_Click(object sender, RoutedEventArgs e)
{
    var currentAV = ApplicationView.GetForCurrentView();
    //get the bounds of current window.
    var rect = Window.Current.Bounds;
    var newAV = CoreApplication.CreateNewView();
    await newAV.Dispatcher.RunAsync(
                    CoreDispatcherPriority.Normal,
                    async () =>
                    {
                        var newWindow = Window.Current;
                        var newAppView = ApplicationView.GetForCurrentView();
                        newAppView.Title = "New window";

                        var frame = new Frame();
                        //send current window's size as parameter.
                        frame.Navigate(typeof(MainPage), rect.Width.ToString() + ":" + rect.Height.ToString());
                        newWindow.Content = frame;
                        newWindow.Activate();

                        await ApplicationViewSwitcher.TryShowAsStandaloneAsync(
                            newAppView.Id,
                            ViewSizePreference.UseHalf,
                            currentAV.Id,
                            ViewSizePreference.UseHalf);
                    });
}

然后在OnNavigatedTo方法和Loaded事件中:

private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
    if (size != null)
    {
        var newwidth = Convert.ToInt32(size[0]) - 300;
        var newheight = Convert.ToInt32(size[1]) - 200;
        ApplicationView.GetForCurrentView().TryResizeView(new Size { Width = newwidth, Height = newwidth });
    }
}

private string[] size;

protected override void OnNavigatedTo(NavigationEventArgs e)

{
    if (e.Parameter.ToString() != "")
    {
        size = e.Parameter.ToString().Split(':');
    }
}

或者如果你不介意看一下新窗口的resize过程,也可以试试这个代码,这个方法中不需要将size作为参数发送给新窗口:

Or if you don't mind to see the resizing process of the new window, you can also try this code, and there is no need to send size as parameter to the new window in this method:

var frame = new Frame();
frame.Navigate(typeof(MainPage), null);
newWindow.Content = frame;
newWindow.Activate();

await ApplicationViewSwitcher.TryShowAsStandaloneAsync(
    newAppView.Id,
    ViewSizePreference.UseHalf,
    currentAV.Id,
    ViewSizePreference.UseHalf);
newAppView.TryResizeView(new Size { Width = rect.Width - 300, Height = rect.Height - 200 });

这篇关于如何在 UWP 多视图中获得不同大小的窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆