UWP 为每个打开的文件创建新的 window

UWP create new window for each file opened

我正在尝试让应用程序为每个文件打开 window。

-- 代码 ---

App.cs:

    protected async override void OnFileActivated(FileActivatedEventArgs Args)
    {
        //Opens Main Page
        base.OnFileActivated(Args);
        var RF = new Frame();
        var MW = new MainPage();
        AppWindow appWindow = await AppWindow.TryCreateAsync();
        var TB = appWindow.TitleBar;
        RF.Navigate(typeof(MainPage), Args);
        ElementCompositionPreview.SetAppWindowContent(appWindow, RF);
        appWindow.Equals(MW);
        Window.Current.Equals(MW);
        await appWindow.TryShowAsync();
        TB.ButtonHoverBackgroundColor = Colors.White;
        TB.ButtonHoverForegroundColor = Colors.Black;
        TB.ButtonBackgroundColor = Colors.Transparent;
        TB.ButtonPressedBackgroundColor = Colors.WhiteSmoke;
        TB.ButtonPressedForegroundColor = Colors.Black;
        TB.ButtonInactiveBackgroundColor = Colors.Transparent;
        TB.ButtonInactiveForegroundColor = Color.FromArgb(1, 3, 165, 252);
        TB.ExtendsContentIntoTitleBar = true;
        Window.Current.Activate();
    }

MainPage.cs:

    protected override async void OnNavigatedTo(NavigationEventArgs EvArgs)
    {
        //File opened arguments
        base.OnNavigatedTo(EvArgs);
        var Args = EvArgs.Parameter as IActivatedEventArgs;
        var FArgs = Args as FileActivatedEventArgs;
        string Value = GetText(REB);
        string SecValue = GetText(RTB);
        if (Args != null)
        {
            //Check if the app is opened by file
            if (Args.Kind == ActivationKind.File)
            {
                //Set file content
                TXTFile = FArgs.Files[0] as StorageFile;
                if (Value == "")
                {
                    var Str = await TXTFile.OpenReadAsync();
                    ContentDialog ED2 = FileSaveDialog;
                    ED2.PrimaryButtonClick += ED2_PrimaryButtonClick;
                    void ED2_PrimaryButtonClick(ContentDialog Sender, ContentDialogButtonClickEventArgs DialogEvArgs)
                    {
                        //Save the file if it isn't saved
                        Save();
                    }
                    ED2.SecondaryButtonClick += ED2_SecondaryButtonClick;
                    void ED2_SecondaryButtonClick(ContentDialog Sender, ContentDialogButtonClickEventArgs DialogEvArgs)
                    {
                        //Don't save the file
                        //Set document content
                        REB.Document.LoadFromStream(TextSetOptions.FormatRtf, Str);
                        RTB.Document.LoadFromStream(TextSetOptions.FormatRtf, Str);
                        Str.Dispose();
                    }
                    ED2.CloseButtonClick += ED2_CloseButtonClick;
                    void ED2_CloseButtonClick(ContentDialog Sender, ContentDialogButtonClickEventArgs DialogEvArgs)
                    {
                        //Cancel the action
                    }
                    await ED2.ShowAsync();
                    Str.Dispose();
                }
                else
                {
                    //Set document content
                    var Str = await TXTFile.OpenReadAsync();
                    REB.Document.LoadFromStream(TextSetOptions.FormatRtf, Str);
                    RTB.Document.LoadFromStream(TextSetOptions.FormatRtf, Str);
                    Str.Dispose();
                }
            }
        }
        else
        {
            //If there are no arguments, the file, and the RichEditBox should both remain empty 
        }
    }

    public string GetText(RichEditBox RichEditor)
    {
        RichEditor.Document.GetText(TextGetOptions.FormatRtf, out string Text);
        var Range = RichEditor.Document.GetRange(0, Text.Length);
        Range.GetText(TextGetOptions.FormatRtf, out string Value);
        return Value;
    }

注:

预期行为: 如果该应用程序有一个 window 活动文本,该应用程序应该在辅助 window 中双击打开一个文件。否则,如果文本等于“”,应用程序应将其替换为文件中的任何内容。

实际行为: 该应用程序覆盖文本、崩溃、随机生成 windows 或创建只能使用任务管理器或 Visual Studio

终止的黑色 windows

我不确定您使用的文件类型是什么,所以我没有检查文本部分。但我不得不说,您创建新 window 的方式不正确。如果你想每次打开一个新文件时都打开一个新的window,你不必每次都调用Window.Current.Activate();。我做了一个简单的演示,你可以检查一下。

App.xaml.cs:

   protected async override void OnFileActivated(FileActivatedEventArgs args)
    {
        Frame rootFrame = Window.Current.Content as Frame;

        // Do not repeat app initialization when the Window already has content,
        // just ensure that the window is active
        if (rootFrame == null)
        {
            // Create a Frame to act as the navigation context and navigate to the first page
            rootFrame = new Frame();
            // Place the frame in the current Window
            Window.Current.Content = rootFrame;

            rootFrame.Navigate(typeof(MainPage), args.Files);

            Window.Current.Activate();
        }
        else 
        {
            AppWindow appWindow = await AppWindow.TryCreateAsync();

            Frame appWindowContentFrame = new Frame();
            appWindowContentFrame.Navigate(typeof(MainPage),args.Files);

            ElementCompositionPreview.SetAppWindowContent(appWindow, appWindowContentFrame);

            await appWindow.TryShowAsync();
        }
    }

当您第一次启动应用程序时,您应该完成正常的启动过程。当应用程序多次启动时,您可以使用 AppWindow.

创建一个新的 window

MainPage.cs

  protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        // this is the file you need
        var d = e.Parameter;
    }

您可以先尝试此代码以确保您的 window 已正确创建和显示。