如何通过WPF中的按钮启动UWP应用程序

yftpprvb  于 2023-08-07  发布在  其他
关注(0)|答案(1)|浏览(156)

如果我直接在Visual Studio中启动我的UWP应用程序(WLAN连接器),它工作正常。但是,如果我尝试在WPF程序中启动它,它将以正确的大小打开UWP窗口,但它只显示标准的splashscreen,它不会启动程序?我不知道怎么了?
安装Nuget PCage:NUGET Microsoft.Toolkit.Wpf.UI.XamlHost NUGET Microsoft.Windows.SDK.Contracts .NET 4.8
希望有人比我更有UWP的经验。谢啦,谢啦
下面你会发现洞项目,WPF启动程序和UWP应用程序,将无法从WPF应用程序正确启动,也许有人可以帮助我:
UWP应用程序(WLAN连接器)

App.xaml.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

using Windows.UI;

using Windows.UI.ViewManagement;
using Windows.ApplicationModel.Core;

namespace WLANConnectorUWP
{
    /// <summary>
    /// Provides application-specific behavior to supplement the default Application class.
    /// </summary>
    sealed partial class App : Application
    {
        /// <summary>
        /// Initializes the singleton application object.  This is the first line of authored code
        /// executed, and as such is the logical equivalent of main() or WinMain().
        /// </summary>
        public App()
        {
            this.InitializeComponent();
            this.Suspending += OnSuspending;
        }

        /// <summary>
        /// Invoked when the application is launched normally by the end user.  Other entry points
        /// will be used such as when the application is launched to open a specific file.
        /// </summary>
        /// <param name="e">Details about the launch request and process.</param>
        protected override void OnLaunched(LaunchActivatedEventArgs e)
        {
            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();

                rootFrame.SizeChanged += RootFrame_SizeChanged;
                Window.Current.Content = rootFrame;


                if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
                {
                    //TODO: Load state from previously suspended application
                }

                // Place the frame in the current Window
                Window.Current.Content = rootFrame;
            }

            if (e.PrelaunchActivated == false)
            {
                if (rootFrame.Content == null)
                {

                    // When the navigation stack isn't restored navigate to the first page,
                    // configuring the new page by passing required information as a navigation
                    // parameter
                    rootFrame.Navigate(typeof(MainPage), e.Arguments);
                }
                // Ensure the current window is active
                ApplicationView.PreferredLaunchViewSize = new Size(300, 300);
                ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize;

                var view = ApplicationView.GetForCurrentView();
                var titleBar = view.TitleBar;

                if (titleBar != null)
                {
                    titleBar.ButtonBackgroundColor = Colors.Transparent;
                    titleBar.ButtonInactiveBackgroundColor = Colors.Transparent;
                    titleBar.ButtonForegroundColor = Colors.Transparent;
                    titleBar.ButtonInactiveForegroundColor = Colors.Transparent;
                    titleBar.BackgroundColor = Colors.Transparent;
                    titleBar.ForegroundColor = Colors.Transparent;
                    titleBar.InactiveBackgroundColor = Colors.Transparent;
                    titleBar.InactiveForegroundColor = Colors.Transparent;

                    CoreApplication.GetCurrentView().TitleBar.ExtendViewIntoTitleBar = true;
                }

                Window.Current.Activate();
            }
        }

        /// <summary>
        /// Invoked when Navigation to a certain page fails
        /// </summary>
        /// <param name="sender">The Frame which failed navigation</param>
        /// <param name="e">Details about the navigation failure</param>
        void OnNavigationFailed(object sender, NavigationFailedEventArgs e)
        {
            throw new Exception("Failed to load Page " + e.SourcePageType.FullName);
        }

        /// <summary>
        /// Invoked when application execution is being suspended.  Application state is saved
        /// without knowing whether the application will be terminated or resumed with the contents
        /// of memory still intact.
        /// </summary>
        /// <param name="sender">The source of the suspend request.</param>
        /// <param name="e">Details about the suspend request.</param>
        private void OnSuspending(object sender, SuspendingEventArgs e)
        {
            var deferral = e.SuspendingOperation.GetDeferral();
            //TODO: Save application state and stop any background activity
            deferral.Complete();
        }

        private void RootFrame_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            ApplicationView.GetForCurrentView().TryResizeView(new Size(300, 300));
        }

    }
}

字符串

MainPage.xaml:

<Page
    x:Class="WLANConnectorUWP.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:WLANConnectorUWP"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Width="300" Height="200">

    <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
        <ComboBox x:Name="AvailableNetworksComboBox" Width="200" Header="WLAN-Netzwerke"/>
        <TextBox x:Name="PasswordTextBox" Width="200" Header="Passwort" Margin="0,10,0,0"/>
        <Button x:Name="ConnectButton" Width="200" Content="Verbinden" Margin="0,10,0,0" Click="ConnectButton_Click"/>
    </StackPanel>
</Page>

MainPage.xaml.cs

using System;
using System.Linq;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

using Windows.Devices.Enumeration;
using Windows.Devices.WiFi;
using Windows.Security.Credentials;
using Windows.UI.Popups;

// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

namespace WLANConnectorUWP
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        private WiFiAdapter _wifiAdapter;

        public MainPage()
        {
            InitializeComponent();
            InitializeWiFiAdapter();
        }

        private async void InitializeWiFiAdapter()
        {
            var access = await WiFiAdapter.RequestAccessAsync();
            if (access != WiFiAccessStatus.Allowed)
            {
                await new MessageDialog("Zugriff auf WLAN-Adapter nicht erlaubt").ShowAsync();
                return;
            }

            var result = await DeviceInformation.FindAllAsync(WiFiAdapter.GetDeviceSelector());
            if (result.Count == 0)
            {
                await new MessageDialog("Kein WLAN-Adapter gefunden").ShowAsync();
                return;
            }

            _wifiAdapter = await WiFiAdapter.FromIdAsync(result.First().Id);
            await _wifiAdapter.ScanAsync();

            foreach (var network in _wifiAdapter.NetworkReport.AvailableNetworks)
            {
                AvailableNetworksComboBox.Items.Add(network.Ssid);
            }
        }

        private async void ConnectButton_Click(object sender, RoutedEventArgs e)
        {
            var selectedSsid = AvailableNetworksComboBox.SelectedItem as string;
            if (selectedSsid == null)
            {
                await new MessageDialog("Bitte wählen Sie ein WLAN-Netzwerk aus der Liste aus").ShowAsync();
                return;
            }

            var selectedNetwork = _wifiAdapter.NetworkReport.AvailableNetworks.FirstOrDefault(network => network.Ssid == selectedSsid);
            if (selectedNetwork == null)
            {
                await new MessageDialog("Das ausgewählte Netzwerk wurde nicht gefunden").ShowAsync();
                return;
            }

            string password = PasswordTextBox.Text;
            WiFiReconnectionKind reconnectionKind = WiFiReconnectionKind.Automatic;
            var credential = new PasswordCredential { Password = password };

            var connectionResult = await _wifiAdapter.ConnectAsync(selectedNetwork, reconnectionKind, credential);

            if (connectionResult.ConnectionStatus == WiFiConnectionStatus.Success)
            {
                await new MessageDialog("Erfolgreich mit dem Netzwerk verbunden").ShowAsync();
            }
            else
            {
                await new MessageDialog($"Verbindungsfehler: {connectionResult.ConnectionStatus}").ShowAsync();
            }
        }
    }
}

WPF启动程序:
主窗口.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;
namespace UWPStartTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Process.Start("myapp://");
        }
    }
}

//NUGET Microsoft.Toolkit.Wpf.UI.XamlHost wird benltigt!!!
//NUGET Microsoft.Windows.SDK.Contracts für die passende WIN 10 Version
//.NET 4.8 oder höher

主窗口.xaml

<Window x:Class="UWPStartTest.MainWindow"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:xamlhost="clr-namespace:Microsoft.Toolkit.Wpf.UI.XamlHost;assembly=Microsoft.Toolkit.Wpf.UI.XamlHost"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Button Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Click="Button_Click"/>

    </Grid>
</Window>

uoifb46i

uoifb46i1#

您可以使用Protocol在WPF中启动UWP。
1.在UWP项目的文件Package.appxmanifest中定义协议名称。

<Extensions>
  <uap:Extension Category="windows.protocol">
     <uap:Protocol Name="uwptestapp">
         <uap:DisplayName>UWP-Test-app</uap:DisplayName>
     </uap:Protocol>
  </uap:Extension>
</Extensions>

字符串
1.覆盖App.xaml.cs中的Application.OnActivated。

protected override void OnActivated(IActivatedEventArgs args)
{
    Frame rootFrame = Window.Current.Content as Frame;
    if (rootFrame == null)
    {
        rootFrame = new Frame();
        Window.Current.Content = rootFrame;
    }

    rootFrame.Navigate(typeof(MainPage));
    Window.Current.Activate();
}


1.使用LaunchUriAsync和协议名称在WPF中启动UWP。

using Windows.System;

private async void Button_Click(object sender, RoutedEventArgs e)
{
    var res=await Launcher.LaunchUriAsync(new Uri("uwptestapp:"));
}


注意:要在桌面应用中调用Windows运行时API,您需要修改目标Framework。
右键单击您的WPF项目->选择Unload Project->修改TargetFramework

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net6.0-windows10.0.22621.0</TargetFramework>
    <Nullable>enable</Nullable>
    <UseWPF>true</UseWPF>
    <SupportedOSPlatformVersion>10.0.22621.0</SupportedOSPlatformVersion>
  </PropertyGroup>

</Project>

相关问题