【问题标题】:How get different size windows in UWP Multiple Views?如何在 UWP 多视图中获得不同大小的窗口?
【发布时间】:2016-08-29 15:41:26
【问题描述】:

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

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

【问题讨论】:

  • 你为什么不用<Popup>控制? Pupup class
  • Hassaan:我现在正在尝试 Popup,但还不能在显示屏上滑动它。这是我的要求之一。

标签: c# xaml uwp sizing


【解决方案1】:

但是,两个窗口的大小完全相同。

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

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

例如:

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 event

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

或者如果你不介意看新窗口的大小调整过程,你也可以试试这段代码,在这个方法中不需要将大小作为参数发送给新窗口:

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

【讨论】:

  • 事实证明,较长的参数方法不起作用,但较短的第二个选项有效。我想我需要进一步研究这个。我得到了一些不良的副作用,例如多个帧不会消失并落后于其他帧等。今天我试图让一个弹出窗口做我想做的事情,但我不能在显示器周围滑动它。我只是想建立一个有用的参考系统,可以从关键内容中移除。
  • 原来如果我setnewAppView.TryResizeView(new Size { Width = 300, Height = 200 });不起作用,而是使用rect.Width - 300,为什么?如何将新窗口设置为固定大小?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-09-19
  • 2010-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-02
  • 2015-03-19
相关资源
最近更新 更多