Skip to content

Commit

Permalink
现在进程保持后可以正常新建窗口了
Browse files Browse the repository at this point in the history
  • Loading branch information
wherewhere committed Nov 7, 2023
1 parent 6017399 commit b202e16
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 50 deletions.
9 changes: 5 additions & 4 deletions CoreAppUWP/Controls/DesktopWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public static Task<DesktopWindow> CreateAsync(Action<DesktopWindowXamlSource> la
{
TaskCompletionSource<DesktopWindow> taskCompletionSource = new();

Thread thread = new(async () =>
new Thread(async () =>
{
try
{
Expand Down Expand Up @@ -158,8 +158,10 @@ public static Task<DesktopWindow> CreateAsync(Action<DesktopWindowXamlSource> la
{
taskCompletionSource.SetException(e);
}
});
thread.Start();
})
{
Name = nameof(DesktopWindowXamlSource)
}.Start();

return taskCompletionSource.Task;
}
Expand Down Expand Up @@ -204,7 +206,6 @@ public static Task<DesktopWindow> CreateAsync(DispatcherQueue dispatcherQueue, A
WindowXamlSource = source
};
taskCompletionSource.SetResult(desktopWindow);
dispatcherQueue.RunEventLoop();
}
catch (Exception e)
{
Expand Down
12 changes: 6 additions & 6 deletions CoreAppUWP/Pages/SettingsPages/SettingsPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@
Grid.Column="1"
Padding="32,0,0,8"
Foreground="{ThemeResource TextFillColorSecondaryBrush}"
Text="{Binding DeviceFamily}"
Text="{x:Bind settingspages:SettingsViewModel.DeviceFamily}"
TextWrapping="WrapWholeWords" />

<TextBlock
Expand Down Expand Up @@ -253,7 +253,7 @@
Grid.Column="1"
Padding="32,0,0,8"
Foreground="{ThemeResource TextFillColorSecondaryBrush}"
Text="{Binding WinRTVersion}"
Text="{x:Bind settingspages:SettingsViewModel.WinRTVersion}"
TextWrapping="WrapWholeWords" />

<TextBlock
Expand All @@ -267,7 +267,7 @@
Grid.Column="1"
Padding="32,0,0,8"
Foreground="{ThemeResource TextFillColorSecondaryBrush}"
Text="{Binding SDKVersion}"
Text="{x:Bind settingspages:SettingsViewModel.SDKVersion}"
TextWrapping="WrapWholeWords" />

<TextBlock
Expand All @@ -281,7 +281,7 @@
Grid.Column="1"
Padding="32,0,0,8"
Foreground="{ThemeResource TextFillColorSecondaryBrush}"
Text="{Binding WASVersion}"
Text="{x:Bind settingspages:SettingsViewModel.WASVersion}"
TextWrapping="WrapWholeWords" />

<TextBlock
Expand All @@ -295,7 +295,7 @@
Grid.Column="1"
Padding="32,0,0,8"
Foreground="{ThemeResource TextFillColorSecondaryBrush}"
Text="{Binding ToolkitVersion}"
Text="{x:Bind settingspages:SettingsViewModel.ToolkitVersion}"
TextWrapping="WrapWholeWords" />

<TextBlock
Expand Down Expand Up @@ -341,7 +341,7 @@
</controls:Setting>
</controls:SettingsGroup>
<controls:SettingsGroup Header="About">
<controls:SettingExpander Header="{x:Bind Provider.VersionTextBlockText, Mode=OneWay}">
<controls:SettingExpander Header="{x:Bind settingspages:SettingsViewModel.VersionTextBlockText, Mode=OneWay}">
<controls:SettingExpander.Icon>
<controls:AppLogo FontFamily="{StaticResource ContentControlThemeFontFamily}" />
</controls:SettingExpander.Icon>
Expand Down
30 changes: 16 additions & 14 deletions CoreAppUWP/Pages/SettingsPages/SettingsPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.UI.Windowing;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Hosting;
using Microsoft.UI.Xaml.Media.Animation;
using Microsoft.UI.Xaml.Navigation;
using System;
Expand Down Expand Up @@ -92,32 +93,33 @@ private async void Button_Click(object sender, RoutedEventArgs e)
{ desktopWindow.AppWindow.SetPresenter(AppWindowPresenterKind.CompactOverlay); }
break;
case "NewWindow":
bool isProcessKept = Provider.IsProcessKept;
_ = await WindowHelper.CreateWindowAsync(window =>
{
if (SettingsHelper.Get<bool>(SettingsHelper.IsExtendsTitleBar))
{ CoreApplication.GetCurrentView().TitleBar.ExtendViewIntoTitleBar = true; }
Frame _frame = new();
window.Content = _frame;
ThemeHelper.Initialize(window);
try { _ = _frame.Navigate(typeof(MainPage), null, new DrillInNavigationTransitionInfo()); }
catch { _ = _frame.Navigate(typeof(MainPage)); }
NavigationTransitionInfo transitionInfo = null;
if (!isProcessKept) { try { transitionInfo = new DrillInNavigationTransitionInfo(); } catch { } }
_ = _frame.Navigate(typeof(MainPage), null, transitionInfo);
BackdropHelper.SetBackdrop(window, SettingsHelper.Get<BackdropType>(SettingsHelper.SelectedBackdrop));
});
break;
case "NewAppWindow":
isProcessKept = Provider.IsProcessKept;
DesktopWindow window = await (IsCoreWindow
? WindowHelper.CreateDesktopWindowAsync(window =>
{
Frame _frame = new();
window.Content = _frame;
_ = _frame.Navigate(typeof(MainPage), null, new DrillInNavigationTransitionInfo());
})
: DispatcherQueue.CreateDesktopWindowAsync(window =>
{
Frame _frame = new();
window.Content = _frame;
_ = _frame.Navigate(typeof(MainPage), null, new DrillInNavigationTransitionInfo());
})).ConfigureAwait(false);
? WindowHelper.CreateDesktopWindowAsync(OnLaunched)
: DispatcherQueue.CreateDesktopWindowAsync(OnLaunched)).ConfigureAwait(false);
void OnLaunched(DesktopWindowXamlSource source)
{
Frame _frame = new();
source.Content = _frame;
NavigationTransitionInfo transitionInfo = null;
if (!isProcessKept) { try { transitionInfo = new DrillInNavigationTransitionInfo(); } catch { } }
_ = _frame.Navigate(typeof(MainPage), null, transitionInfo);
}
if (AppWindowTitleBar.IsCustomizationSupported()
&& SettingsHelper.Get<bool>(SettingsHelper.IsExtendsTitleBar))
{ window.ExtendsContentIntoTitleBar = true; }
Expand Down
46 changes: 20 additions & 26 deletions CoreAppUWP/ViewModels/SettingsPages/SettingsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,17 @@ public class SettingsViewModel : INotifyPropertyChanged
{
public static Dictionary<DispatcherQueue, SettingsViewModel> Caches { get; } = [];

public static string WASVersion => Assembly.GetAssembly(typeof(ExtendedActivationKind)).GetName().Version.ToString(3);
public static string WASVersion { get; } = Assembly.GetAssembly(typeof(ExtendedActivationKind)).GetName().Version.ToString(3);

public static string SDKVersion => Assembly.GetAssembly(typeof(PackageSignatureKind)).GetName().Version.ToString();
public static string SDKVersion { get; } = Assembly.GetAssembly(typeof(PackageSignatureKind)).GetName().Version.ToString();

public static string WinRTVersion => Assembly.GetAssembly(typeof(TrustLevel)).GetName().Version.ToString(3);
public static string WinRTVersion { get; } = Assembly.GetAssembly(typeof(TrustLevel)).GetName().Version.ToString(3);

public static string DeviceFamily => AnalyticsInfo.VersionInfo.DeviceFamily.Replace('.', ' ');
public static string DeviceFamily { get; } = AnalyticsInfo.VersionInfo.DeviceFamily.Replace('.', ' ');

public static string ToolkitVersion => Assembly.GetAssembly(typeof(HsvColor)).GetName().Version.ToString(3);
public static string ToolkitVersion { get; } = Assembly.GetAssembly(typeof(HsvColor)).GetName().Version.ToString(3);

public static string VersionTextBlockText { get; } = $"{Package.Current.DisplayName} v{Package.Current.Id.Version.ToFormattedString(3)}";

public DispatcherQueue Dispatcher { get; }

Expand Down Expand Up @@ -134,42 +136,34 @@ protected void SetProperty<TProperty>(ref TProperty property, TProperty value, [
}
}

public string VersionTextBlockText
{
get
{
string ver = Package.Current.Id.Version.ToFormattedString(3);
string name = Package.Current.DisplayName;
_ = GetAboutTextBlockTextAsync();
return $"{name} v{ver}";
}
}

public SettingsViewModel(DispatcherQueue dispatcher)
{
Dispatcher = dispatcher ?? DispatcherQueue.GetForCurrentThread();
Caches[dispatcher] = this;
}

private async Task GetAboutTextBlockTextAsync()
private async Task GetAboutTextBlockTextAsync(bool reset)
{
await ThreadSwitcher.ResumeBackgroundAsync();
const string langCode = "en-US";
Uri dataUri = new($"ms-appx:///Assets/About/About.{langCode}.md");
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(dataUri);
if (file != null)
if (reset || string.IsNullOrWhiteSpace(_aboutTextBlockText))
{
string markdown = await FileIO.ReadTextAsync(file);
AboutTextBlockText = markdown;
await ThreadSwitcher.ResumeBackgroundAsync();
const string langCode = "en-US";
Uri dataUri = new($"ms-appx:///Assets/About/About.{langCode}.md");
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(dataUri);
if (file != null)
{
string markdown = await FileIO.ReadTextAsync(file);
AboutTextBlockText = markdown;
}
}
}

public void KeepProcess()
{
if (!_isProcessKept)
{
_ = Dispatcher.TryEnqueue(() => Dispatcher.RunEventLoop(DispatcherRunOptions.ContinueOnQuit, new DispatcherExitDeferral()));
IsProcessKept = true;
Dispatcher.RunEventLoop(DispatcherRunOptions.ContinueOnQuit, new DispatcherExitDeferral());
}
}

Expand All @@ -182,7 +176,7 @@ public async Task Refresh(bool reset)
nameof(SelectedBackdrop),
nameof(IsExtendsTitleBar));
}
await GetAboutTextBlockTextAsync();
await GetAboutTextBlockTextAsync(reset);
}
}
}

0 comments on commit b202e16

Please sign in to comment.