我正在嘗試讓應用程式為每個檔案打開一個視窗。
- 代碼 - -
應用程式.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();
}
主頁.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;
}
筆記:
- 使用的 GetText 方法等價于 RichEditBox.Document.GetText(TextGetOptions.FormatRtf, out string Value);
- REB 是用戶輸入的主要作業區
- RTB 是與 REB 比較以查看內容是否已保存的框(如果內容已保存,則 GetText 方法應回傳它們的值相等)
預期行為:如果應用程式有一個帶有文本的活動視窗,應用程式應在輔助視窗中雙擊打開一個檔案。否則,如果文本等于“”,則應用程式應將其替換為檔案中的任何內容。
實際行為:應用程式覆寫文本、崩潰、隨機生成視窗或創建只能使用任務管理器或 Visual Studio 殺死的黑色視窗
uj5u.com熱心網友回復:
我不確定您使用的檔案型別是什么,所以我沒有檢查文本部分。但我不得不說你創建新視窗的方式是不正確的。如果您想每次打開新檔案時都打開一個新視窗,則不必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.
在MainPage.cs
protected override void OnNavigatedTo(NavigationEventArgs e)
{
// this is the file you need
var d = e.Parameter;
}
您可以先嘗試此代碼,以確保您的視窗已正確創建和顯示。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/481577.html
上一篇:C#組合框默認值
下一篇:如何打包和匯出您的所有專案?
