在下面的代码中,我不能修补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);
});
}
但是,两个窗口的大小完全相同。
是的,我刚刚发现了同样的问题,我已经报告了这个问题。一旦我收到这个问题的回复,我会在这里发布。
到目前为止,我们可以使用一个变通方法来解决这个问题,因为你想要展开一个比主窗口小的新窗口,你可以使用ApplicationView。TryResizeView | TryResizeView方法以编程方式设置新窗口的大小。
例如: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 });